global mouse position - position

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Glennster

This is 2D

Hi, I havent used godot for a few months now. So when I came back and found this in one of my scrips “(get_ global_ mouse_position()-position).normalized()”. Which in itself i understand what it does but not how.

So to explain the first part gets the mouse position then the second part “-position”" is so you can get a charater follow it staright to the position. Not just the x axel. then the last part is to normaliz the numbers. But i dont have a singel clue on how “-position” can make a thing follow your mouse on both axel on the same time

So my question is what does position in this thing actuly do?

is there any aditional information you need inform me

Thousand thanks

When you normalize, you are returning a value that gives you the direction between 0 and 1, so if you multiply it for example, 2, it will give you the direction, and you can multiply how far away it goes.
I think you want to make you character go to a certain position?, what you should do is store the position you want him to go, then you do, (position - gotoposition)

usurun | 2019-10-26 20:17

“When you normalize, you are returning a value that gives you the direction between 0 and 1” - this is incorrect. Normalizing scales a vector to a length of 1.

kidscancode | 2019-10-26 22:12

I’m sorry, I meant to have said -1, and 1. Thanks for the correction.

usurun | 2019-10-26 22:23

Except that doesn’t make sense either. It doesn’t return a “direction between -1 and 1” because there’s no such thing. It specifically keeps the vector’s direction and scales its length to 1. This is what we call a “direction vector”.

Perhaps you were thinking that the normalized vector’s components (x and y) will have values between -1 and 1, but that’s not really all that useful on its own, it’s just the mathematical effect of having a vector of length 1.

kidscancode | 2019-10-26 22:29

:bust_in_silhouette: Reply From: kidscancode

Vector subtraction has a specific application. If you have two vectors, A and B, then A - B gives you the vector pointing from B to A.

So, what this code is doing is calculating a vector pointing from position to the global mouse position. In other words, this gives you the vector to move towards the mouse.

You can see more information and learn more about vector math in the docs: