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. Dismiss Notice

Question Smooth player movement / move in direction of camera issue?

Discussion in 'Scripting' started by AerionXI, Feb 24, 2021.

  1. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    I'm so close to getting my player movement script to work. How can I make the movement of my script act like a car where it speeds up & slows down for both rotation of the player & movement of the player, & how can I move the player in the direction the camera is facing? :

    Code (CSharp):
    1.  
    2.    using UnityEngine;
    3.  
    4.    public class PlayerMovement : 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.    
    13.        public Rigidbody rb;
    14.  
    15.        void Awake () {
    16.            rb.freezeRotation = true;
    17.            rb.useGravity = false;
    18.        }
    19.    
    20.        void FixedUpdate () {
    21.            if (grounded) {
    22.                // Calculate how fast we should be moving
    23.                Vector3 targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    24.                targetVelocity = transform.TransformDirection(targetVelocity);
    25.                targetVelocity *= speed;
    26.    
    27.                // Apply a force that attempts to reach our target velocity
    28.                Vector3 velocity = rb.velocity;
    29.                Vector3 velocityChange = (targetVelocity - velocity);
    30.                velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
    31.                velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
    32.                velocityChange.y = 0;
    33.                rb.AddForce(velocityChange, ForceMode.VelocityChange);
    34.    
    35.                // Jump
    36.                if (canJump && Input.GetButton("Jump")) {
    37.                    rb.velocity = new Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
    38.                }
    39.            }
    40.    
    41.            // We apply gravity manually for more tuning control
    42.            rb.AddForce(new Vector3 (0, -gravity * rb.mass, 0));
    43.    
    44.            grounded = false;
    45.        }
    46.    
    47.        void OnCollisionStay () {
    48.            grounded = true;  
    49.        }
    50.    
    51.        float CalculateJumpVerticalSpeed () {
    52.            // From the jump height and gravity we deduce the upwards speed
    53.            // for the character to reach at the apex.
    54.            return Mathf.Sqrt(2 * jumpHeight * gravity);
    55.        }
    56.    }
    57.  
    Any help is ABSOLUTELY appreciated!

    Thanks in advance!
     
  2. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246
    Your current player movement does not rotate and that make
    Code (CSharp):
    1. targetVelocity = transform.TransformDirection(targetVelocity);
    useless

    to convert inputspace vector (targetVelocity) you need a Camera Transform:
    Code (CSharp):
    1. targetVelocity = myCameraTransform.TransformDirection(targetVelocity);
    note:Your current player movement is based on vector coordinate you should not messing with quaternion, as long as your player object does not rotate.

    edit: if your Camera muss be a child of some object, you have to use parent object TransformDirection
     
    Last edited: Feb 24, 2021
  3. simiel7

    simiel7

    Joined:
    Dec 18, 2020
    Posts:
    7
    Smooth Movement / Rotation
    For smooth movement, you should try out the a tween library / tweening / write your own tweening system, what needs for you. It takes a few hours to understand them and how they works, but I think it worth to check out.
    For code example, https://gist.github.com/Fonserbc/3d31a25e87fdaa541ddf

    In general, it is using mathematical functions and it will give you back a point from that function in a specific time.
    The time is between 0 and 1, where 0 is the starting point and 1 is the end point in the function.
    For example, to see the functions: https://sole.github.io/tween.js/examples/03_graphs.html

    It can be used for almost anything that need some kind of "animation", like Color, movement, rotation, and so on. You can pass the time parameter, like time (or the deltaTime divided by some value) to the tween function from the Update / FixedUpdate method and it will give you back a point and you can set this value back to whenever you want.

    If you try to search for more tween example for Unity or tween animations, you will find a bunch of examples and descriptions about how it works and how you can implement it in your game (or just check out the Unity Store).
     
  4. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    Thanks, but that doesn't answer my questions....
     
  5. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    Someone can help?
     
  6. Prazo20

    Prazo20

    Joined:
    Jul 9, 2022
    Posts:
    10
    you guys aint using the new input system