- Home /
Script for alternative gravity?
I’m trying to make a puzzle game where, depending on the color of the object, it is pulled towards a different wall in the room. (I don’t need to test for the color, I’m doing that part manually.) For example, green objects are affected by normal gravity, while blue ones are pulled in the negative x direction.
I need a script I could apply to any object to replicate gravity in a specific direction, such as a BlueGravity script for mimicking gravity in the x direction.
I can’t just set gravity to that direction, as various colors will be present simultaneously.
I would need to replicate an acceleration due to gravity, as well as replicating a collision correctly (right now, my failed attempts have the object still accelerating while colliding with a wall, so that when the wall disappears it continues at high speeds rather than resetting from zero like it should).
The cubes also need to be able to be carried and activate pressure switches, but that’s for another time and not the focus of this question.
How would I be able to do this? I need a BlueGravity and RedGravity (z direction) script, although I can simply make one based on the other. Green gravity can just use Unity’s gravity, I don’t need to over complicate at the moment.
Answer by Kciwsolb · May 02, 2018 at 04:08 PM
Gravity is just a force in the downwards y direction by default.
See here: https://docs.unity3d.com/ScriptReference/Physics-gravity.html
You could just turn off "use gravity" on your Rigidbodies and manually apply a continuous force in what ever direction you want depending on the color. If you want them to "fall" in the global negative x direction, then just:
private void FixedUpdate()
{
//if some color condition:
rigidbody.AddForce(-9.81f, 0.0f, 0.0f);
}
I won't deny that it would work, however adding scripts to hundreds of objects that have to access another script to get direction. Then add the force, seems like it could seriously slow down your game. I believe the easiest solution is just to change their gravity.
Answer by JxWolfe · May 02, 2018 at 05:05 PM
I had a similar idea.
Physics.gravity = new Vector3 (0, -9.81f,0);
the Vector3 is direction and force. ((0, -9.81f,0) is normal gravity)
2D gravity requires different code
Physics2D.gravity = new Vector2 (0, -9.81f);
Hope you enjoy playing with the gravity features. :)
If you want to edit the original gravity that unity has to begin with you can find it at:
Edit>ProjectSettings>Physics (or Physics2D)
Tell me if it doesn't work
Physics.gravity is the gravity applied to ALL Rigidbodies in a scene.
https://docs.unity3d.com/ScriptReference/Physics-gravity.html
The user wants to apply different "gravity" to different objects at once. As he put it:
I can’t just set gravity to that direction, as various colors will be present simultaneously.
So, just changing Physics.gravity will not suffice. I do not see an alternative and since gravity is basically just a force being applied to a Rigidbody to begin with, I do not think this will result in much performance overhead compared to normal gravity.
$$anonymous$$y answer will probably prevent the Rigidbody from going to sleep. If you were worried about that (if you have LOTS of objects with this script) you could check velocity yourself and put it to sleep if it's below a threshold for a certain amount of time.
Your answer
Follow this Question
Related Questions
How To Set Individual Rigidbody Gravity [Solved] 3 Answers
rigidbody.useGravity doesnt work? 1 Answer
rigidbody.Use gravity not working from script 0 Answers
Horizontal gravity on One gameobject 2 Answers