- Home /
How to keep object moving over mesh
Hey there!
So I'm currently in the process of creating a "hover" racing game. I say "hover" because it's really an invisible sphere collider rolling around with a car mesh "floating" in the center. I've tried doing it without a sphere collider and only using the car object (actually floating with AddForce upwards and gravity downwards), but I couldn't get it to work well with collisions.
Everything has been going great so far. Movement, turning, sliding, boosting, drifting, etc. The one thing that I'm having trouble with is how to keep the sphere collider "magnetized" to the track.
My current Gravity code is this:
//Gravity
sphere.AddForce(-carNormal.transform.up * gravity, ForceMode.Acceleration);
This allows the car to be pulled to whatever "carNormal" is aligned to. I'm using this for the Normal rotation:
RaycastHit hitOn;
RaycastHit hitNear;
Physics.Raycast(transform.position + (transform.up * .1f), -carNormal.transform.up, out hitOn, 1.1f);
Physics.Raycast(transform.position + (transform.up * .1f), -carNormal.transform.up, out hitNear, 2.0f);
//Normal Rotation
carNormal.up = Vector3.Lerp(carNormal.up, hitNear.normal, Time.deltaTime * 8.0f);
carNormal.Rotate(0, transform.eulerAngles.y, 0);
So a ray is being cast downwards tangental to the car, and if it the raycast hits an object, align the "up" axis to the hit normal. Pretty straightforward to me. However, I'm running into issues now where for some reason, the forward acceleration tanks when driving at a steep incline, as well as the inability to drive upside-down.
I'm unsure where to go from here, or if there's an easier way to kind of snap the sphere collider to the track until it can't detect a track in its proximity (like going off a jump, or something), which a true downwards force would be applied. Anyone have any advice on this? Thanks!
Your answer
Follow this Question
Related Questions
vehicle not accelerating after braking 2 Answers
How to play sound when object stops moving? 1 Answer
vehicle moveing on the terrain 0 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
YES/NO Can I implement a bouncing effect on my vehicle using physics joints 0 Answers