0 votes

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

asked Oct 26, 2019 in Projects by Glennster (12 points)

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)

"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.

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

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.

1 Answer

+1 vote

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:
https://docs.godotengine.org/en/3.1/tutorials/math/vector_math.html#practical-applications

answered Oct 26, 2019 by kidscancode (13,244 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.