【问题标题】:Unity 2D: Facing direction without movingUnity 2D:面向方向而不移动
【发布时间】:2017-04-05 21:47:44
【问题描述】:

我正在尝试在 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);
}

非常感谢!

【问题讨论】:

    标签: c# unity5 unity2d


    【解决方案1】:

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

    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。

    【讨论】:

    • 哦,顺便说一句 base.AttemptMove 只是检查您要输入的字段是否可访问。但我想那是显而易见的
    【解决方案2】:

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

    【讨论】:

    • 天哪,太明显了!我还没有尝试过,但它很有意义。谢谢! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多