- Home /
Waypoint pathfinding gone terribly wrong.
Hey everyone, i have this script(Below). And its my messy attempt at doing a waypoint pathfinding script without arrays, the problem is, when the enemy gets to the first waypoint, he turns upways, angled himself, and flies away.
var Waypoint1 : GameObject;
var Waypoint2 : GameObject;
var Waypoint3 : GameObject;
var Waypoint4 : GameObject;
var Waypoint5 : GameObject;
var Waypoint6 : GameObject;
var TargetWaypoint : GameObject;
var Counter : int = 1;
function Start () {
Waypoint1 = GameObject.Find("Waypoint1");
Waypoint2 = GameObject.Find("Waypoint2");
Waypoint3 = GameObject.Find("Waypoint3");
Waypoint4 = GameObject.Find("Waypoint4");
Waypoint5 = GameObject.Find("Waypoint5");
Waypoint6 = GameObject.Find("Waypoint6");
TargetWaypoint = GameObject.Find("Waypoint" + Counter);
}
function Update () {
var toTarget : Vector3 = TargetWaypoint.transform.position - transform.position;
var turnRate : float = 200 * Time.deltaTime;
var lookRotation : Quaternion = Quaternion.LookRotation(toTarget);
transform.rotation = Quaternion.RotateTowards(transform.rotation, lookRotation, turnRate);
transform.Translate(Vector3.forward * Time.deltaTime);
}
function OnCollisionEnter( hit : Collision)
{
if (hit.gameObject.name == GameObject.Find("Waypoint" + Counter))
{
Counter = Counter + 1;
Debug.Log("We collided");
TargetWaypoint = GameObject.Find("Waypoint" + Counter);
}
}
I noticed that this code: if (hit.gameObject.name == GameObject.Find("Waypoint" + Counter)) { Counter = Counter + 1; Debug.Log("We collided");
TargetWaypoint = GameObject.Find("Waypoint" + Counter);
}
is not executing for some reason. so i figure thats the problem. but i dont know why its not working.
Any help would be nice.
Way points are usually used as invisible nodes of the path.That is player will penetrate through these points.'OnCollisionEnter()' works based on mesh blocking and requires more physics and rigid body dynamics calculations. Use 'OnTriggerEnter()' ins$$anonymous$$d of 'OnCollisionEnter()' and make your all waypoints objects assigned with a box or sphere Collider with 'Trigger' enabled.
Answer by sethuraj · Oct 28, 2013 at 07:19 AM
You are missing a RigidBody component on your player object.Add a RigidBody component to the object which you are attaching this script to.Enable 'IsTrigger' on all your waypoint object colliders.
Use this ('OnTriggerEnter' instead of 'OnCollisionEnter') for detecting the way points.
function OnTriggerEnter( hit : Collider)
{
if (hit.gameObject.name ==TargetWaypoint.name)
{
Counter = Counter + 1;
Debug.Log("We collided");
TargetWaypoint = GameObject.Find("Waypoint" + Counter);
}
}
Tested Working.. Cheers... :-)
Your answer
Follow this Question
Related Questions
Looking for: Simple pathfinding in 2D Tower Defense game with no objects to collide with? 1 Answer
[CLOSED] Can't Move Left Or Right (a/d) In This Movement Script? [CLOSED] 0 Answers
How do I correctly combine behaviors on a base class? 1 Answer
Mobile camera smooth 0 Answers
Limiting the rotation based on the angle of the player mesh 0 Answers