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 Topthink · Nov 23, 2016 at 12:54 AM · movementnavmeshselection

Can't Get My Navmeshagents to Move to Different Directions.

I have several Navmeshagents and I want to move them to different locations at the same time. Here is my code. When I "right click" my mouse, they all move to the same location.

 private NavMeshAgent agent;

 void Start()
 {
     agent = GetComponent <NavMeshAgent> ();
 }

 void Update()
 {
     RaycastHit hit;
     if (Input.GetMouseButtonDown(1))  
     {
         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         if (Physics.Raycast(ray, out hit)) 
         {
             agent.SetDestination (hit.point);    
         }
     }
 }

I've been working on this for some time and can't quite figure out how to move them separately.

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

2 Replies

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

Answer by Eric5h5 · Nov 23, 2016 at 01:34 AM

You should use RaycastHit.collider.GetComponent to get the script on the agent that was hit, and use that with agent.SetDestination (hit.point). Also, make a manager object and put this script on the manager, and remove it from all the agents, since it's wasteful to have many instances of the script doing exactly the same thing in Update.

Comment
Add comment · Show 4 · 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 Topthink · Nov 23, 2016 at 01:40 AM 0
Share

Thank you. I'll look into your comment on the "$$anonymous$$anager Object" right away.

avatar image Eric5h5 Topthink · Nov 23, 2016 at 01:58 AM 0
Share

Just an empty GameObject that has a script on it.

avatar image christoph_r · Nov 23, 2016 at 01:58 AM 0
Share

As a small addition: You'd probably want to get the agent component when left clicking and set the destination when right clicking (or vice versa.)

avatar image Topthink christoph_r · Nov 23, 2016 at 02:13 AM 0
Share

Yes, that is exactly what I had in $$anonymous$$d...very simple and classic as in most of the RTS games. So far, I have been able to get the movement worked out...I can even set multiple waypoints with the right button click...no problem there. BUT, I'm having a heckuva time trying to figure out how to limit the movement to a single unit with the left click. I'm still working on it.

avatar image
0

Answer by christoph_r · Nov 23, 2016 at 01:25 AM

This code makes all agents it's attached to move to the hit point. You need to define conditions that decide if an individual agent should move or not. Assuming you are trying to create a basic RTS game, you would need a bool indicating if an agent is selected. This bool would need to be set e. g. through a selection manager. For example, it could set troops as selected if the left mouse button is clicked and a mouse raycast hit a collider that is attached to an agent's GameObject.

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 Topthink · Nov 23, 2016 at 01:38 AM 0
Share

Indeed, I'm trying to make a very simple RTS game...I have no plans on ever selling anything, I just get a lot of entertainment out of program$$anonymous$$g. I'm an old retired guy...beats watching TV.

Anyway, Ya, I've been working on this problem for awhile. I've been coding in C# for awhile and I've done some in-depth array and list sort of things but I'm new to Unity and can't quite grasp how all that converts over to Unity (yet).

I'm familiar with Raycasting but I've never done anything with a "selection manager" or with "select" but I will begin research into that immediately.

Thank you for your kind response.

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

72 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

Related Questions

Navmesh baking -- Odd results 1 Answer

Weird jumpy motion of character in Navigation Fundamentals proect 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

NavMesh Agent rotates on X axis when moving and snaps back to 0. 2 Answers

TextField/User Controls Conflicts 2 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