- Home /
Can I make gravity work the other way round for one object?
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!
Answer by duck · Nov 20, 2010 at 02:09 PM
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; } }
let me know how it goes! (and remember to use the upvote and 'accept answer' features)
Hi there, I tested out the script you wrote on a cube with a rigid body attach but it comes up with compiler errors on lines 6 and 8 saying: Assets/Scripts/JavaScripts/NewBehaviourScript 1.js(6,39): BCE0051: Operator '*' cannot be used with a left hand side of type 'UnityEngine.Vector3' and a right hand side of type 'UnityEngine.Vector3'.
I'm sure I'm just doing something daft but I don't know how to fix it :S. Sorry if I'm being really dim!
oops, yes there's an error - it should say "Physics.gravity.magnitude" rather than just "Physics.gravity"
Your answer
Follow this Question
Related Questions
Gravity Direction Change 4 Answers
Changing gravity on collision 4 Answers
How to change gravity with a trigger? 1 Answer
Turning on and off Rigidbody programmatically? 3 Answers
Flip gravity 0 Answers