• 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 /
avatar image
1
Question by DubstepDragon · Jun 07, 2013 at 08:39 PM · c#gamegravityswitch

Changing gravity direction...

I have a simple script in C# that changes gravity vertically on the press of a button. Here it is:

 using UnityEngine;
 using System.Collections;
 
 public class GravitronThingy : MonoBehaviour {
     
     public bool Gravitronned = false;
     
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         
         if(Gravitronned = true && Input.GetKeyDown(KeyCode.Q)) {
             Physics.gravity *= 1;
         }
         
         if(Input.GetKeyDown(KeyCode.Q)) {
             Physics.gravity *= -1;
             Gravitronned = true;
         }
     }
 }
 

How would I switch from side to side? how about front to back?

Thanks in advance!

Comment
Add comment · Show 1
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 DubstepDragon · Jun 07, 2013 at 08:42 PM 0
Share

Also the script stops working when the objects in the scene are still... :( any help with that?

5 Replies

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

Answer by clunk47 · Jun 07, 2013 at 08:44 PM

Just store Physics.gravity in a temporary variable, then have it always be equal to that variable (Vector3). Then change the x, y, and z values of your gravity variable.

 Vector3 gravity;
 
 void Start()
 {
     gravity = Physics.gravity;
 }
 
 void FixedUpdate()
 {
     Physics.gravity = gravity;
     
     if(somethingIsTrue)
     {
         gravity.x = 0;
         gravity.y = -25;
         gravity.z = 0;
     }
 }

Comment
Add comment · Show 3 · 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 sparrow · Nov 06, 2013 at 10:46 PM 2
Share

This is a very helpful answer, there is only one thing. Whenever manipulating with physics you should do it in FixedUpdate funcation and not Update. Also if you plan on using Time.deltaTime in FixedUpdate, change that to Time.FixedDeltaTime. Documentation is very thorough if you require deeper explanation

avatar image clunk47 · Nov 06, 2013 at 10:56 PM 1
Share

Thanks for the additional information, +1.

avatar image icecold043 · Aug 10, 2020 at 07:29 PM 0
Share

Hello! Your comment seems to be very helpful, however it doesn't work for me, the vector 3 updates, but the gravity doesn't change at all, is there something else I need to do ? Thanks!

avatar image
2

Answer by Lovrenc · Jun 07, 2013 at 08:44 PM

In unity you have (as you already know) Physics variable gravity that directs direction and force of gravity effect. It is simply a vector. So you can achieve all the effects you mention above by simply changing that vector to your needs. So to have it go left (assuming -X goes to your left)

 Physics.gravity = new Vector3(-1.0f, 0, 0);

would do your job.

Comment
Add comment · Show 1 · 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 RealSoftGames · Apr 14, 2017 at 03:35 AM 0
Share

does this effect the games gravity or an individual objects gravity though?

avatar image
1

Answer by Dave-Carlile · Jun 07, 2013 at 08:45 PM

Unity Gravity is a vector. The vector can point in any direction. You're interested in the up/down, left/right, forward/back, which happen to correspond to the 3 axes.

Up and down use the y axis. Left and right use the x axis. Forward and back use the z axis.

The Vector struct in Unity has properties that correspond to these axis directions.

Vector3.forward, Vector3.up, and Vector3.right.

So to set your gravity to the right, set it to Vector3.right * gravityAmount, where gravityAmount is the amount of force. To set it to the left, use -Vector3.right * gravityAmount. The - reverses the right direction so it points left. You can use this same method for the other directions.

Comment
Add comment · 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
1

Answer by Seizure · Jun 07, 2013 at 08:50 PM

First it seems you have some logic error where: If(Gravitronned = true .. it needs ==

Second, your gravity is going to start out inversed which may be what you want, but where your going to have trouble is when you switch to normal gravity, and then back, because you will need to add Gravitronned = false in your first if statement. I hope that helps!

Comment
Add comment · 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
0

Answer by K0ST4S · May 24, 2019 at 05:38 PM

Use force over lifetime module, with a constant.

Comment
Add comment · 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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

3D Gravity towards one object 3 Answers

Unity: C#: Eyes look at mouse: HELP!! 3 Answers

Errors with gravity switching... 1 Answer

Inverted gravity makes character walk on the wrong side of the platform 2 Answers

About to start a new project, need help... 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges