Changing gravity at the click of a button...

I'm trying to write a script that will allow my character to invert gravity when a button is pressed, effectivly pulling him up to the celing:

function Update ()
{
    if(Input.GetButtonDown("Fire1"));
    {
        Physics.gravity = (0, 10, 0);
    }
}

But all i'm getting is error messages :(

Halp?

If you post code snippets mark it as code. Just highlight the code and press the "101010" button. Also format the code in a common way so others can read it.

6 Answers

6

Sure, and normally the error messages tells you what is wrong...

  1. You have a semicolon after your if statement, remove it
  2. Just putting some numbers between 2 brackets don't form a Vector3 struct...

Here's the script:

function Update ()
{
    if(Input.GetButtonDown("Fire1"))
    {
        Physics.gravity = Vector3(0, 10, 0);
    }
}


If you plan to toggle the gravity when you press the button it's easier to multiply the gravity by (-1)

if(Input.GetButtonDown("Fire1"))
{
    Physics.gravity *= -1;
}

Thank you. Yea, i feel a bit dumb for missing that...but, still new, still learning :)

Hi, I have a question. Is the script in C# or Java? Also, when I use the exact same script nothing happens. How do I apply it to the character?

That command only effects one object (And in this case, the object in which the script is connected to).

So, you'd have to loop through all object in your scene, and make them all change their gravity... Or of what items you want to be effected...

var speed = 10;
function Update ()
{
    if(Input.GetButtonDown("Fire1"))
    {
        if(speed > 0)
        {
            Physics.gravity = Vector3(0, speed, 0);
            speed = -10;
            moveAll();
        }
        else
        {   
            Physics.gravity = Vector3(0, speed, 0);
            speed = 10;
            moveAll();
        }
    }
}

function moveAll()
{    
    var objectsToEffect = GameObject.FindGameObjectsWithTag ("ObjectToEffect");
    for (var object in objectsToEffect) {
            object.Physics.gravity = Vector3(0, speed, 0);
    }
}

Put the tag on every object you want to effect as ObjectToEffect.

Have fun.

I'll give that a go, thank you :)

Physics.gravity apply the gravity to all rigidbody in the scene…

Yes - although this was answered back in 2011, it’s still high in Google results for “unity change gravity”. The code posted above by Justin is incorrect and not required - the code from Bunny83 is the correct code to be using. Physics.gravity only needs to be set once to make the change for all objects in the current scene.

This doesn’t work for me. are you sure it’s not,

Physics.gravity = new Vector3(x,x,x);

It's because the OP (and Bunny83's correct response) was written UnityScript. You're probably using C#, like most people, and so yes you want Physics.gravity = new Vector3(x, x, x); Or just do Physics.gravity = Physics.gravity * -1; // invert existing gravity

You may have to do your gravity manipulation (see Bunny83’s answer) in the “LateUpdate()” method.

I was trying it in other methods in my mobile game and it was acting weird and not changing right away. Tonight I tried doing the "Physics.gravity = " statement in “LateUpdate()” and that works perfectly.

What I do is in my other methods, when I know I want gravity changed, I set a boolean variable to True. Then in “LateUpdate()” I check that bool, and if it’s true, do the manipulation, then set it to False.