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

[Solved]Problem with diagonal speed movement | smooth/delay mouse movement

Discussion in 'Scripting' started by Dr. Argento, Apr 7, 2016.

  1. Dr. Argento

    Dr. Argento

    Joined:
    Jan 29, 2015
    Posts:
    23
    Hi there, i have two problems, first, i managed to find out how can move my character controller, but when i press for example w+a or w+d or s+a or s+d (diagonal movement) the speed is incremented, so my question is how can edit my script to solve this problem.

    Code (CSharp):
    1. public class CharacterBehavior : MonoBehaviour
    2. {
    3.     public float movementspeed = 5.0f;
    4.     public float mouseSensitivity = 5.0f;
    5.     public float upDownRange = 60.0f;
    6.     public HeadbobBehavior headBobBehavior;
    7.  
    8.     private Vector3 lastPos;
    9.     private bool imRunning;
    10.     private float verticalVelocity = 0;
    11.     private float verticalRotation = 0;
    12.     private CharacterController characterController;
    13.  
    14.     void Start ()
    15.     {
    16.         lastPos = transform.position;
    17.         Cursor.visible = false;
    18.         Cursor.lockState = CursorLockMode.Locked;
    19.         characterController = GetComponent<CharacterController>();
    20.     }
    21.    
    22.     void Update ()
    23.     {
    24.         // Rotation.
    25.         float rotLeftRight = Input.GetAxis("Mouse X") * mouseSensitivity;
    26.         transform.Rotate (new Vector3(0, rotLeftRight, 0));
    27.         verticalRotation -= Input.GetAxis("Mouse Y") * mouseSensitivity;
    28.         verticalRotation = Mathf.Clamp(verticalRotation, -upDownRange, upDownRange);
    29.         Camera.main.transform.localRotation = Quaternion.Euler (verticalRotation, 0, 0);
    30.  
    31.         // Movement.
    32.         float forwardSpeed = Input.GetAxis("Vertical") * movementspeed;
    33.         float sideSpeed = Input.GetAxis("Horizontal") * movementspeed;
    34.         verticalVelocity += Physics.gravity.y * Time.deltaTime;
    35.         Vector3 speed = new Vector3(sideSpeed, verticalVelocity, forwardSpeed);
    36.         speed = transform.rotation * speed;
    37.         characterController.Move(speed * Time.deltaTime);
    38.  
    39.         Vector3 displacement = transform.position - lastPos;
    40.         lastPos = transform.position;
    41.  
    42.         // Running ?.      
    43.         if (displacement.magnitude > 0.001)
    44.         {
    45.             if (Input.GetKey(KeyCode.LeftShift) && !imRunning)
    46.             {
    47.                 imRunning = true;
    48.                 movementspeed = 2.5f;
    49.                 headBobBehavior.playerState = HeadbobBehavior.PlayerState.Running;
    50.             }
    51.             else
    52.             {
    53.                 imRunning = false;
    54.                 movementspeed = 1f;
    55.                 headBobBehavior.playerState = HeadbobBehavior.PlayerState.Walking;
    56.             }
    57.         }
    58.         else
    59.         {
    60.             imRunning = false;
    61.             movementspeed = 1f;
    62.             headBobBehavior.playerState = HeadbobBehavior.PlayerState.Idle;
    63.         }
    64.     }
    65. }
    And second, i wanna apply smooth/delay when i move my mouse, i don¡t know how to do that, any help will be grateful!.
     
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,077
    When you're moving left/right you might move by 1 unit horizontally and when moving up/down you might move by 1 unit vertically. However, when you move right and up at the same time you actually move by 1.4 units. If you think of it as a triangle then the movement is the length of the hypotenuse which is SquareRoot(1 squared + 1 squared). That's why it appears faster.

    To get around that you need to normalise your movement vector first. e.g.

    Code (CSharp):
    1.  
    2.     Vector2 DirVec = new Vector2( Input.GetAxis("Vertical") * 1.0f , Input.GetAxis("Horizontal") * 1.0f );
    3.     DirVec.Normalize();
    4.     DirVec *= movementspeed;
    5.     float forwardSpeed = DirVec.x;
    6.     float sideSpeed = DirVec.y;
    There's lots of ways of smoothing movement - a simple one is to hold 2 variables, a 'current' value and a 'destination' value. Each frame you would update the destination value with the mouseX or mouseY value and then use something like Mathf.SmoothDamp to move the current value towards the destination one. You would then use the current value to do your rotation.
     
    Dr. Argento likes this.
  3. Dr. Argento

    Dr. Argento

    Joined:
    Jan 29, 2015
    Posts:
    23
    I fix the diagonal movement (thanks to you!), but i still stuck in the mouse part, i can' figured out how can adapt smooth / delay in this code:

    Code (CSharp):
    1. float rotLeftRight = Input.GetAxis("Mouse X") * mouseSensitivity;
    2.         transform.Rotate (new Vector3(0, rotLeftRight, 0));
    3.         verticalRotation -= Input.GetAxis("Mouse Y") * mouseSensitivity;
    4.         verticalRotation = Mathf.Clamp(verticalRotation, -upDownRange, upDownRange);
    5.         Camera.main.transform.localRotation = Quaternion.Euler (verticalRotation, 0, 0);
     
  4. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,077
    Instead of using Input.GetAxis directly you would store it first and then tend the current value towards it. This code does something similar - you can change the 0.1 value to change how quickly it smooths the values:

    Code (CSharp):
    1.         float currentMouseX;
    2.         float destinationMouseX;
    3.         float velocityMouseX;
    4.  
    5.         float currentMouseY;
    6.         float destinationMouseY;
    7.         float velocityMouseY;
    8.        
    9.        
    10.         void Test()
    11.         {
    12.             destinationMouseX = Input.GetAxis("Mouse X");
    13.             destinationMouseY = Input.GetAxis("Mouse Y");
    14.            
    15.             currentMouseX = Mathf.SmoothDamp( currentMouseX , destinationMouseX , ref velocityMouseX , 0.1f );
    16.             currentMouseY = Mathf.SmoothDamp( currentMouseY , destinationMouseY , ref velocityMouseY , 0.1f );
    17.            
    18.             float rotLeftRight = currentMouseX * mouseSensitivity;
    19.             transform.Rotate (new Vector3(0, rotLeftRight, 0));
    20.             verticalRotation -= currentMouseY * mouseSensitivity;
    21.             verticalRotation = Mathf.Clamp(verticalRotation, -upDownRange, upDownRange);
    22.             Camera.main.transform.localRotation = Quaternion.Euler (verticalRotation, 0, 0);
    23.         }
    24.  
     
    Dr. Argento likes this.
  5. Dr. Argento

    Dr. Argento

    Joined:
    Jan 29, 2015
    Posts:
    23
    Wow thanks you, thats works like a charm, really appreciate your help, thanks again!.