- Home /
how can i use multiple gravity direction in multiple game objects?
I'm new to unity. I have 2 game objects. First one should have a gravity towards the negative Y direction and the Second game object should have gravity towards negative Z direction.
Physics.gravity = new Vector3(0, -9.81f, 0);
Physics.gravity = new Vector3(0, 0, 9.81f);
I have following 2 code for these 2 game objects. But if the first one is going towards negative Y direction then second one is also going to the negative Y direction instead of going towards the negative Z direction.
Please help me.. Thanks in advance..
Answer by mikelortega · Jun 02, 2016 at 03:03 PM
Physics.gravity is a global parameter that affects all your objects in your scene. You cannot have different gravities for each object. In your code, the second line overwrites the first one.
You should set the gravity to zero and set a different Rigidbody.AddForce to each object to simulate gravity.
Thanks mate! I've done in the same way. Now I've Used AddForce to both of my game objects. :)
Answer by Hellium · Jun 04, 2016 at 04:46 PM
If you don't want to bother with calling AddForce each frame, there is the ConstantForce component ! ;)
Rigidbody.AddForce applies a force to the Rigidbody only for one frame, thus you have to keep calling the function. ConstantForce on the other hand will apply the force every frame until you change the force or torque to a new value.
Your answer
Follow this Question
Related Questions
Trigger enter issue 1 Answer
Game object not falling 1 Answer
How to keep a GameObject inside a collider? 3 Answers
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
GameObject to GameObject gravity 1 Answer