unity3d 当我在Unity中移动时,玩家重力关闭

1yjd4xko  于 2023-04-21  发布在  其他
关注(0)|答案(1)|浏览(87)

我对unity还很陌生,我有两个脚本,一个是重力脚本,一个是玩家移动脚本。我使用重力脚本的原因是第三人称移动不支持使用启用了位置和旋转的刚体,所以我冻结了刚体的位置和旋转(在刚体中关闭重力)。我自己制作了重力脚本,但我遵循了一个关于玩家移动脚本的教程,因为我不知道如何制作第三人称移动,所以我不知道。I don’我真的不知道动作脚本中发生了什么。
移动脚本:

public class ThirdPersonMovement : MonoBehaviour
{
    public CharacterController controller;
    public Transform cam;
    public float speed = 6f;
    public float turnSmoothTime = 0.1f;
    float turnSmoothVelocity;

    void Update()
    {
        float horizontal = Input.GetAxisRaw("Horizontal");
        float vertical = Input.GetAxisRaw("Vertical");

        UnityEngine.Vector3 direction = new UnityEngine.Vector3(horizontal, 0f, vertical).normalized;

        if (direction.magnitude >= 0.1f)
        {
            float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
            float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);

            transform.rotation = UnityEngine.Quaternion.Euler(0f, angle, 0f);
            UnityEngine.Vector3 moveDir = UnityEngine.Quaternion.Euler(0f, targetAngle, 0f) * UnityEngine.Vector3.forward;
            controller.Move(moveDir.normalized * speed * Time.deltaTime);

        }

    }
}

重力脚本:

public class gravityScript : MonoBehaviour
{
    public float GravitySpeed = -0.03f;
    public bool GravityCheck = false;

    void OnCollisionEnter(Collision col)
    {
        if (col.gameObject.name == "Terrain0_0")
        {
            GravityCheck = true;
        }
    }

    void OnCollisionExit(Collision col)
    {
        GravityCheck = false;
    }

    void Update()
    {
        if (GravityCheck == false)
        {
            transform.Translate(0, GravitySpeed, 0);
        }
    }

}

谢谢你提前:)

wgeznvg7

wgeznvg71#

我不知道发生了什么,但是当我今天打开它的时候(发布后的第二天),它工作得很好。这可能只是一个在重启后得到修复的错误。

相关问题