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

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour {
    public float Speed;

    float h;
    float v;
    Rigidbody rigid;
    public Vector3 newPos;
    bool isWalking;
    Animator anim;
    int floorMask;


    void Start () {
        rigid = GetComponent<Rigidbody> ();   
        anim = GetComponent<Animator> ();
        floorMask = LayerMask.GetMask("Floor");
    }
   

    void FixedUpdate () {
       
        v = Input.GetAxisRaw("Vertical");
        h = Input.GetAxisRaw("Horizontal");

        Move (h,v);
        Animate ();
        Turn ();
    }
    void Move(float h, float v){
        newPos = new Vector3 (h,0f,v);
        newPos = newPos * Time.deltaTime * Speed;
        rigid.MovePosition (transform.position + newPos);
    }
    void Animate(){
        isWalking = h != 0f || v!= 0f;
        anim.SetBool ("isWalking", isWalking);
    }
    void Turn(){
        Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit floorHit;

        if(Physics.Raycast(camRay,out floorHit,100f,floorMask)){
            Vector3 playerToMouse = floorHit.point - transform.position;
            playerToMouse.y = 0;
            Quaternion newRot = Quaternion.LookRotation(playerToMouse);
            rigid.MoveRotation(newRot);
        }
    }
}

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

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?

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.forwardm_Input.y + transform.rightm_Input.x;

This considers the direction the player is facing.

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.

The problem I have are the speeds of the vector on both axes not being equal.

bump… anyone?

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.

1 Like