Answer the question
In order to leave comments, you need to log in
How to determine that the player is moving towards the object?
Hello everyone
Please tell me, who knows how I can determine whether the player is moving towards the cursor or moving in the opposite direction from it (the position of the cursor is constantly changing, the player is given a motion vector from the keyboard
) animation, in the opposite - another
Answer the question
In order to leave comments, you need to log in
1. Determine the position of the cursor and the position of the player at time X and find the length of the vector from these points;
2. Determine the position of the cursor and the player at the moment X + n and also find the length of the vector;
3. Compare the lengths of the vectors. If the second one is less, it moves to the cursor; if it is more, it leaves;
4. Spit in the face of those who say that a programmer does not need mathematics.
Ronald McDonald painted everything correctly, but since this is a unity topic, where would it be without a ready-made code xD
private GameObject player;
private float oldSqrMagnitude;
private Camera camera => Camera.main;
private void Update()
{
var newSqrMagnitude = (player.transform.position - camera.ScreenToWorldPoint(Input.mousePosition)).sqrMagnitude;
if (newSqrMagnitude > oldSqrMagnitude)
{
Debug.Log("дальше");
}
else if (newSqrMagnitude < oldSqrMagnitude)
{
Debug.Log("ближе");
}
else
{
Debug.Log("стоит");
}
oldSqrMagnitude = newSqrMagnitude;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question