- Home /
Move object along the path,Move an object along the fixed path
How can i move my object along the fixed path (like spiral) and avoid obstacles? I've found that tool https://www.assetstore.unity3d.com/en/#!/content/84. It seems allows to move along the path, but what about avoiding obstacles auto? I mean an object should move and if along his path he see an obstavle it should avoid it and continue moving along the path. Are there any tools?
Answer by SohailBukhari · Jun 04, 2017 at 10:47 AM
Use Navigation mesh agent.
This component is attached to a mobile character in the game to allow it to navigate the scene using the NavMesh.
public bool SetDestination(Vector3 target);
Sets or updates the destination thus triggering the calculation for a new path. Note that the path may not become available until after a few frames later. While the path is being computed, pathPending will be true. If a valid path becomes available then the agent will resume movement.
using UnityEngine;
using System.Collections;
public class NavmeshMovement: MonoBehaviour {
public GameObject targets;
public NavMeshAgent mNavMeshAgent;
public int i ;
float dist;
void Start () {
mNavMeshAgent = GetComponent<NavMeshAgent>();
mNavMeshAgent.destination = targets.transform.GetChild(i).transform.position;
}
// Update is called once per frame
void Update () {
if(mNavMeshAgent.enabled)
{
dist = Vector3.Distance(targets.transform.GetChild(i).transform.position,transform.position);
mNavMeshAgent.destination = targets.transform.GetChild(i).transform.position;
if (dist < 2)
{
i++; //change next target
if (i < targets.transform.childCount )
{
mNavMeshAgent.destination = targets.transform.GetChild(i).transform.position; //go to next target by setting it as the new destination
}
//check if at end of cycle, then reset to beginning of cycle
if (i == targets.transform.childCount )
{
Debug.Log("NAVIGATION FINISHED. RESET.");
i = 0;
mNavMeshAgent.destination = targets.transform.GetChild(i).transform.position;
}
}
}
}
}
targets are the fixed path points for movement of your navmesh agent.
NavMeshObstacle
An obstacle for NavMeshAgents to avoid.
A NavMeshObstacle is cylindrical in shape and can move around the surface of the NavMesh with a specified velocity. By default, the obstacle will only affect the agent's avoidance behaviour rather than the pathfinding. This means that the agent will ignore the obstacle when plotting a path but will sidestep around it while moving along the path. If carving is enabled, the obstacle will create a temporary "hole" in the NavMesh. The hole will be recognised by the pathfinding, so paths will be plotted to avoid the obstacle. This means that if, say, an obstacle blocks a narrow gap, the pathfinding will seek an alternative route to the target. Without carving, the agent will head for the gap but won't be able to pass until the obstacle is clear.
Attach Navmesh Obstacle Component on your obstacles then your agent will avoid collision with the obstacles. For More Navmesh Obstacle description see this Link