Hi, I'm trying to make a game where you control a car (a 4 wheel colliders and 1 rigid body setup) and can switch gravity for that car alone.
I'm pretty new to unity and haven't got a clue where to start, I'm also completely useless at scripting (I'm more of a game artist). I thought maybe I could change the mass to a negative value, but unity won't allow it so that didn't work...
Anyway, any assistance at all would be greatly appreciated!
duck
2
Sure, just turn that object's gravity off using .useGravity, eg:
rigidbody.useGravity = false;
Then apply your own equivalent gravity in the direction you want, either using AddForce in FixedUpdate, or by adding a ConstantForce component.
Here's a simple example using the "AddForce" method, which switches gravity when you hit spacea:
private var gravityReversed = false;
function FixedUpdate() {
if (gravityReversed) {
rigidbody.AddForce(Vector3.up * Physics.gravity.magnitude);
} else {
rigidbody.AddForce(-Vector3.up * Physics.gravity.magnitude);
}
}
function Update() {
if (Input.GetKeyDown(KeyCode.Space)) {
gravityReversed = !gravityReversed;
}
}