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 /
  • Help Room /
avatar image
0
Question by DJGhostViper · Jun 20, 2017 at 12:59 AM · movementnavmeshagentdirectiontransform.forwarddestination

How to make a NavMeshAgent move in the direction it is looking?

I have a navmesh agent I'm trying to move off another gameobject below it. I'm trying to move it forward in whichever direction it is looking by telling the agent to move in front of where the eyes are looking. This code moves the agent in a direction but not where it is looking. How can I make the agent move the direction the eyes of the agent are looking? Thanks

 public float distance = 10f;

     void OnTriggerStay(Collider c)
     {
     
     
   c.GetComponent<StateController> ().agent.destination = c.transform.position + c.GetComponent<StateController> ().eyes.forward * distance;
 
     
     }
 
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

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Dizy · Jun 20, 2017 at 08:03 AM

NavMeshAgent are especially usefull when you make a RTS-like game. Setting the destination of your units allow them to use the built-in path finding algorithme.

Using it this way seems strange for me but here is the solution :

 void OnTriggerStay(Collider c) {
     float distance = 10;
     c.GetComponent<StateController> ().agent.destination = c.transform.position + c.GetComponent<StateController>().eyes.forward * distance;
 }

(Not Tested)

In fact you say to your NavMeshAgent to move forward, corresponding to Vector3(0, 0, 1). You need to specify the distance or the movement will be almost invisible.

Edit : Nevermind, i've not seen the "OnTriggerStay", well if you need to move forward OnTriggerStay it make sens... just use this :

 c.transform.position + c.GetComponent<StateController>().eyes.forward

As Woltus said

Comment
Add comment · Show 1 · 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
avatar image DJGhostViper · Jun 20, 2017 at 04:04 PM 0
Share

@Dizy You were right you need to specify the distance for the agent to travel or else it will not move at all.

avatar image
0

Answer by tongtheluong · Jun 20, 2017 at 01:32 AM

What do you mean? Do you need an agent finding target? so it must be SetDestination

Comment
Add comment · Show 1 · 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
avatar image DJGhostViper · Jun 20, 2017 at 05:34 AM 0
Share

I have a navmesh agent and the only way I know how to move it is setting the destination. But what should I do if I just want to tell it to move forward? Theres no easy way to tell it just to move forward it seems while using the navmeshagent.destination because you have to set the destination to some type of gameobject in the scene for it to move to it. Transform.forward moves the agent but it doesn't move the agent where the eyes of the agent are pointing. I'm looking for a good way to tell the agent to move forward to where the eyes are looking currently as right now the agent is moving to a random spot not where the eyes were pointing.

@tongtheluong

avatar image
0

Answer by rina81 · Jun 23, 2017 at 04:16 AM

I have the same problem. @DJGhostViper : Have you find a solution?

Comment
Add comment · Show 1 · 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
avatar image DJGhostViper · Jun 24, 2017 at 04:59 AM 0
Share

@rina81 The above code is corrected with the answer but here it is again, modify the distance to deter$$anonymous$$e how far you want the agent to move forward. ("Eyes" in the code is just an empty gameobject in front of the agent so you can tell where the agent is looking). Enjoy!

 public float distance = 10f;
 
      void OnTriggerStay(Collider c)
      {
      
      
    c.GetComponent<StateController> ().agent.destination = c.transform.position + c.GetComponent<StateController> ().eyes.forward * distance;
  
      
      }


avatar image
0

Answer by Woltus · Jun 20, 2017 at 07:44 AM

Try use:

  void OnTriggerStay(Collider c)
  {
      void sc = c.GetComponent<StateController> ();
      sc.agent.destination =  c.transform.position + sc.eyes.forward;
  }
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

93 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Top Down Character - Face direction of movement? 0 Answers

Get Nav Agent To Circle The Player 0 Answers

Air Control While Jumping? 1 Answer

ClampMagnitude while ignoring a direction 0 Answers

How do I make it so that my First Person Controller script allows for movement in all directions? 1 Answer


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