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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Character moves faster on the X axis, slower on the Z axis

Discussion in 'Scripting' started by danilo12, Jan 30, 2017.

  1. danilo12

    danilo12

    Joined:
    Jul 21, 2015
    Posts:
    64
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PlayerMovement : MonoBehaviour {
    7.     public float Speed;
    8.  
    9.     float h;
    10.     float v;
    11.     Rigidbody rigid;
    12.     public Vector3 newPos;
    13.     bool isWalking;
    14.     Animator anim;
    15.     int floorMask;
    16.  
    17.  
    18.     void Start () {
    19.         rigid = GetComponent<Rigidbody> ();  
    20.         anim = GetComponent<Animator> ();
    21.         floorMask = LayerMask.GetMask("Floor");
    22.     }
    23.    
    24.  
    25.     void FixedUpdate () {
    26.        
    27.         v = Input.GetAxisRaw("Vertical");
    28.         h = Input.GetAxisRaw("Horizontal");
    29.  
    30.         Move (h,v);
    31.         Animate ();
    32.         Turn ();
    33.     }
    34.     void Move(float h, float v){
    35.         newPos = new Vector3 (h,0f,v);
    36.         newPos = newPos * Time.deltaTime * Speed;
    37.         rigid.MovePosition (transform.position + newPos);
    38.     }
    39.     void Animate(){
    40.         isWalking = h != 0f || v!= 0f;
    41.         anim.SetBool ("isWalking", isWalking);
    42.     }
    43.     void Turn(){
    44.         Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    45.         RaycastHit floorHit;
    46.  
    47.         if(Physics.Raycast(camRay,out floorHit,100f,floorMask)){
    48.             Vector3 playerToMouse = floorHit.point - transform.position;
    49.             playerToMouse.y = 0;
    50.             Quaternion newRot = Quaternion.LookRotation(playerToMouse);
    51.             rigid.MoveRotation(newRot);
    52.         }
    53.     }
    54. }
    55.  
    56.  
    So I have this simple movement script for my top-down project, the Move() function does all the movement, my character seems to be moving faster on the X axis than on the Z axis, I measured the ratio and it is always 1.5 or 1.6 times faster than what it should be, or Z is 1.6 times slower than what it should be.

    I tried looking at the Input Settings-> Axis, but all the variables for Horizontal were the same as Vertical. I even tried multiplying the "v" variable by 1.6 in order to catch up to the horizontal speed, still didn't work, then I tried to check if v is even changing and multiplied it by 100, but it moves the same speed. Please help
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    Is it the perspective of your camera? Z goes away from the default camera setup.

    Is gravity perhaps affecting the vertical in some way?

    On or about line 36, can you print newPos using Debug.Log() and see if the X and Z components are equal?

    Also, look at the Scene window at the same time you are playing it in the Game window: is what you expect happening?
     
  3. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,727
    It doesn't look like your factoring in the current direction the player is facing when applying the movement.
    Look at the FirstPersonController.cs script in the standard assets for a guide:

    Vector3 desiredMove = transform.forward*m_Input.y + transform.right*m_Input.x;

    This considers the direction the player is facing.
     
  4. danilo12

    danilo12

    Joined:
    Jul 21, 2015
    Posts:
    64
    I don't want the player to move in regards to his rotation, because i want him to rotate only by casting a Ray on the ground.

    I thought gravity might have had an effect but it seems like it doesn't,

    I debbugged it and X,Z values are the same.

    The last thing to check is the camera? I have no idea how it could affect anything so if you can please explain that it would be great.
     
  5. danilo12

    danilo12

    Joined:
    Jul 21, 2015
    Posts:
    64
    The problem I have are the speeds of the vector on both axes not being equal.
     
  6. danilo12

    danilo12

    Joined:
    Jul 21, 2015
    Posts:
    64
    bump... anyone?
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    If the camera is looking down the Z axis, and you move down the Z axis, you are going to see zero translational motion.

    Similarly, if the camera is looking "somewhat" down the Z axis and completely orthogonal to the X axis, you will also see less motion in the apparent Z direction.
     
    Cshank19 likes this.