Search Unity

Third Person Camera AddForce problem

Discussion in 'Scripting' started by masterjason322, Sep 22, 2019.

  1. masterjason322

    masterjason322

    Joined:
    Aug 27, 2019
    Posts:
    5
    Hello, I'm new to Unity and was following a third person camera tutorial (this one:
    ) and I am using rb.addforce to move my character. The movement works, but it is not relative to the camera and if you rotate the camera to the left the movement feels weird. Here is my code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class control : MonoBehaviour
    5. {
    6.  
    7.     public float speed = 5f;
    8.     public float sprintSpeed = 10f;
    9.     Vector3 position = new Vector3(0, 1, 0);
    10.     public Transform _camera;
    11.     public bool isMoving = false;
    12.  
    13.     private Rigidbody rb;
    14.  
    15.     void Start()
    16.     {
    17.         rb = GetComponent<Rigidbody>();
    18.     }
    19.    
    20.     void FixedUpdate()
    21.     {
    22.         Vector3 cameraDirection = transform.position - _camera.position;
    23.         bool sprint = Input.GetKey("left shift");
    24.         float verticalMove = Input.GetAxisRaw("Vertical");
    25.         float horizontalMove = Input.GetAxisRaw("Horizontal");
    26.  
    27.         if (verticalMove != 0 | horizontalMove != 0)
    28.         {
    29.             isMoving = true;
    30.         }
    31.         else
    32.         {
    33.             isMoving = false;
    34.         }
    35.  
    36.         if (sprint == false)
    37.         {
    38.             speed = 5f;
    39.         }
    40.  
    41.         if (sprint == true)
    42.         {
    43.             speed = sprintSpeed;
    44.         }
    45.  
    46.         Vector3 movement = new Vector3(horizontalMove, 0.0f, verticalMove);
    47.  
    48.         if (isMoving == true)
    49.         {
    50.             rb.AddForce(movement * speed);
    51.         }
    52.        
    53.     }
    54. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Around line 47 (above) you need to create a Quaternion that is your player's heading, something like this:

    Code (csharp):
    1. Quaternion facing = Quaternion.Euler( 0, transform.eulerAngles.y, 0);
    Now you need to "bend" the inputted velocity that way:

    Code (csharp):
    1. movement = facing * movement;
    That should true up your motion and make it local relative.
     
  3. masterjason322

    masterjason322

    Joined:
    Aug 27, 2019
    Posts:
    5
    Alright, so if done correctly it should be done like this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class control : MonoBehaviour
    5. {
    6.  
    7.     public float speed = 5f;
    8.     public float sprintSpeed = 10f;
    9.     Vector3 position = new Vector3(0, 1, 0);
    10.     public Transform _camera;
    11.     public bool isMoving = false;
    12.  
    13.     private Rigidbody rb;
    14.  
    15.     void Start()
    16.     {
    17.         rb = GetComponent<Rigidbody>();
    18.     }
    19.  
    20.     void FixedUpdate()
    21.     {
    22.         Vector3 cameraDirection = transform.position - _camera.position;
    23.         bool sprint = Input.GetKey("left shift");
    24.         float verticalMove = Input.GetAxisRaw("Vertical");
    25.         float horizontalMove = Input.GetAxisRaw("Horizontal");
    26.  
    27.         if (verticalMove != 0 | horizontalMove != 0)
    28.         {
    29.             isMoving = true;
    30.         }
    31.         else
    32.         {
    33.             isMoving = false;
    34.         }
    35.  
    36.         if (sprint == false)
    37.         {
    38.             speed = 5f;
    39.         }
    40.  
    41.         if (sprint == true)
    42.         {
    43.             speed = sprintSpeed;
    44.         }
    45.  
    46.         Vector3 movement = new Vector3(horizontalMove, 0.0f, verticalMove);
    47.         Quaternion facing = Quaternion.Euler(0, transform.eulerAngles.y, 0);
    48.         movement = facing * movement;
    49.  
    50.         if (isMoving == true)
    51.         {
    52.             rb.AddForce(movement * speed);
    53.         }
    54.      
    55.     }
    56. }
    However, now moving just feels stiff and my character ends up rolling back to the same spot. Is there anything wrong?
     
    Last edited: Sep 23, 2019