Hey there everyone!
I'm currently having a problem in Unity figuring out code to change the direction of gravity on a Character Controller. By this I mean the gravity normally is facing downwards, as gravity normally does, so my character can run left, right and jump as I want him to. But I want to be able to change it so the gravity faces upwards so the character can walk and jump on the roof.
Any help of guidance would truly be brilliant and much appreciated!!!
http://www.youtube.com/watch?v=Fxuac...eature=related This in essence is what I am attempting to achieve from the game VVVVVV
It should be fairly easy to do. Instead of using the built-in gravity, just implement your own. You first need to set the Use Gravity to false.
Then, you would just apply this script to your objects:
public class ReversibleGravity : MonoBehaviour
float gravity = -9.8f;
void Update ()
rigidbody.velocity.y += gravity * Time.deltaTime;
public void ReverseGravity()
gravity = -gravity;
The slightly harder part would be orienting your character correctly without it "snapping" to the new gravity orientation. I would use spherical interpolation on the Up vector of your objects, like so:
Vector3 targetUp = new Vector3(0, -1, 0);
float damping = 8;
transform.up = Vector3.Slerp(transform.up, targetUp, Time.deltaTime * damping);
didnt see the video but to change gravity just need to change its physics.gravity property
Physics.gravity = new Vector3(1f,-9.81f,0f);
this make a gravity same on y but changes on x axis, just play a little with the values and there you go
hope this helps you
One of the following lines changes gravity based on whether your game is 2d or 3d.
Physics.gravity = new Vector3(0f,-10f,0f);
Physics2d.gravity = new Vector3(0f,10f,0f);
Once you change the gravity in your code, you could also set a boolean to false. isGravityNormal could be the boolean variable name.
Then, in your Jump() function you could check that boolean in order to determine which way to jump upwards or downwards.
The answer is most simple - you do not create your own gravity settings, you can just modify the direction using this:
Changing the direction of Gravity using the built-in gravity system in Unity3D