Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Rigidbody Controller move player forward in the direction of the camera

Discussion in 'Physics' started by Serge144, Aug 1, 2020.

  1. Serge144

    Serge144

    Joined:
    Oct 2, 2018
    Posts:
    7
    I'm trying to make my character move in such way that when I move my joystick in the upwards direction, the character should move forward in the direction the camera is facing, and not in character's own forward direction. I have created this script which is basically everything that you can see in this page: https://wiki.unity3d.com/index.php/RigidbodyFPSWalker with the small diference that i'm obtaining horizontal axis and vertical axis from joystick. However, as i mentioned, the player is moving in its own forward vector and not of camera's, how can i make this change? Thank you in advance.

    Code (CSharp):
    1. using UnityEngine;
    2. [RequireComponent(typeof(Rigidbody))]
    3. [RequireComponent(typeof(CapsuleCollider))]
    4. public class PlayerControllerRigid : MonoBehaviour
    5. {
    6.      public float speed = 10.0f;
    7.      public float gravity = 10.0f;
    8.      public float maxVelocityChange = 10.0f;
    9.      public bool canJump = true;
    10.      public float jumpHeight = 2.0f;
    11.      private bool grounded = false;
    12.      private new Rigidbody rigidbody;
    13.      public Joystick joystick;
    14.      void Awake()
    15.      {
    16.          rigidbody = GetComponent<Rigidbody>();
    17.          rigidbody.freezeRotation = true;
    18.          rigidbody.useGravity = false;
    19.      }
    20.      void FixedUpdate()
    21.      {
    22.          if (grounded)
    23.          {
    24.              float horizontal = joystick.Horizontal;
    25.              float vertical = joystick.Vertical;
    26.              // Calculate how fast we should be moving
    27.              Vector3 targetVelocity = new Vector3(horizontal, 0, vertical);
    28.              targetVelocity = transform.TransformDirection(targetVelocity);
    29.              targetVelocity *= speed;
    30.              // Apply a force that attempts to reach our target velocity
    31.              Vector3 velocity = rigidbody.velocity;
    32.              Vector3 velocityChange = (targetVelocity - velocity);
    33.              velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
    34.              velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
    35.              velocityChange.y = 0;
    36.              rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
    37.              // Jump
    38.              if (canJump && Input.GetButton("Jump"))
    39.              {
    40.                  rigidbody.velocity = new Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
    41.              }
    42.          }
    43.          // We apply gravity manually for more tuning control
    44.          rigidbody.AddForce(new Vector3(0, -gravity * rigidbody.mass, 0));
    45.          grounded = false;
    46.      }
    47.      void OnCollisionStay()
    48.      {
    49.          grounded = true;
    50.      }
    51.      float CalculateJumpVerticalSpeed()
    52.      {
    53.          // From the jump height and gravity we deduce the upwards speed
    54.          // for the character to reach at the apex.
    55.          return Mathf.Sqrt(2 * jumpHeight * gravity);
    56.      }
    57. }
     
unityunity