Gravity Direction Change (11104)

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!!!

Many Thanks,

Matthew

http://www.youtube.com/watch?v=Fxuac...eature=related This in essence is what I am attempting to achieve from the game VVVVVV

The link seems to be a bit borked.

fix the youtube link please

4 Answers

4

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:

using UnityEngine;
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);

Only one correction, change Update to FixedUpdate nad Time.deltaTime to Time.fixedDeltaTime. There are functions with fixed time step and are not framerate dependant.

This really helped me out, Thanks!

didnt see the video but to change gravity just need to change its physics.gravity property like this

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

Can you achieve that effect locally on an object? Cause i'm looking to make a planet that you can walk 360 degrees around.

no, if you want that kind of "Gravity" use force for the object outside the planet, i mean, use the Universal Gravity Formula with some tweeks that suits you best another explanation every update add force to the walker depending on how close to the planet is, the vector of the force would be something like the distance between planet and walker, and a force calculated value to be able to keep it "walking" no mather it possision in the planet, good luck gqferbs

@Teague

One of the following lines changes gravity based on whether your game is 2d or 3d.

//3d games
Physics.gravity = new Vector3(0f,-10f,0f);

//2d games
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.

One thing incorrect there, the 'd' in Physics2d needs capitalizing, like so: Physics2D.gravity = new Vector3(0f, 10f, 0f);

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