【问题标题】:How do I move and flip my character in Unity2D?如何在 Unity2D 中移动和翻转我的角色?
【发布时间】:2017-04-11 20:56:53
【问题描述】:

我是 Unity 的新手。我正在玩一个 2D 横向卷轴。以下是我正在尝试编写的脚本的功能:

  • 左右移动
  • 改变方向时翻转字符
  • 在持续移动时将速度提高到上限

目前,唯一有效的是动画过渡。我不确定出了什么问题。代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Avatar_Manager : MonoBehaviour {
    private Animator anim;
    private Rigidbody2D rigidbody2D;
    private Transform trans;
    private int direction;
    private bool moving;
    public const float acceleration = 1.0f / 180;
    public float horizontal_speed;
    public float vertical_speed;
    // Use this for initialization
    void Start () {
        anim = GetComponent<Animator>();
        rigidbody2D = GetComponent<Rigidbody2D>();
        trans = GetComponent<Transform>();
        //Default facing right
        direction = 1;
        moving = false;
        vertical_speed = 0;
    }

    // Update is called once per frame
    void Update () {
        //Check direction
        if (rigidbody2D.velocity.x < 0)
        {
            direction = -1;
        }
        else if (rigidbody2D.velocity.x > 0)
        {
            direction = 1;
        }
        // Move right
        if (Input.GetKeyDown(KeyCode.D))
        {
            //Flip Avatar if facing left
            if (direction == -1)
            {
                direction = 1;
                trans.rotation.Set(trans.rotation.x, trans.rotation.y + 180, trans.rotation.z, trans.rotation.w);
            }
            //Start moving
            if (!moving)
            {
                moving = true;
                horizontal_speed = 10;
                rigidbody2D.velocity = new Vector2(horizontal_speed, vertical_speed);
                anim.SetInteger("State", 1);
            }
            //Update speed
            else
            {
                if(horizontal_speed < 20)
                {
                    horizontal_speed += acceleration;
                    rigidbody2D.velocity.Set(horizontal_speed, vertical_speed);
                }
            }
        }
        //Stop moving right
        if (Input.GetKeyUp(KeyCode.D))
        {
            moving = false;
            horizontal_speed = 0;
            rigidbody2D.velocity.Set(horizontal_speed, vertical_speed);
            anim.SetInteger("State", 0);
        }

        // Move left
        if (Input.GetKeyDown(KeyCode.A))
        {
            //Flip Avatar if facing right
            if (direction == 1)
            {
                direction = -1;
                trans.rotation.Set(trans.rotation.x, trans.rotation.y - 180, trans.rotation.z, trans.rotation.w);
            }
            //Start moving
            if (!moving)
            {
                moving = true;
                horizontal_speed = -10;
                rigidbody2D.velocity = new Vector2(horizontal_speed, vertical_speed);
                anim.SetInteger("State", 1);
            }
            //Update speed
            else
            {
                if (horizontal_speed > -20)
                {
                    horizontal_speed -= acceleration;
                    rigidbody2D.velocity.Set(horizontal_speed, vertical_speed);
                }
            }
        }
        //Stop moving right
        if (Input.GetKeyUp(KeyCode.A))
        {
            moving = false;
            horizontal_speed = 0;
            rigidbody2D.velocity.Set(horizontal_speed, vertical_speed);
            anim.SetInteger("State", 0);
        }
    }
}

【问题讨论】:

    标签: c# unity3d-2dtools unity2d


    【解决方案1】:

    改变这个

    if (rigidbody2D.velocity.x < 0)
            {
                direction = -1;
            }
    else if (rigidbody2D.velocity.x > 0)
            {
                direction = 1;
            }
    

    到这里

    if (rigidbody2D.velocity.x < 0)
            {
                 transform.localscale = new Vector3 (-1,1,1);
            }
    else if (rigidbody2D.velocity.x > 0)
            {
                 transform.localscale = new Vector3 (1,1,1);
            }
    

    【讨论】:

      猜你喜欢
      • 2014-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-16
      • 2013-11-16
      相关资源
      最近更新 更多