1
\$\begingroup\$

I'm trying to figure out how script gravity reversal on collision with an object. How can I reverse gravity?

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Are you wanting to invert gravity for that single item that collides or are you wanting to do it for the entire scene? \$\endgroup\$ – MichaelHouse Dec 9 '14 at 22:38
  • \$\begingroup\$ trying to reverse gravity for the single item \$\endgroup\$ – Carson Dec 10 '14 at 13:40
  • \$\begingroup\$ since we're here, I am very, very new to scripting. what does Invalid token '-given characters' in class, struct, or interface member declaration. \$\endgroup\$ – Carson Dec 10 '14 at 23:59
5
\$\begingroup\$

If it's 2D then you can simply use: rigidbody2D.gravityScale = -1;. You tagged the question 2D so this should be all you need to do. To toggle isntead of simply set you can change it to rigidbody2D.gravityScale *= -1;

If it's 3D and you want the entire scene to have gravity reversed you can do: Physics.gravity = new Vector3(0, -1.0F, 0);

If it's 3D and you want only that single object to have its gravity reversed you'll have to manually do something like @SteakOverflow points out in his answer. I believe what you'll have to do is add a constant rigidbody.AddForce call in your FixedUpdateFunction.

\$\endgroup\$
5
  • \$\begingroup\$ If that did the trick for you don't forget to upvote/mark as answer. Also, in the future you'll get more help if you can be more descriptive and add more details to your questions such as what you've tried, what worked, what didn't, etc... \$\endgroup\$ – McAden Dec 11 '14 at 0:11
  • \$\begingroup\$ since you're here, well, first of all, I only have 2 points... I cant rate yet. anyways, what does Invalid token '-given characters-' in class, struct, or interface member declaration mean? \$\endgroup\$ – Carson Dec 11 '14 at 0:52
  • \$\begingroup\$ correction, 1 point \$\endgroup\$ – Carson Dec 11 '14 at 0:55
  • \$\begingroup\$ That would be a syntax error. You're probably missing a quote around a string. \$\endgroup\$ – McAden Dec 11 '14 at 4:30
  • \$\begingroup\$ @Carson You can accept the answer now mate \$\endgroup\$ – John Demetriou Jan 13 '18 at 9:02
2
\$\begingroup\$

You should try to disable the rigidbody's useGravity property when the collision happens and use AddForce to fake your upwards gravity, pretty much like the example in AddForce documentation.

\$\endgroup\$
4
  • \$\begingroup\$ but would the player still be able to jump? \$\endgroup\$ – Carson Dec 10 '14 at 13:41
  • \$\begingroup\$ If you invert gravity what does "jump" mean in your game? It's up to you if it will be able to jump. \$\endgroup\$ – SteakOverflow Dec 10 '14 at 15:51
  • \$\begingroup\$ the game will be a puzzle platformer, so when gravity is reversed the player will fall towards the "ceiling"... wait... what if... instead of reversing gravity. I load 2 scenes for every level, one is rightside up the other is upside down and the trigger swaps you between the two but maintains your relative position! GENIUS \$\endgroup\$ – Carson Dec 10 '14 at 16:00
  • \$\begingroup\$ That's way more complicated (and resource intensive) than what you need. \$\endgroup\$ – McAden Dec 10 '14 at 21:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.