Question by
vespera · May 11, 2016 at 07:06 PM ·
c#gameobjectnavmesh
one game object follows another, somethings wrong with my script?
In my game I need pathfinding for a car, I used navmesh, but the navmesh agent performs weirdly on the car (keep rotating when stop), so I use a capsule as navmesh agent and want the car to follow, but the car doesn't move, can anyone tell me what wrong in my script? using UnityEngine; using System.Collections; using UnityStandardAssets.Vehicles.Car;
public class carpathfinding : MonoBehaviour
{
//CarController carcontrol;
public NavMeshAgent navagent;
float rotationspeed= 100;
float maxspeed = 150;
private GameObject capsule;
private float distance;
public bool reachdestination = false;
private Vector3 wantedposition;
void Start()
{
capsule = GameObject.Find ("Capsule");
navagent = GetComponent<NavMeshAgent>();
//carcontrol = GetComponent<CarController>();
wantedposition = new Vector3(capsule.transform.position.x, capsule.transform.position.y, capsule.transform.position.z);
distance = Vector3.Distance(capsule.transform.position, transform.position);
}
void LateUpdate()
{
if (navagent.velocity.magnitude >= 0)
{
if (distance >= 0.5f)
{
transform.position = Vector3.MoveTowards(transform.position, wantedposition, Time.deltaTime * navagent.speed );
var rotation = Quaternion.LookRotation(capsule.transform.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * navagent.angularSpeed);
}
if (distance <= 0.5f)
{
reachdestination = true;
return;
}
}
}
}
Comment