如何制作像《Feed Frenzy》中那样动人的角色?

问题描述 投票:0回答:1

我创建的这个游戏,角色只能通过跟随光标向右移动。我想要像疯狂喂食一样,当我移动鼠标时,角色会跟随光标,当短按左键单击时,角色会有更快的速度。

我的代码中的问题是当我长按左键时,角色也会移动得更快。

这是我的代码:

//variable for moving characters
public float moveSpeed;
public float turnSpeed;
private Vector3 moveDirection;

//variable for detect short/long click
private float t0;
private bool longClick;
private bool shortClick;

void Start () 
{
    moveDirection = Vector3.right;
    t0 = 0f;
    longClick = false;
    shortClick = false;

}

void Update () 
{
    // code to moving my characters (moving to right only) 
    Vector3 currentPosition = transform.position;
    Vector3 moveToward = Camera.main.ScreenToWorldPoint( Input.mousePosition );
    moveDirection = moveToward - currentPosition;
    moveDirection.z = 0; 
    moveDirection.Normalize();
    if (moveDirection.x <= 0)
    {
        moveDirection = Vector3.right;
    }       

    Quaternion rot = Quaternion.LookRotation (transform.position - moveToward, Vector3.forward);
    rot *= Quaternion.Euler (0, 0, 90); 
    transform.rotation = rot;
    transform.eulerAngles = new Vector3 (0, 0, transform.eulerAngles.z);
    Vector3 target = moveDirection * moveSpeed + currentPosition;
    transform.position = Vector3.Lerp( currentPosition, target, Time.deltaTime );
    float targetAngle = Mathf.Atan2(moveDirection.y, moveDirection.x) * Mathf.Rad2Deg;
    transform.rotation = Quaternion.Slerp( transform.rotation, 
                         Quaternion.Euler( 0, 0, targetAngle ), 
                         turnSpeed * Time.deltaTime );


    //THIS IS MY PROBLEM! 
    //IN MY CODE BELOW, WHEN I PRESS LONG CLICK IT HAVE moveSpeed=12.
    //I WANT moveSpeed = 12 WHEN I PRESS SHORT CLICK ONLY
    if (Input.GetMouseButtonDown (0)) 
    {
        t0 = Time.time;
        moveSpeed = 12;
    }

    else if (Input.GetMouseButtonUp(0) && (Time.time - t0) > 0.5f)
    {
        longClick = true;
        moveSpeed = 3;
    }
    else if (Input.GetMouseButtonUp(0) && (Time.time - t0) < 0.5f)
    {
        shortClick = true;
        moveSpeed = 3;
    }
    longClick = false;
    shortClick = false;
}
c# unity-game-engine click
1个回答
0
投票
    function Update()
{
    longClick = false;     //Remove this
    shortClick = false;    //Remove this
    if ( Input.GetMouseButtonDown (0) )
    {
           t0 = Time.time ;
           longClick = false;
            shortClick = false;
    }

    if ( Input.GetMouseButtonUp(0)  (Time.time -  t0) < 0.2 )
   {
        longClick = false;
        shortClick = true;
   }

    if ( Input.GetMouseButtonUp(0)  (Time.time -  t0) > 0.2 )
   {
      longClick = true;
      shortClick = false;
   }

       //Process Movement
}

然后使用

if(longclick)
if(shortClick)
来调整速度。当短暂点击时,角色会快速移动,在下一次更新中,角色会恢复到相同的速度。 如果您希望角色继续以相同的速度移动直到获得下一次点击,请在更新后立即删除线条。

设置

longClick
shortClick
后处理角色移动

© www.soinside.com 2019 - 2024. All rights reserved.