- Home /
Contact Triggered Waypoint
Does anyone have an idea on how to make it so that once a trigger hits an object, the object is forced along a way-point that's ahead of it and forced to move along it until it reaches the end of the way-point?
Answer by skovacs1 · Sep 15, 2010 at 02:54 PM
You would just setup a waypoint script that is activated on collision like you describe.
MoveScript.js
var wayPoints : Vector3[] = new Array(); //points to move to var moveToWayPoint : boolean = false; //whether to move to points var startPosition : Vector3; //where we are moving from var speed : float = 0.5; //how fast to move (1/speed seconds to move between points) private var weight : float = 0; //how far we've moved from between points private var wayPoint : int = 0; //the point we're moving to
Update() { if(moveToWayPoint && startPosition) { //moving weight += Time.deltaTime * speed; //update how far if(weight >= 1) { //move to the next point weight -= 1; startPosition = wayPoints[wayPoint]; wayPoint++; } if(wayPoint < wayPoints.Length) { //not at the end, so move transform.position = Vector3.Lerp(startPosition, wayPoints[wayPoint], weight); } else { //at the end, so reset weight = 0; wayPoint = 0; moveToWayPoint = false; } } }
TriggerScript.js
OnTriggerEnter(other : Collider) { //collided so make it move to waypoints
var script : MoveScript = other.gameObject.GetComponent(MoveScript);
script.StartPosition = other.transform.position;
script.moveToWayPoint = true;
}
You could put your waypoints wherever you like or generate them somehow (finding one which is ahead of it? what defines being ahead?).
Because of the poor English, I'm not sure if you meant waypoint (singular) (it should read "...the object is forced along a path to a waypoint..." and "...until it reaches the waypoint?") or waypoints (plural) (it should read "...the object is forced along a path of waypoints..." and "...until it reaches the last waypoint?"), so I went with plural and if you only want one, you can simplify it from there.
Your answer
![](https://koobas.hobune.stream/wayback/20220613073226im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Trigger on OnCollisionEnter 0 Answers
Waypoints, avoidance, and general advice. 1 Answer
OnCollisionStay melee 1 Answer