I've been using unity for some school projects recently, and I made a racing game fairly similar to F-zero GX. It was really unpolished though, and I've got a question about one thing that I thought really needed polish. While the hover car goes along the track, it shoots a ray directly below it and orientates itself so that the car's up is the same as the surface's normal. Gravity is always pulling in the hover cars down direction. The biggest problem I am having with doing this is that with upward curves. Lets say I was doing a loop de loop. The car would move forward by an amount, then orientate itself and move forward again. If you are going fast enough though, you end up moving so far forward that you tap the surface of the loop de loops curve with your ship, making you loose all momentum. Does anyone have any ideas as to what I could do to work around this?
Smoothing out the ray tracing of a surface on a curve,
Moderators - Reputation: 5478
Posted 13 October 2013 - 09:11 AM
Another possibility with the physics engine is to use a ray cast prior to stepping where you figure out the normal at the end position before stepping the physics. Now impart rotational velocity so that over the physics update the ship will automatically rotate to the end orientation such that the car will not be purely purely linear motion straight forward. This would keep the nose from going straight into the road though it still would not solve for the car actually moving fast enough to penetrate.
There are a lot of possibilities ranging from the multiple stepping solutions (probably your best bets) to working around the linear time steps in the physics engine (probably not as good). The thing I see with the physics solutions though is that while you can probably get the rotational velocities to mostly work, it would leave speed control as a function of gameplay such that it works kinda like a slot car track, going too fast and you wipe out, going too slow and you won't make the loop etc.
Some more information on the setup of the code would be most useful to dig into the details better. I.e. which physics engine, if you have an actual curve description you can analyse pre-physics, etc.
Members - Reputation: 159
Posted 13 October 2013 - 02:46 PM
Am I correct in thinking the ship is not orienting fast enough to the track? IE the the ship eases to its next target orientation, and if the ship is blazing past a load of polys with radically different normals, it doesn't adopt its orientation fast enough?
If you are relying on a physics engine and you can't simply increase how fast the ship orients to the normals, there's a few things you could do.
First, increasing the polycount of the collision geometry would help, so would casting a ray from the ship's nose rather than centre point. If those don't work, you could setup a dummy model that is positioned a % of your speed ahead of you on the spline, that governs your ship's orientation. For example if you were going 50mph it would be only half a ship ahead, and if you were going 500mph it would be 5 ships ahead. Then simply copy the dummy model's orientation matrix to the actual player ship.
By the way, how are you handling the hover mechanic?
Edited by rumblesushi, 13 October 2013 - 02:48 PM.
Members - Reputation: 228
Posted 16 October 2013 - 05:14 PM
For the hover mechanic, I shoot a raycast straight down from the center of the ship, if the distance from the start of the ray to where the ray hits is shorter then a specific amount it moves it back up to the minimum distance. As for the physics engine, It seems as though Unity uses the PhysX engine. Unfortunately I can't give you specific pieces of code right now as I am not at my house, but when I return there I will put up some more info on how I am doing everything. I think this first suggestion AllEightUp gave will probably do the trick though! Thanks very much for your input. I'm also thinking of doing something like what rumblesushi suggested, I'm thinking having a raycast at the middle, nose, and back, all checking would probably be best.
Edited by ThePointingMan, 16 October 2013 - 05:17 PM.
Members - Reputation: 159
Posted 20 October 2013 - 11:53 AM
As it happens I use 4 rays to orient my ship to the track. One at the nose and tail to calculate the pitch, and one at either side of the middle of the ship to calculate the roll.
The orientation behaviour is generally perfect, the only problem I've found is not orienting all the way round an F-Zero style tube. You drop off at the sides. I'll need the gravity to be the vector between the ship and the middle of the tube rather than the ship's Y Axis.