Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Sadakos Boyfriend · Apr 19, 2011 at 09:34 PM · gravity

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?

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 Bunny83 · Apr 19, 2011 at 09:45 PM 1
Share

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.

5 Replies

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

Answer by Bunny83 · Apr 19, 2011 at 09:43 PM

Sure, and normally the error messages tells you what is wrong...

  1. You have a semicolon after your if statement, remove it
  2. 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;
}
Comment
Add comment · Show 4 · 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 Justin Warner · Apr 19, 2011 at 09:47 PM 0
Share

Still only effects one object =P haha, but yeah, I couldn't figure out why it was pointing there XD.

avatar image Sadakos Boyfriend · Apr 19, 2011 at 09:59 PM 0
Share

Thank you.

Yea, i feel a bit dumb for missing that...but, still new, still learning :)

avatar image thefourthpilot · Jun 25, 2014 at 03:58 AM 0
Share

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?

avatar image Bunny83 · Jun 25, 2014 at 10:51 AM 0
Share

@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.

avatar image
2

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.

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 Sadakos Boyfriend · Apr 19, 2011 at 10:00 PM 0
Share

I'll give that a go, thank you :)

avatar image
0

Answer by WILEz1975 · Sep 03, 2013 at 04:14 PM

Physics.gravity apply the gravity to all rigidbody in the scene...

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

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 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);

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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


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