unity3d 统一游戏物体位置总是回到0和中心

ct2axkht  于 2023-01-31  发布在  其他
关注(0)|答案(1)|浏览(207)

大家好,我开始学习Unity游戏引擎和C#,我一步一步地学习视频课程,但当我按下播放按钮时,它会立即居中游戏对象,当我试图移动它时,它会移动,但总是返回中心(位置0,0,0)

public class PlayerMovment : MonoBehaviour
{
    public float speed = 5f;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");

        Vector2 pos = transform.position;

        pos.x = h * speed * Time.deltaTime;
        pos.y = v * speed * Time.deltaTime;

        transform.position = pos;
    }
}
6l7fqoea

6l7fqoea1#

您需要将您的移动“添加”到该位置,而不是覆盖它。请尝试以下操作:

Vector2 pos = transform.position;

pos.x += h * speed * Time.deltaTime;
pos.y += v * speed * Time.deltaTime;

transform.position = pos;

相关问题