- Home /
Change direction of gravity for a specific instance of a prefab
Hi,
I'm very new to unity and scripting in general. What I'm trying to do is create a puzzle game where in certain levels gravity is of a different direction that other objects.
See the following Diagram
....B1B2B3
BLOCKBLOCK
........B4
B1, B2, B3, and B4 are all instances of prefab B. Block is just a stationary Block. B1-3 I would like to have normal gravity, but B4 I would like to have inverted gravity. Obviously the levels will be more complex with gravity going along both axis' for different objects, etc.
What I tried to do was this in update:
if(rigidbody.mass == 3.1313) {
gravity = new Vector3(1f,9.81f,0f);
}
I thought if I changed the mass ever so slightly on those instances, and used that as a unique attribute, it would allow me to change the gravity. This did not work lol. Any help would be greatly appreciated!
Answer by Berenger · Jun 27, 2012 at 03:51 PM
You can't do this simply with the gravity. However, you could simulate the gravity for all your objects by adding a constant force. Those with the regular gravity will have the constant force at (0,-9.81,0), and you can change the other as you like. Just don't forget to disabled 'useGravity' on every rigidbodies.
That worked perfectly! Thank you so much! Such a simple thing, if you knew all the crazy things I tried doing to manipulate gravity...