- Home /
Using .move(), the enemy moves faster at a diagonal
Considering:
direction = _player.position - transform.position;
direction.y = 0;
direction = Vector3.Normalize(direction);
velocity = direction * _moveSpeed * Time.deltaTime;
Why does the enemy have variable speed at different angles when using:
_controller.Move(velocity);
While the enemy has constant speed no matter the direction using:
_controller.transform.position += velocity;
I'd prefer to use the .Move function but what is going on in it? Why isn't the speed constant?
Answer by Maui-M · Feb 13, 2014 at 10:47 PM
Normalize scales all the values down so the max value is one. So if x=1 and z=1 you would essentially be moving at a speed of 1.4(square root of 2) instead of 1 in the x or z direction.
Try using:
transform.TransformDirection(direction);
Instead of:
direction = Vector3.Normalize(direction);
Answer by Ultroman · Nov 13, 2017 at 08:02 PM
Since normalizing the movement vector results in a movement vector that is always 1 in length, you lose the granularity/interpolation functionality of an analog stick, so I would like to refer to the following Unity forum post: https://forum.unity.com/threads/diagonal-movement-speed-to-fast.271703/
...where I found the solution to my similar problem, when using the analog stick on my controller.
Quote from cranky:
Normalizing [the movement vector] isn't always the best solution. If you're using a keyboard, it's fine. But with an analog stick, it will always force the length of the vector to be 1, so you won't be able to move slowly. Instead, get the length of the vector and see if it's greater than 1. If so, divide the movement vector by the length.
...and also (quoting myself)...
Make sure the "Sensitivity" of your analog stick axes is set to no more than 1! Mine were set to 2 for some reason (damn copy/pasting from old projects), so even when using cranky's fix, I had inputs of (1f, 1f) when within ~30 degrees of either diagonal, while holding the stick to its extremity. This made me think there might be some input-multiplier...and voila, sensitivity was the culprit.
I'm not sure that's the solution either. Or maybe I'm confused.
The solution seems to always work when travelling straight.
The solution works when travelling at a diagonal at FULL FORC$$anonymous$$
But travelling at, say, HALF force at a diagonal causes problems. Even using $$anonymous$$agnitude or Sqr$$anonymous$$agnitude I think I am seeing this problem... If the player intends to go half force at an angle, I'm having trouble getting a simple "if" statement to catch that. So I'm still losing granularity at angles.
Unless I'm confused?? I drew a picture where half forces are shown in RED
Your answer
Follow this Question
Related Questions
Pushing a character controller (physics interaction) 0 Answers
why does character controller accelerate off ledges? 1 Answer
How to combine Quaternions? 1 Answer
(CharacterController) Jumping from other script 0 Answers
CharacterController movement messed up when Y Center is larger than radius 0 Answers