Also just be sure to know that this is in a 2D game.
Now what this code does is move the object from side to side or left to right depending on what buttons you press in game. If you press a horizontal button whilst pressing a vertical button the sprite will move diagonally.
The problem is that the sprite moves faster when moving diagonally. I sort of understand this to be because the speed is being added to the equation twice so basically speed is doubled… at least I think that’s what’s happening I don’t know I might very well be wrong. But anyways the question is simple.
Try to visualise the issue. If you hit up, you’re making a vector of (0, 1). Right is (1, 0). But both is (1, 1). If you draw a square, the distance from the center to the corner is longer than the distance to the middle of one of the sides.
So instead, you want to make a circle, which has the same distance to any point along its circumference. This is easily done by just normalising your direction (meaning it will have a length of 1, always) before using it.
Though firstly, don’t write long lines of code like that. Break it up so you can clearly see what you’re doing:
float delta = spriteSpeed * Time.deltaTime;
float xDir = Input.GetAxis("Horizontal");
float yDir = Input.GetAxis("Vertical");
Vector3 direction = new Vector3(xDir, yDir, 0);
direction = direction.normalized * delta;
transform.Translate(direction);
In this case we get the normalized direction and then apply our speed/delta time.
Unlike Input.GetAxisRaw, Input.GetAxis will return a float in the range of 0 to 1 and so sometimes it’s best to clamp the direction instead of normalizing it so then you get smoothed Input which can help to give the impression of inertia. This is especially useful for character controller scripts that will often just pass values from GetAxis to CharacterController.Move.
It all depends on the kind of feel you’re going for. Some people like a slightly heavy realistic feel while others like a light and fast responsive movement. I guess it comes down to whether your character is a tank or a fly…
Can you elaborate further on the difference clamped vs normalized?
I understand what your saying about it giving an “inertial” feeling to movement, but why, or what is the difference and how does that affect (what I would have thought) the magnitude of the players motion?