- Home /
Gravity value changes, has no effect.
I have a simple scene. A box that acts as a button with a simple onMouseUp function on a javascript attached. I have another box, rigidbody with no scripts. When the scene starts the rigidbody drops and I want the 'button' to reset Unity's physics to something else (to pull object in another direction) like so;
function Start () {
Physics.gravity = Vector3(0,0,0);
}
function Update () {
Debug.Log(Physics.gravity);
}
function OnMouseUp()
{
Physics.gravity = Vector3(0,0,-100);
}
With this setup nothing happens. A debug.log tells me the gravity does indeed change though. When I remove the start function however, the box drops (as it now has default Unity gravity) and when I click the button gravity changes and it gets pulled away.
I'm guessing that the starting gravity value doesn't like to be changed. If I manually change the gravity in the inspector to 0,0,0 then I have the same issue, no new gravity value affects the rigidbody at all IF the starting value is 0,0,0.
Any clues as to what's going on? Thank you!
EDIT I should point out that the desired effect here is to have an object suspended in space until a gravity force is applied to the world.
Answer by MikeErty · Sep 26, 2013 at 03:16 PM
Solved it - Partly my fault for not knowing that the rigidbodies fail to wake up if gravity changes and they are already at rest.
To summarise for anyone else who confuddles themselves like me - If you set the starting gravity to 0,0,0 to suspend your objects, they are asleep as soon as the game starts. If you change the gravity they will not respond because they are asleep!
So, I looked up how to force them to wake up and wrote a script that would check if the gravity value changes and then wake them up every time it does. Works as I had hoped :)
Answer by sumeetkhobare · Sep 26, 2013 at 02:39 PM
What i read in your question seems to be quite simple..
You set the gravity to zero at start.. all directions.
Later on, you set it to Vector3(0,0,-100) on a MouseUp event. Well my friend, as you said that the object(box here) gets pulled away seems to be correct. as you set the gravity in the negative z-axis' direction..
One more thing, as you said it is getting pulled away, i think, your camera must be looking in negative z-axis direction, so it is getting pulled away for you. Hope this helps.. and if it does. mark the question as solved.
Thank you for the reply. However I may have misled you with what my problem was - I do want it to get pulled away in that direction, so that wasn't the issue. The issue was that it would not get pulled in any direction at all if I set gravity to 0,0,0 to start with. I did solve my own problem though with a bit of luck, i'll solve it in a sec! Thanks again for looking though, I appreciate it.
Your answer
Follow this Question
Related Questions
Movement on a tube 1 Answer
Need help on my script to calculate gravity for a mass 1 Answer
Model tips over for no reason 1 Answer
shrinking according to mass 0 Answers
Check if 2D Player is grounded with Physics2D.Linecast 1 Answer