- Home /
enable/disable navmesh agent and obstacle same object (player)
hi mates,
I'm about to make my player move to clicked point and to prevent other game object to push my player around (vice versa) my code is something like this =
if raycast to ground -enable agent -disable obstacle -move to hit.point
if player arrive to destination || player no movement -enable obstacle -disable agent
can someone help me to code this??
its c sharp. my code is as follow:
void Start ()
{
agent = GetComponent<Nav$$anonymous$$eshAgent> ();
obstacle = GetComponent<Nav$$anonymous$$eshObstacle> ();
}
void Update ()
{
if (Input.Get$$anonymous$$ouseButtonUp (0))
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit))
{
if (hit.collider.tag == "Ground")
{
agent.enabled = true;
obstacle.enabled = false;
agent.destination = hit.point;
}
}
}
if (agent.velocity == Vector3.zero)
{
agent.enabled = false;
obstacle.enabled = true;
}
}
when move, i can get my player agent enabled & obstacle disabled. but when it stop, it doesn't turn into agent disabled obstacle enabled
I see. In your start function:
void Start ()
{
agent = GetComponent<Nav$$anonymous$$eshAgent>();
obstacle= GetComponent<Nav$$anonymous$$eshObstacle>(); // You wrote "agent" ins$$anonymous$$d of "obstacle"
}
EDIT: Nvmd, you fixed it.
Just now typo during copy paste.. i have repost the corrected one.. but still not working
Your answer
Follow this Question
Related Questions
Graphics Problem and NavMesh Obstacles 0 Answers
Navmesh Bridges 0 Answers
Unity NavMesh obstacle avoidance? 0 Answers
Draw path along Navmesh agent path 3 Answers