How to get a constant direction vector with mouse input

Hi guys,

Im having an object travel in the direction of my mouse input using “rb.velocity = vectorToTarget(object to mouse vector)) * moveSpeed”. However, the speed in which my object travels changes depending on the mouses distance from it. I just want a constant speed. How can I achieve this?

I have tried the many variations of normalizing the vector but it doesn’t change anything.

I really appreciate any replies, this problem is a headache!

Please, send more code if you want some help ...

–

1 Answer

1

You need to normalize the movement direction.

So, if your mouse position is “mousePos” and your character position is “charPos”, this code should work:

Vector3 moveDirection = (mousePos - charPos).normalized;
rb.velocity = moveDirection * moveSpeed;

Normalization turns a vector into a vector with the same direction, but a length of 1 - a direction vector. Normalizing the mouse or character position makes no sense, as those are points in space. Normalizing the direction from the character to the mouse makes a lot of sense.

yeah thanks, I realized that it was infact normalizing, however when I was moving below (1,1,1), it stuffs up. Thanks!

–