- Home /
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.
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.
Thank you. I'll look into your comment on the "$$anonymous$$anager Object" right away.
Just an empty GameObject that has a script on it.
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.)
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.
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.
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
Follow this Question
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