繁体   English   中英

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);
}

非常感谢你!

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

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是写入此逻辑的类的一个属性,并用0初始化。

检查您是否已经面对方向。 如果没有,请旋转对象。 如果您已经朝着按键的方向移动,请移动播放器/对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM