Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Can't get Rigidbody to properly follow camera direction

Discussion in 'Scripting' started by Karraticus, May 27, 2022.

  1. Karraticus

    Karraticus

    Joined:
    May 24, 2022
    Posts:
    3
    Hi all,

    I am trying to build my rigidbody controller for a space-based mech FPS. I am running into two specific issues. Difficulties getting both the model to pivot towards the direction the camera is facing as well as getting my character to fluidly move in the same direction. Right now I can sort of get my character moving in the rough direction the camera is facing, but trying to change my camera facing drastically doesn't make pronounced difference in heading.

    For example, I can't fly up or down or left to right at straight angles, but I can fly forward and back on a flat plane if I don't move the mouse. I think it has something to do with how the controller is trying to calculate my camera position, but I can't figure out how to make it more precise while keeping its momentum. I'm guessing I will need more constraints on the camera as well.

    Here's my code for movement and my current attempt at transforming position:

    Code (CSharp):
    1.   void Start()
    2.  
    3.     {
    4.  
    5.         rb = GetComponent<Rigidbody>();
    6.         Cursor.lockState = CursorLockMode.Locked;
    7.  
    8.     }
    9.     void FixedUpdate()
    10.  
    11.     {
    12.         HandleMovement();
    13.     }
    14.  
    15.     void HandleMovement()
    16.  
    17.     {
    18.        // Turn Character to Face Camera Direction - This does not really work...
    19.        characterTurn.x += Input.GetAxis("Horizontal");
    20.        characterTurn.y += Input.GetAxis("Vertical");
    21.        transform.localRotation = Quaternion.Euler(characterTurn.y, characterTurn.x, 0);
    22.  
    23.  
    24.  
    25.     // Movement via Mouse Transform Camera Direction
    26.         Vector3 mouseInput = new Vector3(Input.GetAxis("Horizontal"),0, Input.GetAxis("Vertical"));
    27.         var cameraLookingDirection = Camera.main.transform.rotation * Vector3.forward;
    28.         cameraLookingDirection = new Vector3(cameraLookingDirection.x, cameraLookingDirection.y, cameraLookingDirection.z).normalized;
    29.         rb.AddRelativeForce(Quaternion.FromToRotation(Vector3.forward, cameraLookingDirection) * mouseInput * thrust * Time.deltaTime, ForceMode.Impulse);
    30.  
    31.         // Thrust
    32.         if (thrust1D > 0.1f || thrust1D < -0.1f)
    33.         {
    34.             float currentThrust = thrust;
    35.  
    36.             rb.AddRelativeForce(Vector3.forward * thrust1D * currentThrust * Time.deltaTime);
    37.             glide *= thrust;
    38.         }
    39.         else
    40.         {
    41.             rb.AddRelativeForce(Vector3.forward * glide * Time.deltaTime);
    42.             glide *= thrustGlideReduction;
    43.         }
    44.  
    45.        
    46.  
    47.     }
    48.  
    49.     #region Input Methods
    50.     public void OnThrust(InputAction.CallbackContext context)
    51.     {
    52.         thrust1D = context.ReadValue<float>();
    53.  
    54.     }
    55.  
    56.     public void OnStrafe(InputAction.CallbackContext context)
    57.     {
    58.         strafe1D = context.ReadValue<float>();
    59.  
    60.     }
    61.  
    62.     public void OnUpDown(InputAction.CallbackContext context)
    63.     {
    64.         upDown1D = context.ReadValue<float>();
    65.  
    66.     }
    67.  
    68.     public void OnRoll(InputAction.CallbackContext context)
    69.     {
    70.         roll1D = context.ReadValue<float>();
    71.  
    72.     }
    73.  
    74.     public void OnBoost(InputAction.CallbackContext context)
    75.     {
    76.         boost1D = context.ReadValue<float>();
    77.  
    78.     }
    79.  
    80.     public void OnPitchYaw(InputAction.CallbackContext context)
    81.     {
    82.         pitchYaw = context.ReadValue<Vector2>();
    83.  
    84.  
    85.     }
    86.     #endregion
    87.  
    88. }
    89.  

    Please note I have not added all the input behavior I've accounted for yet.

    The end goal should be that I can move in the direction I face with thrust input, then rotate to a desired angle to engage my targets with that input continuing until I add additional thrust in a new direction. The character should always face/pivot in the direction he is aiming, but it should not affect his movement unless thrust is being applied.

    Thanks
     
  2. Karraticus

    Karraticus

    Joined:
    May 24, 2022
    Posts:
    3
    After tinkering with it some more I've made some progress. Now my character will move and rotate in the direction my camera faces when I press a W and S key. Since it's in space I want my character to also rotate to face down in order to shoot enemies below them while flying in a specific direction. The problem I face now is that my character will not also rotate their body (and thus force direction) to accommodate the change in direction on the x axis via the camera. If I look right or left and press A or D my character shifts to face the camera but then moves away from my view direction as if I pressed S. I would have expected the character to rotate towards the camera and then strafe sideways.


    Code (CSharp):
    1.  
    2. void Start()
    3.  
    4.     {
    5.  
    6.         rb = GetComponent<Rigidbody>();
    7.         rb.freezeRotation = true;
    8.         Cursor.lockState = CursorLockMode.Locked;
    9.  
    10.     }
    11.     void FixedUpdate()
    12.  
    13. // Movement via Mouse Transform Camera Direction
    14.         Vector3 mouseInput = new Vector3(Input.GetAxis("Horizontal"),0, Input.GetAxis("Vertical"));
    15.         mouseInput.Normalize();
    16.         var cameraLookingDirection = Camera.main.transform.rotation * Vector3.forward;
    17.         cameraLookingDirection = new Vector3(cameraLookingDirection.x, cameraLookingDirection.y, cameraLookingDirection.z).normalized;
    18.         rb.AddRelativeForce(Quaternion.FromToRotation((Vector3.forward), cameraLookingDirection) * mouseInput * thrust * Time.deltaTime, ForceMode.Impulse);
    19.  
    20.  
    21.         // Turn Character to Face Camera Direction
    22.  
    23.         if (mouseInput != Vector3.zero)
    24.  
    25.         {
    26.             Quaternion toRotation = Quaternion.LookRotation(cameraLookingDirection, Vector3.up);
    27.             transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, rotationSpeed * Time.deltaTime);
    28.         }
    29.  
    30.         // Thrust
    31.         if (thrust1D > 0.1f || thrust1D < -0.1f)
    32.         {
    33.             float currentThrust = thrust;
    34.  
    35.             rb.AddRelativeForce(Vector3.forward.normalized * thrust1D * currentThrust * Time.deltaTime);
    36.             glide *= thrust;
    37.         }
    38.         else
    39.         {
    40.             rb.AddRelativeForce(Vector3.forward.normalized * glide * Time.deltaTime);
    41.             glide *= thrustGlideReduction;
    42.         }
    43.  
    44.  
    45.  
    46.     }
     
    Last edited: May 28, 2022