让我的 Unity 角色像 Roblox 角色一样移动(不是相机)

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

我很难找到一种方法让我的角色移动并面向那个方向,然后旋转,曾经向前的东西是侧面的。

举个例子:我向右移动,我的角色现在面向右侧。现在右变向前。

我基本上想要一个复制 Roblox 动作的脚本。我已经找到了一个很好的相机跟随脚本,只是缺少动作..

我尝试了各种脚本,但找不到任何我喜欢的。有很多不同的网站,我想我应该注册一个帐户并在这里尝试!

c# unity-game-engine roblox
1个回答
0
投票

这是代码,这是由 ChatGpt 提供给我的,所以我不能因此而获得荣誉。

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float moveSpeed = 4.0f;
    public float rotationSpeed = 15.0f;

    private CharacterController characterController;

    private void Start()
    {
        characterController = GetComponent<CharacterController>();
    }

    private void Update()
    {
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");

        Vector3 inputDir = new Vector3(horizontalInput, 0, verticalInput);

        if (inputDir != Vector3.zero)
        {
            // Calculate the rotation to face the movement direction.
            Quaternion toRotation = Quaternion.LookRotation(inputDir, Vector3.up);

            // Apply the rotation smoothly using Slerp.
            transform.rotation = Quaternion.Slerp(transform.rotation, toRotation, rotationSpeed * Time.deltaTime);

            // Translate the character in the forward direction.
            characterController.Move(transform.forward * moveSpeed * Time.deltaTime);
        }
    }
}

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