Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by FIVESIGN · Jun 19, 2016 at 04:49 AM · 2dgravityscalepowerup

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#. :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FIVESIGN · Jun 19, 2016 at 04:40 PM 0
Share

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

avatar image Dibbie FIVESIGN · Jun 20, 2016 at 01:56 AM 1
Share

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.

avatar image FIVESIGN Dibbie · Jun 20, 2016 at 05:58 AM 0
Share

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!

Show more comments
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

2D Mobile GUI Scaler 1 Answer

Spawning multiple gameobjects with random scales 2 Answers

How to keep upward momentum after reversing gravity 0 Answers

Gradually editing the scale of an object 1 Answer

Scale 2d rigidbody when moved farther "away" (y axis) based on Y position 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges