struggle with normalized direction vector...

Hi everyone !

for a good amount of time now, i’ve been using this technique to give a direction for player / bullets / etc to move:

Imagine that A is the player position and B a world point where i want to direct my bullet.

the normalized direction vector should be (B-A)/(B-A).magnitude right ?

but when i do this, the further the point B is from the point A, the longer will be my normalized vector…
but i just want a vector that is pointing from A to B with a length of 1 (like a radius on a trigonometric circle).

Do i did it wrong ?

A more detailled example with my code:
//how i get the direction vector of the bullet (transform.position is the playerPos)
Vector3 mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
Vector3 bulletDir = mousePos - transform.position;
bulletDir = bulletDir / bulletDir.magnitude;

//how i move the bullet (in FixedUpdate):
rb.AddForce (gameObject.transform.right * direction.x * speed

  • gameObject.transform.up * direction.y * speed);

the problem is that when i do this, the further my mouse is, the faster my bullet is, and i just want a stable speed.

I also wonder what techniques are you using when you want to move something in a particular direction ?

vector3 Direction = (TargetLocation - StartLocation).normalized

thanks for this it’s good to know (faster to do), but it doesn’t change a thing… the lenght of the vector is still changing as further my mouse is from the player…

here’s an exemple of what i’m seeing, it’s a Debug.DrawRay of my direction vector:
http://imgur.com/a/VopZe

first one is with my mouse near the player, second one is the same but with my mouse further
What i want is the always same lenght vector, cause with that vector, bullet can be faster the further my mouse is from the player, and i don’t want this

Ok i found a solution:

i don’t use normalized direction anymore, i’ll just now use a trigonometric circle radius like this:
Vector2 direction = endPos - startPos;
float angle = Mathf.Atan2 (direction.y, direction.x);
direction = new Vector2 (Mathf.Cos (angle), Mathf.Sin (angle));

this result to a vector that is 1unit long ALWAYS (not like normalized directions) pointing from my start to the end point.

But i still believe there is a faster and easier way to do this… what techniques are you using to move objects in a particular direction ?

.normalized is always a unit length, but you were using the Vector3 positions of the transform and mouse position, and there’s no guarantee they’re at the same “z”, so as you moved your mouse the vector was still unit length, just pointing in 3d space (probably toward the camera).

What you probably want is:
Vector3 mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
Vector3 transformPos = transform.position;
mousePos.z = transformPos.z;
Vector3 bulletDir = (mousePos - transformPos).normalized;

4 Likes

oh… all this for this simple mistake… yep you’re right, I was not paying attention to the Z value of my mouse position…

my solution works though, but it use Atan2 Sin and Cos ^^

Thanks a lot !

1 Like