Change Gravity on falling objects
Hello dear unity community!
Please help me out here, I'm going crazy! :)
I am making a simple Catch and Fall Game. Now I'm trying to put some "PowerUps" in the Game.
I want to create a Button with an Hourglass on it, if the Player pushes the button all of the falling objects will change the gravity from (-1.8 to -2.0) ... this will stay for about 5 seconds and then the normal gravity sets in again.
I already have made an other PowerUp that destroys all of the objects in the scene at a push of a button.
This is the code:
#pragma strict
public var object : GameObject;
var gameObjects : GameObject[];
var points:int;
function FindObject() {
gameObjects = GameObject.FindGameObjectsWithTag ("ice");
for(var i = 0 ; i < gameObjects.length ; i ++)
Destroy(gameObjects[i]);
FindObject2();
}
function FindObject2() {
gameObjects = GameObject.FindGameObjectsWithTag ("beach") ;
for(var i = 0 ; i < gameObjects.length ; i ++)
Destroy(gameObjects[i]);
GameObject.Find( "Canvas" ).GetComponent( Punkte ).points += points;
object.SetActive(false);
}
How can I instead of destroying the gameobjects change the gravity scale? All of my attempts are not working ... I don't get it.
Appreciate your help already! And if it is possible please use Unityscript, I'm not familiar with c#. :)
Answer by Dibbie · Jun 19, 2016 at 07:24 AM
Well, if your objects have a Rigidbody, you can increase each objects Rigidbody's force or velocity, or change the entire games world gravity. If your game is a 2D game, then youd be using "Rigidbody2D" or "Physics2D" instead.
Thank you Dibbie ... this helped ... I didn't thought of the entire world gravity.
I managed to make the gravity change ... but how can I get it back to the normal value?
This is my Code, and I want that after 5 seconds the gravity gets -10 again.
#pragma strict
public var object : GameObject;
var gameObjects : GameObject[];
var points:int;
var timer:float = 0.0;
function GravityOn() {
GameObject.Find( "Canvas" ).GetComponent( Punkte ).points += points;
Physics2D.gravity = Vector3(0, 0, -10.0);
}
function Gravity() {
Physics2D.gravity = Vector3(0, 0, -2.0);
timer += Time.deltaTime;
if(timer > 5)
{
GravityOn();
}
}
Appreciate your help a lot!!!!!
It doesnt seem like you have any trigger for "Gravity" - something would need to call it, be it Update, or some game event, from the looks of it, youll probably want to call "Gravity" in "Update".
Another thing to note, "timer > 5" means after 6 seconds technically, because 5 is not greater than 5, its equal to 5, so itll run again. You could say "timer > 4" or "timer >= 5" for exactly 5 seconds if that matters to you.
And a final thing - you should want to reset "timer" to 0 again at some point, probably after "GravityOn" is called.
Thanks Dibbie ... I'm absolutely new to coding and not yet able to fully understand the logic. :)
The Trigger would be a "Button". In Fact I have a Hourglass Symbol. If the player pushes the button, the gravity changes to -2.0 and then after 5 seconds back to -10.0.
What I've achieved with your help is that the gravity sets in as wanted ... but I don't get it how I can put the gravity back to -10.0.
#pragma strict
public var object : GameObject;
var gameObjects : GameObject[];
var points:int;
var timer:float = 0.0;
function Gravity() {
Physics2D.gravity = Vector3(0.0, -3.0, 0.0);
GameObject.Find( "Canvas" ).GetComponent( Punkte ).points += points;
Update();
}
function Update () {
timer += Time.deltaTime;
if(timer > 10)
{
GravityOn2();
timer = 0.0;
}
}
function GravityOn2() {
Physics2D.gravity = Vector3(0.0, -10.0, 0.0);
object.SetActive(false);
}
This script is attached to a Button.
I've tried with WaitForSeconds and with a timer but i don't get it.
Thank you! Appreciate your time and help a lot!