I have this car… it needs to follow a set of points to get where it needs to go. THese points are calculated using A* pathfinding as per Aron Granberg’s method. I’ve looked on his page and it says that I should be able to put the method PathComplete( Vector3[ ] points ) in my code and the array points will have all the points I need to follow in order to get to where I need to go. I should be able to scroll through that array and move my car to each Vector3 in that array, right? Here’s the complete script:
using UnityEngine;
using System.Collections;
public class GroundUnitAI : MonoBehaviour
{
public Transform target;
public int Speed = 20;
public int rotateSpeed = 30;
private Transform tempform;
private Command state = Command.Walk;
public enum Command
{
Attack,
Walk,
Stay
}
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if( Input.GetButtonDown("Fire2") )
(GetComponent (typeof(Seeker)) as Seeker).StartPath(transform.position, target.position);
}
public void LateUpdate()
{
float temp;
temp = Terrain.activeTerrain.SampleHeight(transform.position);
Vector3 v = new Vector3( transform.position.x, temp, transform.position.z );
transform.position = v;
}
public void PathComplete( Vector3[] points )
{
//The points are all the waypoints you need to follow to get to the target
//we'll scroll through the array, rotating the transform and then moving it.
//it doesn't work, though
if( state != Command.Stay )
{
for( int x = 0; x < points.Length; x++)
{
tempform.position = new Vector3( points[x].x, transform.position.y, points[x].z );
if( transform.position != tempform.position )
{
transform.LookAt( tempform );
transform.Translate( 0, 0, Time.deltaTime*Speed );
}
else
transform.Translate( 0, 0, 0 );
}
}
}
}
Why does the car not move?