- Home /
How to get a velocity unit vector from a NavMeshAgent?
I am trying to use a NavMeshAgent to chase around the player character. I wrote a Character class, from which both the Player and the Enemy inherit from. The character class basically takes in a velocity vector and uses it to move character as such:
public class Character : MonoBehaviour {
public void Move (Vector3 velocity) {
this.velocity = new Vector3 (velocity.x, 0.0F, velocity.z).normalized * MOVEMENT_SPEED;
}
public void FixedUpdate () {
this.rigidBody.MovePosition(this.transform.localPosition + this.velocity * Time.deltaTime);
}
}
The player class basically just takes the input from the WASD keys and sends it to the parent class, the Controller:
public class Player : Character {
public void Update () {
base.Move(new Vector3 (Input.GetAxisRaw("Horizontal", 0.0F,
Input.GetAxisRaw("Vertical"));
}
}
This works perfectly fine, however, the trouble comes with the Enemy class. I'd like to use the enemy class in pretty much the same way I used the Player class, with the class calling base.Move when it needs to move. So far I wrote the following class:
public class Enemy: Character {
public void Update () {
this.GetComponent<NavMeshAgent> ().SetDestination(player.transform.position);
// base.Move(this.GetComponent<NavMashAgent>().velocity); <-- Doesn't do anything useful
}
}
The problem with this code is that the NavMeshAgent uses its own built in methods to move the character around the screen, however, I do not want it to do that. I would like for the Enemy class to somehow communicate a velocity vector to the Character class, just like the Player class is doing. I've tried to pass the NavMeshAgent's velocity component to the Character class however the velocity that NavMeshAgent seems to be giving me is useless, as it seems to have only a Y component for some reason. Also, how can I stop the NavMeshAgent from moving around the enemy, so that this can be handled solely by the Character class? Thank you very much, I appreciate any help or advice on this matter!
Answer by kirbyderby2000 · May 13, 2020 at 10:52 PM
Hey I know this question is pretty old but I also had a problem very similar to yours. For anyone else that finds this in the future, you need to access the NavMeshAgent's "desiredVelocity" property. The enemy example in the script should really look like this:
public class Enemy : Character
{
NavMeshAgent agent;
private void Awake()
{
agent = GetComponent<NavMeshAgent>();
}
void FixedUpdate()
{
base.Move(agent.desiredVelocity);
}
}
My problem was slightly different than yours because I wanted to translate the NavMeshAgent's desired velocity into a 2D vector for my character controller class (identical to the player's 2D input vector). The 2D vector I wanted was an X input relative to the agent's right-left axis, and a Y input relative to the agent's forward-backward axis. I did this by doing the following:
/// Transforms a NavMeshAgent's desired velocity into an input Vector2 (identical to raw axis player input)
public Vector2 AgentVelocityToVector2DInput(UnityEngine.AI.NavMeshAgent agent)
{
float xValue;
float yValue;
// Get the NavMeshAgent's desired velocity direction relative from it's actual position
Vector3 desiredVelocityRelativeToAgent = agent.transform.InverseTransformDirection(agent.desiredVelocity);
// Normalize the vector so it doesn't have a magnitude beyond 1.0f
desiredVelocityRelativeToAgent.Normalize();
// X value will be the X value of the vector
xValue = desiredVelocityRelativeToAgent.x;
// Y value will be the Z value of the vector
yValue = desiredVelocityRelativeToAgent.z;
// It's worth noting that you should scale this 2D vector by a desired speed on a scale of 0 - 1
return new Vector2(xValue, yValue);
}
Your answer
Follow this Question
Related Questions
Editing NavMesh Paths? 0 Answers
is it possible to use navmesh on flying character and target ? 0 Answers
Navmesh agent forward movement 1 Answer
My ai is getting stuck when there is a lot of them 0 Answers
Need help detecting barriers for my game 0 Answers