Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jellythedonut · Feb 17, 2016 at 09:37 PM · c#aicharacternavmeshnavmeshagent

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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);
 }



Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges