【问题标题】:Unity converting Character Controller code to Rigidbody codeUnity 将角色控制器代码转换为刚体代码
【发布时间】:2017-05-22 02:29:52
【问题描述】:

我得到了一个相对于相机的运动。像超级马里奥 64 等。首先我使用 CharacterController 制作它,但我想要默认的碰撞检测,所以我需要使用带有 Rigidbody 的碰撞器。

我的代码如下所示:

    public class PlayerMovementController : PlayerCommonController
{
    PlayerMovementData data; // data class
    PlayerMovementView view; // animation, etc. ... class

    float turnSmoothVelocity;
    float speedSmoothVelocity;

    private void Start()
    {
        data = new PlayerMovementData();
        view = new PlayerMovementView();
    }

    private void Update()
    {
        Vector2 inputDirection = (new Vector2(data.InputHorizontal, data.InputVertical)).normalized; // get the inputs

        if (Input.GetButtonDown("Jump"))
        {
            if (data.PlayerCharacterController.isGrounded) // player is on ground?
                data.VelocityY = Mathf.Sqrt(-2 * data.PlayerGravity * data.JumpPower);
        }

        if (inputDirection != Vector2.zero) // Rotate the player
        {
            transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(
                transform.eulerAngles.y, 
                Mathf.Atan2(inputDirection.x, inputDirection.y) * Mathf.Rad2Deg + data.CameraTransform.eulerAngles.y,
                ref turnSmoothVelocity,
                GetModifiedSmoothTime(data.TurnSmoothTime));
        }

        data.CurrentMovementSpeed = Mathf.SmoothDamp( /* Set the movementspeed */
            data.CurrentMovementSpeed, 
            data.MovementSpeed * inputDirection.magnitude,
            ref speedSmoothVelocity,
            GetModifiedSmoothTime(data.SpeedSmoothTime));

        data.VelocityY += data.PlayerGravity * Time.deltaTime; // vertical velocity

        Vector3 velocity = transform.forward * data.CurrentMovementSpeed + Vector3.up * data.VelocityY; // set the players velocity

        data.PlayerCharacterController.Move(velocity * Time.deltaTime); // Move the player

        data.CurrentMovementSpeed = (new Vector2(data.PlayerCharacterController.velocity.x, data.PlayerCharacterController.velocity.z)).magnitude; // Calc movementspeed

        if (data.PlayerCharacterController.isGrounded) // Set the vertical vel. to 0 when grounded
            data.VelocityY = 0;
    }

    float GetModifiedSmoothTime(float smoothTime) // Handle the movement while in air
    {
        if (data.PlayerCharacterController.isGrounded)
            return smoothTime;

        if (data.AirControlPercentage == 0)
            return float.MaxValue;

        return smoothTime / data.AirControlPercentage;
    }
}

因此,将所有 CharacterController 变量替换为 Rigidbody 关键字似乎是显而易见的。但是说到

data.PlayerCharacterController.Move(velocity * Time.deltaTime);

我不知道在那里替换什么。当我没有CC,只有一个碰撞器和一个刚体时,我从地上掉了下来。这可能是因为代码..

有人可以帮我吗?

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    从播放器中移除 CharacterController 组件并附加一个 Rigidbody 组件。然后使用

    GetComponent<Rigidbody>().velocity = velocity * Time.deltaTime;
    

    我建议您创建一个附加刚体的引用并使用它而不是GetComponent

    【讨论】:

    • 嗯,这几乎可以正常工作。玩家非常非常缓慢地倒下。而我不能动他。我当前的代码如下所示:hastebin.com/axexuwakup.cs 我非常相信我的 VelocityY 不正确。我的刚体使用重力,我也在那里计算。你知道如何解决这个问题吗?
    • 请注意,您现在处于物理世界中,因此,您必须调整您的速度值(接近现实)。是的,刚体有自己的重力,你不需要计算它。
    猜你喜欢
    • 1970-01-01
    • 2017-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    • 2018-10-13
    相关资源
    最近更新 更多