Question3r
Question3r

Reputation: 3832

Unity converting Character Controller code to Rigidbody code

I got a movement relative to the camera. Like Super Mario 64 etc. First i made it with a CharacterController but I want to have the default collision detection so I need to use a collider with a Rigidbody.

My code looks this:

    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;
    }
}

So it seems obvious to replace all the CharacterController variables with a Rigidbody key word. But when it comes to

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

I don't know what to replace there. When I got no CC but a Collider and a Rigidbody i drop through the ground. This may happen because of the code..

Could someone help me out?

Upvotes: 0

Views: 1832

Answers (1)

Anas iqbal
Anas iqbal

Reputation: 1044

Remove CharacterController component from your player and attach a Rigidbody component. Then use

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

I will suggest that you create a reference of you attached rigidbody and use it instead of GetComponent.

Upvotes: 1

Related Questions