Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Help Needed] What can I do to smoothen out my character controller's movement? (ease in, ease out)

Discussion in 'Scripting' started by Raminul, Oct 14, 2015.

  1. Raminul

    Raminul

    Joined:
    Oct 11, 2015
    Posts:
    8
    So, basically, I'd like to be able to control the smoothness of how much the character slides before he fully stops when you're not holding in a movement key anymore. I'd also like to make it so my character gradually increases its movementspeed until its max speed. I've tried several ways of doing this with Vector3.SmoothDamp and Mathf.SmoothDamp. lots of errors pop up, so I guess my logic isn't in the correct place.

    Any help would be Appreciated.

    Here's the character's script. It fully works, it's very simple and clean, I guess. I'm hoping that someone could add some stuff to the script that would make the character do what I want it to do, if not that, just guide me in the right direction, please. Thank you so much.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerControllerScript : MonoBehaviour
    5. {
    6.     float MoveHorizontal;
    7.     float MoveVertical;
    8.     float MovementSpeed = 3f;
    9.  
    10. // Use this for initialization
    11.     void Start ()
    12.     {
    13.      
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update ()
    18.     {
    19.         CharacterController();
    20.     }
    21.  
    22.  
    23.     void CharacterController()
    24.     {
    25.  
    26.  
    27.  
    28.  
    29.         //Find the camera.
    30.         GameObject Target = GameObject.Find ("Camera");
    31.  
    32.  
    33.         //Initialize the movement Axes to the appropriate keys.
    34.          MoveHorizontal = Input.GetAxisRaw ("Horizontal") * Time.deltaTime * MovementSpeed;
    35.          MoveVertical = Input.GetAxisRaw ("Vertical") * Time.deltaTime * MovementSpeed;
    36.  
    37.  
    38.  
    39.         //Execute the movement.
    40.         Vector3 Movement = new Vector3(MoveHorizontal, 0, MoveVertical);
    41.  
    42.         transform.Translate (Movement);
    43.      
    44. transform.eulerAngles = new Vector3(transform.eulerAngles.z, Target.transform.eulerAngles.y, transform.eulerAngles.x);
    45.  
    46.     }
    47. }
    48.  
     
  2. Raminul

    Raminul

    Joined:
    Oct 11, 2015
    Posts:
    8
    Anyone? :)
     
  3. giano574

    giano574

    Joined:
    Nov 28, 2013
    Posts:
    76
    Use Input.GetAxis instead of Input.GetAxisRaw.

    Go to Edit > Project Settings > Input and change the Gravity and Sensitivity for each axis until you find something, you like.
    Gravity is how fast the value returns to 0, when letting go of the key.
    Sensitivity is how fast the value moves toward the target value (max speed) when pressing the key.
     
  4. Raminul

    Raminul

    Joined:
    Oct 11, 2015
    Posts:
    8

    Aaah, okay. Thank you :)

    Thing is though, I wanted to use Raw instead of just GetAxis because i didn't want it to automatically slide. I wanted full control over it in the actual script, create it from scratch. If that makes any sense. :p