• 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
0
Question by SirWaffleGamin · Sep 16, 2015 at 06:25 PM · physicsgravity

How to change the position for gravity?

I am creating a 3D game where the player can push a button, and "position" of gravity will be shifted. To make it any clearer, if I press T in the game, all objects and including the character will move upwards because gravity has been re-positioned so that the ceiling is now the new floor or the level. Same thing with the walls. This is what I have been using so far but it isn't workng:

 private void Start()
 {
       ...
      Physics.gravity = new Vector3(0, -1.0f, 0);
 }
 
 private void InputManager()
 {
      if (Input.GetKeyDown("T"))
      {
            Physics.gravity = new Vector3(0, 1.0f, 0);
      }
 }

Is there another function I should be using or is the solution more complicated than this? The camera isn't an issue by the way.

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 DiegoSLTS · Sep 18, 2015 at 06:29 PM 0
Share

It's called gravity force, not position. $$anonymous$$aybe you'll find better answers looking for that.

Also, you have an Input$$anonymous$$anager method that is private. Unless you're calling that method somewhere else in the same script or with a Send$$anonymous$$essage line, the code that modifies gravity will never be executed.

1 Reply

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

Answer by Nikunj-Kareliya · Sep 16, 2015 at 07:48 PM

Try this sample code :

 using UnityEngine;
 using System.Collections;
 
 public class ChangeGravity : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
         Physics.gravity = new Vector3(0, -1, 0);
         GetComponent<Rigidbody> ().drag = .5f;
     }
     
     // Update is called once per frame
     void Update () {
     
         if(Input.GetKeyUp(KeyCode.UpArrow)) {
             Physics.gravity = Vector3.up * 5;
         }
         else if(Input.GetKeyUp(KeyCode.DownArrow)) {
             Physics.gravity = Vector3.down * 5;
         }
 
 
     }
 }
 

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 SirWaffleGamin · Sep 18, 2015 at 06:02 PM 0
Share

But then how would I apply this to a specific object? Is it the same as linking the object in the editor to the specific code, or do I have to link this script to the script of my character?

avatar image Nikunj-Kareliya SirWaffleGamin · Sep 19, 2015 at 05:26 AM 0
Share

It's sample code for your requirement. $$anonymous$$ake a new scene, create any 3d object, add Rigidbody then drag this script on that object. $$anonymous$$ake sure, you changed default gravity to (0, 0, 0) in Projects Settings > Physics.

This is just for a test. $$anonymous$$odify it as per your need.

avatar image SirWaffleGamin Nikunj-Kareliya · Sep 21, 2015 at 06:05 PM 0
Share

Thank you very much!

avatar image DiegoSLTS · Sep 18, 2015 at 06:26 PM 0
Share

@SirWaffleGa$$anonymous$$ Unity's physics engine applies the gravity force to every rigidbody that uses gravity (the checkbox in the rigidbody inspector). You can't make different objects to have a different gravity force unless you do all the physics by yourself.

avatar image SirWaffleGamin DiegoSLTS · Sep 21, 2015 at 06:06 PM 0
Share

Understood. Thanks!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Re-orienting Player after changing direction of Gravity 0 Answers

Hi, I am trying to detect if a rigidbody has an object below it. 1 Answer

IsGrounded causing problems. 2 Answers

Rigidbody doesn't fall when platform below it shrinks to nothing. 1 Answer

How to get animated sprite to adhere to gravity? 2 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