# Waypoint Setup: How?

 0 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 more ▼ asked Mar 09 '11 at 03:28 PM shawnkilian 376 ● 21 ● 28 ● 41 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 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.. more ▼ answered Apr 15 '12 at 11:21 PM FizzyBear 67 ● 4 ● 10 ● 12 It doesnt work! it says: "Assets/Scripts/Waypoints.js(38,5): BCE0005: Unknown identifier: 'MoveTowards'." Jun 03 '12 at 05:57 PM Estevominador where should I attach this script? and... is the gameobject that you mentioned is the enemy? or just the waypoint? Feb 24 at 11:31 AM chiako add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 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 :) `````` more ▼ answered Jun 18 '12 at 01:55 PM Dee Va 279 ● 4 ● 14 ● 18 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email:

Topics:

x3333
x960
x654
x238
x77

asked: Mar 09 '11 at 03:28 PM

Seen: 3226 times

Last Updated: Feb 24 at 11:31 AM