Search Unity

Question Is it good idea to use rigidbody for my third person character controller?

Discussion in 'Physics' started by Mandalore_the_Creator, Jun 21, 2022.

  1. Mandalore_the_Creator

    Mandalore_the_Creator

    Joined:
    Feb 5, 2021
    Posts:
    10
    Code (CSharp):
    1. public class PlayerMovementAltered : MonoBehaviour
    2. {
    3.     public Transform cam;
    4.  
    5.     Vector3 m_Input;
    6.  
    7.     public float turnSmoothTime = 0.1f;
    8.     float turnSmoothVelocity;
    9.  
    10.     public float horizontal;
    11.     public float vertical;
    12.  
    13.     Rigidbody m_Rigidbody;
    14.     public float m_Speed = 6f;
    15.  
    16.     public float jumpForce;
    17.     private bool isGrounded;
    18.  
    19.     public Animator anim;
    20.     void Start()
    21.     {
    22.         m_Rigidbody = GetComponent<Rigidbody>();
    23.         anim = GetComponentInChildren<Animator>();
    24.     }
    25.  
    26.     void FixedUpdate()
    27.     {
    28.         horizontal = Input.GetAxisRaw("Horizontal");
    29.         vertical = Input.GetAxisRaw("Vertical");
    30.         m_Input = new Vector3(horizontal, 0, vertical).normalized;
    31.         anim.SetFloat("Speed", 0, 0.1f, Time.deltaTime);
    32.         if (m_Input.magnitude >= 0.1f)
    33.         {
    34.             Move();
    35.         }
    36.         if (Input.GetAxis("Jump") > 0)
    37.         {
    38.             if (isGrounded)
    39.             {
    40.                 m_Rigidbody.AddForce(transform.up * jumpForce);
    41.             }
    42.         }
    43.     }
    44.     private void Move()
    45.     {
    46.         anim.SetFloat("Speed", 2 * m_Input.magnitude, 0.1f, Time.deltaTime);
    47.         float targetAngle = Mathf.Atan2(m_Input.x, m_Input.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
    48.         float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
    49.         transform.rotation = Quaternion.Euler(0f, angle, 0f);
    50.  
    51.         Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
    52.         m_Rigidbody.MovePosition(transform.position + moveDir.normalized * Time.deltaTime * m_Input.magnitude * m_Speed);
    53.     }
    54.  
    55.     void OnCollisionEnter(Collision collision)
    56.     {
    57.         if (collision.gameObject.tag == ("Ground"))
    58.         {
    59.             isGrounded = true;
    60.         }
    61.     }
    62.     // This function is a callback for when the collider is no longer in contact with a previously collided object.
    63.     void OnCollisionExit(Collision collision)
    64.     {
    65.         if (collision.gameObject.tag == ("Ground"))
    66.         {
    67.             isGrounded = false;
    68.         }
    69.     }
    70. }
    71.  
    this is my character controller script using rigidbody
     
    Last edited: Jun 21, 2022
  2. r31o

    r31o

    Joined:
    Jul 29, 2021
    Posts:
    460
    You have already made it.
    Just use it, and if you think you need more control, switch to character controller
     
  3. Mandalore_the_Creator

    Mandalore_the_Creator

    Joined:
    Feb 5, 2021
    Posts:
    10
    thanks, but what do you mean by switching to character controller?
     
  4. r31o

    r31o

    Joined:
    Jul 29, 2021
    Posts:
    460
    You are currently adding forces to a rigidbody, but there is a CharacterControler component, which gives you all the control on the movement, at cost of making everything yourself.
    You just call Move() with the movement vector and it moves.
    It can also automatically walk through stairs, and it has a bool to check if it is grounded.