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

Diagonal movement faster speed help plz

Discussion in 'Cache Server' started by borrachasate, Jul 7, 2020.

  1. borrachasate

    borrachasate

    Joined:
    Mar 29, 2020
    Posts:
    9
    I know the function VectorLength but I dont know where to apply it in this code, can someone help me?
    Code (CSharp):
    1.  
    2. public class CC : MonoBehaviour
    3. {
    4.  
    5.     public float walkSpeed = 6.0f;
    6.     public float runSpeed = 12.0f;
    7.     public float jumpSpeed = 8.0f;
    8.     public float gravity = 20.0f;
    9.  
    10.     public CharacterController cc;
    11.     private Vector3 move = Vector3.zero;
    12.  
    13.     public Camera cam;
    14.     public float mouseH = 3.0f;
    15.     public float mouseV = 2.0f;
    16.     public float minRot = -65.0f;
    17.     public float maxRot = 60.0f;
    18.  
    19.     float h_mouse, v_mouse;
    20.     // Start is called before the first frame update
    21.     void Start()
    22.     {
    23.         Cursor.lockState = CursorLockMode.Locked;
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.         h_mouse = mouseH * Input.GetAxis("Mouse X");
    30.         v_mouse += mouseV * Input.GetAxis("Mouse Y");
    31.  
    32.         v_mouse = Mathf.Clamp(v_mouse, minRot, maxRot);
    33.  
    34.         cam.transform.localEulerAngles = new Vector3(-v_mouse, 0, 0);
    35.         transform.Rotate(0, h_mouse, 0);
    36.      
    37.  
    38.         if (cc.isGrounded)
    39.         {
    40.             move = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
    41.  
    42.             if (Input.GetKey(KeyCode.LeftShift))
    43.             {
    44.                 move = transform.TransformDirection(move) * runSpeed;
    45.             } else
    46.             {
    47.                 move = transform.TransformDirection(move) * walkSpeed;
    48.             }
    49.  
    50.             if (Input.GetKey(KeyCode.Space))
    51.             {
    52.                 move.y = jumpSpeed;
    53.             }
    54.             }
    55.         move.y -= gravity * Time.deltaTime;
    56.  
    57.  
    58.         cc.Move(move * Time.deltaTime);
    59.     }
    60. }
    61.  
     
  2. Skrimel

    Skrimel

    Joined:
    Jul 30, 2017
    Posts:
    4
    Row 40:
    You get direction based on user input. When no keys down you get length of 0 for your direction vector. When one key down you get length of one - sqrt(1). Two keys down - you get lenght of sqrt(2)
    You need to Clamp length of your vector to one and in you cases you will get such a result:
    No keys down - length is 0
    Any key down - length is 1

    Docs for solution:
    https://docs.unity3d.com/ScriptReference/Vector3.html
    https://docs.unity3d.com/ScriptReference/Vector3.ClampMagnitude.html

    Solution:
    Change code on row 40 to this:

    move = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
    move = Vector3.ClampMagnitude(move);