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.
Answer by Bunny83 · Apr 19, 2011 at 09:43 PM
Sure, and normally the error messages tells you what is wrong...
- You have a semicolon after your if statement, remove it
- 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;
}
Still only effects one object =P haha, but yeah, I couldn't figure out why it was pointing there XD.
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?
@thefourthpilot:
First of all Unity doesn't use Java. It uses UnityScript which is a JavaScript like language but has nothing to do with Java. The example is written in UniyScript because the OP used it. Personally i only use C#
Second: this script just have to be attached to any object in the scene.
Third: This script changes the Physics system's gravity. If you use for example the CharacterController it won't affect it since the CC don't use the Physics system. Only Rigidbodies are affected.
The CharacterController doesn't have any gravity by default. It's usually "simulated" inside of the controlling script (Character$$anonymous$$otor, ...). If you want to change the gravity of the CC you have to edit the script which simulates the gravity.
Note: The CharacterController can only be rotated wround the y axis. So you can't for example turn it 90° to walk a wall upwards. If you plan such a game, you should use a Rigidbody ins$$anonymous$$d.
Answer by Justin Warner · Apr 19, 2011 at 09:44 PM
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.
Answer by WILEz1975 · Sep 03, 2013 at 04:14 PM
Physics.gravity apply the gravity to all rigidbody in the scene...
Answer by dazman76 · Sep 26, 2013 at 11:32 PM
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.
Answer by FalsAlarm · Dec 24, 2016 at 11:50 PM
This doesn't work for me. are you sure it's not,
Physics.gravity = new Vector3(x,x,x);
Your answer
Follow this Question
Related Questions
Gravity seems "slow". 5 Answers
Want to set differnt gravity for differnt place in one project. 1 Answer
gravity shift 0 Answers
Counteract gravity 2 Answers
I need to create artificial gravity ( like a fake fake centrifugal effect) 2 Answers