- Home /
Waypoint Setup: How?
Can anyone please explain how to set up a proper waypoint system? I want my enemy to patrol along a given waypoint path and if I get within range of the enemy it will attack me. If I run out of range of the enemy, it should go back to it's patrol, or at least the nearest waypoint.
I've been playing with example projects and scripts from all over the place and still no luck. Any help is greatly appreciated. Thank you!
Shawn
Answer by FizzyBear · Apr 15, 2012 at 11:21 PM
Create an empty gameobject, name it waypoint, duplicate it, drag it and make it a child of the waypoint. Position them where ever. Ta da. You got your waypoint.
For the code however.. I'd do something like this..
// speed of the AI player
public var speed:int = 4;
// speed the ai player rotates by
public var rotationSpeed:int = 5;
// the waypoints
public var waypoints:Transform[];
// current waypoint id
private var waypointId:int = 0;
/**
Patrol around the waypoints
*/
function Patrol()
{
// if no waypoints have been assigned
if (waypoints.Length == 0)
{
print("You need to assign some waypoints within the Inspector");
return;
}
// if distance to waypoint is less than 2 metres then start heading toward next waypoint
if (Vector3.Distance(waypoints[waypointId].position, transform.position) < 2)
{
// increase waypoint id
waypointId++;
// make sure new waypointId isn't greater than number of waypoints
// if it is then set waypointId to 0 to head towards first waypoint again
if (waypointId >= waypoints.Length) waypointId = 0;
}
// move towards the current waypointId's position
MoveTowards(waypoints[waypointId].position);
}
/**
Update - Every Frame
*/
function Update()
{
// Patrol!
Patrol();
}
Now you got your guy going from 1 waypoint to the other..
It doesnt work! it says: "Assets/Scripts/Waypoints.js(38,5): BCE0005: $$anonymous$$ identifier: '$$anonymous$$oveTowards'."
where should I attach this script? and... is the gameobject that you mentioned is the enemy? or just the waypoint?
Answer by Tuck · Mar 09, 2011 at 07:38 PM
Do you want the enemy to chase after the player when the player is in range or just stop moving along the path and start shooting? Implementing path following AI is easy. Having enemy AI chase the player intelligently is less so.
Update: The simplest way I've found to make a path following AI requires three parts: an enemy object, a parent path object, and individual path node objects.
The enemy has a public game object that you can assign in the editor to the path object you want it to follow, and a function that moves it towards a given position. In the update function of the enemy have it move toward the position of a node in the list, and upon reaching it have it move to the next node.
The parent path object needs a public list that will contain all nodes in the path and a function that sorts the list based on an int inside of the node class.
The node object needs a public int (as mentioned above) that determines its order in the path. It also needs a function that adds it to the parent object's list of nodes to be called on start.
I realize that's a complicated explanation. I'm tempted to just make a sample project and upload it. I dunno, let me know how it goes.
Lets just go with AI moving from waypoint to waypoint. I don't even know how to create a waypoint. What scripts are involved? Please help. I appreciate it! Thank you for your repsonse.
$$anonymous$$
Alright I updated my post with the logic for a path following system. A waypoint in this case is just a cube object or something with a script attached.
Answer by Dee Va · Jun 18, 2012 at 01:55 PM
If it says: "Assets/Scripts/Waypoints.js(38,5): BCE0005: Unknown identifier: 'MoveTowards'."
add this to your script
function MoveTowards (position : Vector3) {
var direction = position - transform.position;
direction.y = 0;
if (direction.magnitude < 0.5) {
SendMessage("SetSpeed", 0.0);
return;
}
}
hope it works :)
Answer by filip0258 · Mar 12, 2014 at 10:18 AM
function MoveTowards (position : Vector3) { var direction = position - transform.position; direction.y = 0; if (direction.magnitude < 0.5) { SendMessage("SetSpeed", 0.0); return; }
} hope it works :)
Your answer
Follow this Question
Related Questions
2D enemy Field of Vision script 1 Answer
Need Help With My AI Script 1 Answer
AI script troubles 1 Answer
Enemy AI things to consider 1 Answer