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?
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.
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.
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 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!
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.