As you can see in the image above when I click on the white path the object move perfectly towards the clicked position. When I click on the blue ground the object doesn't move there but it finds the nearest possible location on the white path, which is the behavior that I don't want.
I want the object to not move if the click is outside of the white path.
Inspector:
White Path: Navigation Static - Walkable
Blue Ground: Nothing.
Object Script:
void Update ()
{
if (Input.GetButtonDown("Fire1"))
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
navAgent.destination = hit.point;
navAgent.Resume();
}
}
}
I want the object to not move if the click is outside of the white path.
You can do this by checking which object is clicked. You can check it by name with hit.collider.name
or you can use tag with hit.collider.CompareTag
to see which object is clicked. I suggest you use tag.
Create a tag called "whitepath" then set your whitepath GameObject to this tag. You can then compare the tag name after the raycast. This is Unity's on how to create tags.
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
//Check for white path
if (hit.collider.CompareTag("whitepath"))
{
navAgent.destination = hit.point;
navAgent.Resume();
}
}
}
}