Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Resolved My Transform goes faster diagonal than going straight

Discussion in 'Getting Started' started by beovnanno, Apr 1, 2024.

  1. beovnanno

    beovnanno

    Joined:
    Jan 20, 2024
    Posts:
    5
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3.  
    4. public class PlayerMovement : MonoBehaviour
    5. {
    6.     bool grounded;
    7.     Vector3 moveDir, sideDir;
    8.  
    9.     public float desiredHeightAboveGround;
    10.     private Vector3 previousPosition;
    11.  
    12.     void Start()
    13.     {
    14.        
    15.        
    16.     }
    17.  
    18.     private void Gravity()
    19.     {
    20.         transform.Translate(0, -.2f, 0);
    21.     }
    22.  
    23.     private void isGrounded()
    24.     {
    25.         if (Physics.Raycast(transform.position, Vector3.down, 1.7f))
    26.         {
    27.             grounded = true;
    28.         }
    29.         else
    30.         {
    31.             grounded = false;
    32.         }
    33.     }
    34.  
    35.     private void maintainHeight()
    36.     {
    37.         RaycastHit hit;
    38.         if(Physics.Raycast(transform.position, Vector3.down, out hit, Mathf.Infinity))
    39.         {
    40.             float currentHeightAboveGround = transform.position.y - hit.point.y;
    41.             float heightDifference = desiredHeightAboveGround - currentHeightAboveGround;
    42.             transform.Translate(0, heightDifference, 0);
    43.         }
    44.     }
    45.  
    46.     public float GetVelocity()
    47.     {
    48.      
    49.         Vector3 velocity = (transform.position - previousPosition) / Time.deltaTime;
    50.  
    51.  
    52.         return velocity.magnitude;
    53.     }
    54.  
    55.     void Update()
    56.     {
    57.  
    58.         previousPosition = transform.position;
    59.         isGrounded();
    60.  
    61.         if (grounded == false)
    62.         {
    63.             Gravity();
    64.         }
    65.         else
    66.         {
    67.             maintainHeight();
    68.         }
    69.  
    70.  
    71.         moveDir = transform.forward;
    72.         sideDir = transform.right;
    73.  
    74.         if (Input.GetKey(KeyCode.W))
    75.         {
    76.             transform.Translate(moveDir * .02f);
    77.         }
    78.  
    79.         if (Input.GetKey(KeyCode.S))
    80.         {
    81.             transform.Translate(-moveDir * .02f);
    82.         }
    83.  
    84.         if (Input.GetKey(KeyCode.D))
    85.         {
    86.             transform.Translate(sideDir * .02f);
    87.         }
    88.  
    89.         if (Input.GetKey(KeyCode.A))
    90.         {
    91.             transform.Translate(-sideDir * .02f);
    92.         }
    93.  
    94.        
    95.  
    96.         print(Mathf.Round(GetVelocity()));
    97.  
    98.     }
    99. }
    100.  
    I switched from using rigidbodies to just manipulating the transform directly myself. It's a bit of a hassle getting the transfrom to recognize it's on ground, but I managed to do it. One thing I noticed however was that the transform goes a bit faster when traveling diagonal than straight. I'm a little embarrassed to admit it, but I went to ChatGPT for help, but its code didn't help. Any suggestions on what I can do to fix this?
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    1,084
    Code (CSharp):
    1. using UnityEngine;
    2. public class PlayerMovement : MonoBehaviour
    3. {
    4.     void Update()
    5.     {
    6.         if (!Physics.Raycast(transform.position, Vector3.down,1.2f)) // no ground below?
    7.             transform.Translate(0,-0.2f,0); // move downwards
    8.         transform.Translate(new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical")).normalized*0.02f);  // normalizing the movement vector prevents faster movement in diagonal directions
    9.     }
    10. }
     
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,386
    If you have a square, the distance from the center to a corner is father than the distance to the middle of one of the edges. Whereas with a circle, the distance from the center to any point along the circumference is always the same.

    Right now you're making a square when you want to make a circle. This is easily done by making a Vector2/3 out of your input and normalizing it, so its magnitude is exactly 1.

    For example:
    Code (CSharp):
    1. float xInput = Input.GetAxis("Horizontal");
    2. float yInput = Input.GetAxis("Vertical");
    3. Vector3 input = new Vector3(xInput, 0, yInput).normalized; // get the normalized result
     
    beovnanno and MelvMay like this.