# 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.

Gravity is just a force in the downwards y direction by default.

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:
}
``````

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.