- Home /
Question by
Newman2393 · Nov 10, 2013 at 04:00 PM ·
waypointwaypoint system
How do I make my enemy stop at specific waypoints?
Hello everyone, I am making a waypoint system for a game, and it's working exactly the way I want it to. The only issue is that I want my enemy to stop at specific waypoints. So far I have only got it stopping at the very first waypoint. Can you please help me out?
var accel = 0.8;
var inertia = 0.9;
var speedLimit = 10.0;
var minSpeed = 1.0;
var stopTime = 1.0;
private var currentSpeed = 0.0;
private var functionState = 0;
private var moveState : boolean;
private var waypoint : Transform;
var rotationDamping = 6.0;
var smoothRotation = true;
var waypoints : Transform[];
private var WPindexPointer : int;
private var x : int;
function Start ()
{
functionState = 0;
}
function Update ()
{
if (functionState == 0)
{
Accell ();
}
if (functionState == 1)
{
Slow ();
}
waypoint = waypoints[WPindexPointer];
}
function Accell ()
{
if (moveState == false)
{
moveState = true;
}
if (waypoint)
{
if (smoothRotation)
{
var rotation = Quaternion.LookRotation(waypoint.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationDamping);
}
}
currentSpeed = currentSpeed + accel * accel;
transform.Translate (0,0,Time.deltaTime * currentSpeed);
if (currentSpeed >= speedLimit)
{
currentSpeed = speedLimit;
}
}
function OnTriggerEnter (myTrigger : Collider)
{
switch(myTrigger.gameObject.name)
{
case "trigger1":
stopTime = 5.0;
Debug.Log(stopTime);
functionState = 1;
WPindexPointer++;
break;
case "trigger3":
stopTime = 2.0;
Debug.Log(stopTime);
functionState = 1;
WPindexPointer++;
break;
default:
stopTime = 3.0;
Debug.Log(stopTime);
functionState = 1;
WPindexPointer++;
break;
}
if (WPindexPointer >= waypoints.Length)
{
WPindexPointer = 0;
}
}
function Slow ()
{
Debug.Log("I am in Slow()");
moveState = false;
Debug.Log("moveState = false");
currentSpeed = currentSpeed * inertia;
transform.Translate (0,0,Time.deltaTime * currentSpeed);
if (currentSpeed <= minSpeed)
{
currentSpeed = 0.0;
Debug.Log("Current speed is 0");
Debug.Log(stopTime);
yield WaitForSeconds (stopTime);
Debug.Log("Stopped");
functionState = 0;
Debug.Log("Off I go again");
}
}
Also, I figured out it's not running the if(currentSpeed <= minSpeed) function except on the first trigger.
Comment