旋转统一

问题描述 投票:0回答:1

所以我有这个脚本,它通过使用axess来移动玩家,我不仅想移动,而且还想沿axess的方向旋转玩家

这是我用于玩家移动的代码,有人知道我该如何制作吗

 using UnityEngine;
public class moving : MonoBehaviour
{
    






    public float moveSpeed = 5f;
    private Rigidbody r;

    public GameObject camera;

    public string checks1;


    public float Force = 5f;





    private void Start()
    {
        r = GetComponent<Rigidbody>();
    }
    void Update()
    {
        if (Input.GetKey(KeyCode.Space))
        {
            checks1 = "Y";
        }

    }
    private void FixedUpdate()
    {

        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        r.velocity = new Vector3(h * moveSpeed, r.velocity.y, v * moveSpeed);
        if (checks1 == "Y")
        {
            r.velocity = new Vector3(r.velocity.x, Force, r.velocity.y);
            checks1 = "s";
        }
    }
}
c# unityscript
1个回答
0
投票

假设这是一个 2D 游戏,并且您希望对象指向移动方向,您应该能够执行类似的操作

if(r.velocity.magnitude > 0){
    r.rotation = Quaternion.LookAt(r.velocity, Vector3.up);
}

LookAt
方法应该产生一个旋转,使 Z 轴与物体的速度对齐。这假设速度永远不会直接向上/向下,如果可能的话,您要么需要添加检查,要么使用另一个保证不与速度平行的“向上”方向。

© www.soinside.com 2019 - 2024. All rights reserved.