Suggestions for best way to handle waypoints and physics?

Hi,

I am writing a game where a car travels along rails using waypoints to follow the path of the rails, however if the car goes to fast around a bend for instance, I want it to react to physics and lift/fly off the track and collide with the walls around the track.

I have looked at physics joints? but not sure the right way to go?

Anyone got any better ideas or suggestions on how best to do this?

Thanks in advance!

Are you looking for a Scalextric kind of effect where the car follows a fixed path, but the speed is under the user’s control?

Where is the input coming from? The player, bot ?

First of all you have to figure out how to get the car on rails. Maybe animating a “carrot” in your 3D software and have the car rigidbody.MovePosition() following the carrot. Or with enough waypoints you could just make your way through the vectors, using forces. Your simulation needs to get all the way through.

Second, is the influence (the input). Now that you know how to get a car through your waypoints, it should be easy to add user/bot input that will derail the car into oblivion.

In previous projects, we also had a spline influence physics f(t), until a bigger force smashed the object out of its rail (threshold).

How did you guess? yep that’s exactly what I want, I have it working with cars following a path.

I just need to workout how to handle the physics side of things, so that when the car leaves the track it stops trying to follow the path, and re-acts to collisions.

Do you have a suggestion how to do?

Hi Rozgo,

Yes input is from player.

Do you have any code snippets to help me understand, I am using the seeksteer script for the path/waypoint handling.

Just saw the steering script for the first time. It would’ve been better if physics was handled differently, but nonetheless.

Three ideas:

With enough waypoints you can do point-line distance tests, where the point is the car and the line is the segment (WaypointA,WaypointB), creating a virtual cylinder. If a threshold is crossed, stop calling the Update function of the steer script (or just do enabled = false). The rigidbody will maintain its velocity or you could apply force (or torque) to exagerate.

Again with enough waypoints, you can have them close enough as to have overlapping radius, creating a virtual tunnel. If the car exits the closest radius, go havok.

The other idea is to sample (at a 1/2 second rate) the transform of the rigidbody, in a successful run, say by you. If the user car is farther than a threshold to the nearest sample, go havok.

Hope this helps.

Just saw the steering script for the first time.
It would’ve been better if physics was handled
differently, but nonetheless.

What do you mean? I am happy to drop the seeksteer script, if there is a better way of doing it starting from scratch.

The thing is, depending on how you want to approach things, it would’ve been nice to have simulate physics all the way, with forces, instead of fixing velocities and LookAt, like the script does. This way colliding cars could budge or derail each other realistically.

But, then again maybe not, since you might want to simulate a rigid rail. In this case, you can just have a number on each waypoint, maxSpeed. If the user approaches the waypoint at a speed greater than maxSpeed, than turn of the steering script and apply a torque/force.

I follow you, I can do this! :slight_smile: many thanks!

One last question, with the Seeksteer script you have to assign the waypoint gameobjects via the public waypoints variable via Unity editor.

My track has almost 100 waypoints, how can I set the waypoint objects via the script.

Both examples below don’t work, well the final line of code is the problem?

Transform[] waypoints = new Transform[100];

GameObject wp1 = GameObject.Find("Waypoint_001");
waypoints[0] = wp1.Transform;
Transform[] waypoints = new Transform[100];

GameObject wp1 = GameObject.Find("Waypoint_001");
waypoints[0] = wp1.GetComponent(Transform);

I don’t know if it is too late now, but you might find the attached script useful for laying out the track. Basically, it just draws lines between waypoints specified as an array of Vector3. (Alternatively, you can mark the waypoints using empty GameObjects, but it becomes a nuisance when you have a lot of waypoints.) Also, if you are going to have a maximum speed at each waypoint, then there is an easier way to go than setting them by hand. If you take a waypoint along with the one before it and the one after, you can join them together to make an angle (use Vector3.Angle to find the exact value). You can then just make the maximum speed proportional to the size of the angle. This is more consistent and makes for much less work!

194077–6910–$racetrack_144.cs (599 Bytes)

Many thanks I will take a look at your track code, any chance you know the answer to my code specific question above?

In example 1, the transform property shouldn’t begin with a capital T (that’s the class name, not the property name). In example 2, you can’t use GetComponent with the bare class name in C# - you can use a string containing the name in quotes, though.