x


How to make waypoints

Hi In the game were making we need to know how to make way points in unity. The only problem is that were all noobs and don't know how to. Please help.

more ▼

asked Nov 25 '10 at 09:00 PM

GroundHOG-2010 gravatar image

GroundHOG-2010
1 1 1 1

(comments are locked)
10|3000 characters needed characters left

8 answers: sort voted first

Hahah, Class!

Okay, well here is what I use, all it does it allows enemies to follow a waypoint one after another

var waypoint : Transform[];
static var speed : float = 5;
private var currentWaypoint : int;


    function Update () 
    {
        if(currentWaypoint < waypoint.length)
        {
            var target : Vector3 = waypoint[currentWaypoint].position;
            var moveDirection : Vector3 = target - transform.position;
            var velocity = rigidbody.velocity;

            if(moveDirection.magnitude < 1)
            {
                currentWaypoint++;
            }
            else
            {
                velocity = moveDirection.normalized*speed;
            }
        }

        rigidbody.velocity = velocity;
    }

This goes in your enemy!

This code below, goes into a game empty:

// Draw the waypoint pickable gizmo
function OnDrawGizmos () {
    Gizmos.DrawIcon (transform.position, "Waypoint.tif");
}

Now with that, what you do is create a game empty on your scene, then drag that script onto with. Where ever this point is, the enemy will walk to it. You can have more than one waypoint. Just means the enemy will walk to one waypoint, then to the other

more ▼

answered Nov 25 '10 at 09:08 PM

oliver-jones gravatar image

oliver-jones
3.6k 239 283 333

BTW -- the Waypoint.tif is just an image that is placed over the top of your waypoint - so you can see it. To get yours working, create a file in your project called 'Gizmos', then place a simple small image in there, then call it Waypoint.tif

Nov 25 '10 at 09:21 PM oliver-jones

The "rigidbody.velocity = velocity;" line needs to go inside the closing brace } above it -- otherwise velocity is not defined. Normally wouldn't comment on something so obvious, but there is an incorrect answer that could misslead folks -- will comment on that also.

Jan 27 '13 at 03:22 AM shopguy
(comments are locked)
10|3000 characters needed characters left

There are tons of different ways.

The solution above may work for you. Personally I prefer using a spline path (Bezier curves faded together, basically; the forum has a few implementations available) which represents the optimal drive line. You then have each car attempt to match their rotation to the path's direction at the closest point to the car.

more ▼

answered Nov 25 '10 at 09:13 PM

Loius gravatar image

Loius
13.3k 11 26 61

(comments are locked)
10|3000 characters needed characters left

Console says (unknown identifier: "velocity") Is says this for last line of code on the first script (rigidbidy.velocity = velocity;)

more ▼

answered Jul 18 '11 at 07:40 PM

Kota gravatar image

Kota
-4 4 5 6

I deleted that last line but when I ran the game the cylinder that had the script attached to it didn't move

Jul 18 '11 at 08:04 PM Kota
(comments are locked)
10|3000 characters needed characters left

just replace rigidbody.velocity = velocity;

in rigidbody.velocity = speed;

more ▼

answered Jul 18 '11 at 08:21 PM

devilkkw gravatar image

devilkkw
317 9 10 19

This is incorrect, please see the comment I left on that answer -- line just needs to move up a bit. If you used speed, object would move at the same speed, same direction/etc.. would not head towards waypoint at all.

Jan 27 '13 at 03:24 AM shopguy
(comments are locked)
10|3000 characters needed characters left

Hey! is there anything i can add too this script so my enemy/npc turns(rotates) too the next waypoint at the moment he only slide the wrong way to the next way point. Thanks!

more ▼

answered Sep 12 '12 at 09:54 PM

Tuben gravatar image

Tuben
0

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1256
x511
x220
x146

asked: Nov 25 '10 at 09:00 PM

Seen: 18426 times

Last Updated: Jul 31 '13 at 08:48 PM