Unity 2D:面向方向而不移动

Unity 2D: Facing direction without moving

我正在尝试在 unity2d 中制作神奇宝贝游戏。我设法使网格移动,但我不知道如何在不移动的情况下面对方向(停留在同一个地方,当 A 或 W 或 S 或 D 被按下一次只是面对方向而不移动)。

这就是我目前所拥有的:

[SerializeField]
float walkingVelocity = 2;
[SerializeField]
float runingVelocity = 4;

Vector3 p;                                // For movement
Animator anim;

Vector2 input;
float actualSpeed = 0;

void Start()
{
    anim = GetComponent<Animator>();
    p = transform.position;          // Take the initial position
}

void FixedUpdate()
{
    input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
    actualSpeed = Input.GetKey(KeyCode.LeftShift) ? walkingVelocity : runingVelocity;

    if (input != Vector2.zero && p == transform.position)
    {

        //CalcularHierbaAlta();
        if (Mathf.Abs(input.x) > Mathf.Abs(input.y))
        {
            if (input.x > 0)
            {
                //direccion = Direccion.Este;
                //PuedeMoverse = CalcularFrente();
                p += Vector3.right;
                anim.SetFloat("input_x", input.x);
                anim.SetFloat("input_y", input.y);
                anim.SetBool("isMoving", true);
            }
            else
            {
                p -= Vector3.right;
                anim.SetFloat("input_x", input.x);
                anim.SetFloat("input_y", input.y);
                anim.SetBool("isMoving", true);
            }
        }
        else
        {
            if (input.y > 0)
            {
                p += Vector3.up;
                anim.SetFloat("input_x", input.x);
                anim.SetFloat("input_y", input.y);
                anim.SetBool("isMoving", true);
            }
            else
            {
                p -= Vector3.up;
                anim.SetFloat("input_x", input.x);
                anim.SetFloat("input_y", input.y);
                anim.SetBool("isMoving", true);
            }
        }
    }else if (input == Vector2.zero)
    {
        anim.SetBool("isMoving", false);
    }
    transform.position = Vector3.MoveTowards(transform.position, p, actualSpeed * Time.deltaTime);
}

非常感谢!

检查您是否已经面向某个方向。如果不是,则旋转您的对象。如果您已经面向按键将使您进入的方向,请移动 player/object。

我遇到了完全相同的问题,这就是我设法做到的 ;) 行

if (pressWalkTime >= 8) {

可用于确定在开始向该方向移动之前按钮可以按下多少帧。

    if (horizontal != 0 || vertical != 0) {
        if (pressWalkTime != 0 || !direction.Equals (new Vector2 (horizontal, vertical))) {
            pressWalkTime++;

            if (pressWalkTime >= 8) {
                pressWalkTime = 0;
            }   
        } else {
            pressWalkTime = 0;
        }   

        direction = new Vector2 (horizontal, vertical);

        if (pressWalkTime == 0 && base.AttemptMove (horizontal, vertical)) {
            position += new Vector2 (horizontal, vertical);
        }

        if (horizontal == 1)
            animator.Play ("Walk-Right");

        if (horizontal == -1)
            animator.Play ("Walk-Left");

        if (vertical == 1)
            animator.Play ("Walk-Up");

        if (vertical == -1)
            animator.Play ("Walk-Down");

    } else
        pressWalkTime = 0;
} 

你还应该注意到这不能处理转弯。这意味着如果你已经在一个方向上行走然后转向另一个方向你必须再次等待 8 帧才能开始移动。这个问题明天就会解决,但我相信你可以自己解决 ;)

pressWalkTime是class这个逻辑写入的一个属性,初始化为0。