P
P
Pythonchik32020-08-23 11:24:20
C++ / C#
Pythonchik3, 2020-08-23 11:24:20

Get displacement vector in NavMesh Unity?

Hello!
I read the Unity documentation, and I realized that you can set a target for the NavMeshAgent, and it will move there. The question arises: is it possible to obtain a correct displacement vector? ATTENTION - not the vector of the current displacement, but the direction in which my object should move.
I will be grateful for any help.
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2021-09-29
@Andrei1805

I'm not an expert in unity, but I don't know of any specialized team for this. There is such an option:

Vector3 last_position, move_vector = Vector3.zero;
void Start()
{
  last_position = transform.position;
}
void FixedUpdate()
{
     move_vector = transform.position - last_position; //Определения вектора, направленного от предыдущей позиции к следующей
      last_position =  transform.position; // Переназначение предыдущей позиции
}

Also, all this can be done not just in FixedUpdate (), but start a timer so that the vector is determined at large intervals of time like:
float deltatime, timer;
void Update()
{
  if(Time.time > timer)
  {
     move_vector = transform.position - last_position; 
      last_position =  transform.position; 
     timer+=deltatime;
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question