Waypoint script

I have a simple waypoint script I have been working on that works in 2 parts the waypoint(empty game object with box collider) and a gameobject ( in this case a roach). The problem I am having with the script is that it starts up fine but when it enters a new waypoint it is supposed to get the next position to go to and its not detecting the collide, i have tried both convex and non convex mesh colliders and a box collider and still doesn’t detect. Any suggestions or ideas would be much appreciated

here is the scripted attached to the moving object(waypoint.js)

var start : Transform;
var end : Transform;

function Update()
{
	waypoint(start,end);
}

function waypoint(s : Transform, e : Transform)
{
	start=s;
	end=e;
	move();
}

function move() 
{
	transform.position = Vector3.Lerp(start.position, end.position, Time.time);
}

Here is the waypoint spot script (waypointTrigger.js)

var waypoint1 : Transform;
var waypoint2 : Transform;
var way : waypoint;

function OnTriggerEnter (zone : Collider) 
{
	if(zone.CompareTag("Roach"))
	{
		way.waypoint(waypoint1,waypoint2);
	} 
}

You need to have a rigid body on one of them in order for it to detect the collision.

Nice thank you that didn’t even occur to me until you said it then I kinda had to slap myself in the head.

However I an now running into a different problem, instead of following the way points like a Lerp, after getting to the second it just jumps to each like a regular translate.position.

Would i have anything to do the way Im starting it up in the Update function?

Any ideas would be greatly appreciated.

Thank you
TWest

I’m gonna guess the movement between the first two only takes one second as well.

Your call to Mathf.Lerp() is using Time.time as the t factor.

As soon as Time.time is over 1.0, it’s just going to jump from point to point each frame. You should use a timer that is triggered on the change of waypoint. Also on a personal note (I don’t mean to be rude), but the code you’ve got there is quite difficult to follow. Here’s how I’d do it.

// waypoint.js
var start : Transform;
var end : Transform;
var timer : float;
var travelTime : float; // This should be the time it takes to travel from start to end, you may want to add this as a parameter in SetWaypointPath if you need a different time for each section.

function SetWaypointPath(s : Transform, e : Transform)
{
  // Check to make sure we don't reset the timer for the same set of waypoints.
  if(s != start  e != end)
  {
    start = s;
    end = e;
    timer = 0.0f;
  }
}

function Update()
{
  timer += Time.deltaTime;
  if(start != null  end != null)
  {
    // Use the timer to generate a percentage of how far through the travel time we are.
    var percentage : float = timer / travelTime;
    transform.position = Vector3.Lerp(start.position, end.Position, percentage);
  }
}
// waypointTrigger.js
// I assume you are setting these manually in the editor
var waypoint1 : Transform;
var waypoint2 : Transform;
var way : waypoint;

function OnTriggerEnter(zone : Collider)
{
  if(zone.CompareTag("Roach"))
  {
    way.SetWaypointPath(waypoint1, waypoint2);
  }
}

By keeping a timer and resetting it when you call SetWaypointPath, the system becomes more autonomous. Also by having the move function called in Update, you incur a cost, and end up sending variables from your class into your own class…which doesn’t really make sense. Functions called by external scripts should only transport data from other scripts and deal with that data. If there something else that needs to be done internally, it shouldn’t be coupled with code running on external scripts. Sorry if I’m going over the top here, but the easier your code is to follow in the first place, the easier bugs like this are to find.

Hope this gets you back on the right track :slight_smile:

Ya sorry that is usually what my code looks like the begin with, when I first attempt something new, I just try to get something simple working and build from there. Thank You You have been very helpful. The only real reason I am calling from update is for testing purposes it will be triggered by another event when I am done. Ill have to give this a try.

Thank You
TWest

No Problems :slight_smile:

Post your results and let me know how you go.

Awesome this worked perfectly, I have to admit I didn’t fully understand Time and probably should have read up on it. I just had to make a couple of small error changes here is the code.

//waypoint.js
var start : Transform;
var end : Transform;
private var timer : float;
var travelTime: float;

function SetWaypointPath(s : Transform, e : Transform)
{
	if(s != start  e != end)
	{
		start = s;
		end = e;
		timer = 0.0;
	}
}

function Update()
{
	timer += Time.deltaTime;
	if(start != null  end != null)
	{
		var percentage : float = timer / travelTime;
		transform.position = Vector3.Lerp(start.position, end.position, percentage);	
	}
}
//waypointTrigger.js
var waypoint1 : Transform;
var waypoint2 : Transform;
var way : waypoint;

function OnTriggerEnter (zone : Collider) 
{
	if(zone.CompareTag("Roach"))
	{
		way.SetWaypointPath(waypoint1,waypoint2);
	} 
}

I hope this can help some of the other waypoint questions I have seen on the board as well.

Thank You

TWest