I'm trying to figure out how script gravity reversal on collision with an object. How can I reverse gravity?
-
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\$– HouseCommented Dec 9, 2014 at 22:38
-
\$\begingroup\$ trying to reverse gravity for the single item \$\endgroup\$– CarsonCommented Dec 10, 2014 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\$– CarsonCommented Dec 10, 2014 at 23:59
2 Answers
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
.
-
\$\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\$– McAdenCommented Dec 11, 2014 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\$– CarsonCommented Dec 11, 2014 at 0:52
-
-
\$\begingroup\$ That would be a syntax error. You're probably missing a quote around a string. \$\endgroup\$– McAdenCommented Dec 11, 2014 at 4:30
-
\$\begingroup\$ @Carson You can accept the answer now mate \$\endgroup\$ Commented Jan 13, 2018 at 9:02
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.
-
\$\begingroup\$ but would the player still be able to jump? \$\endgroup\$– CarsonCommented Dec 10, 2014 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\$ Commented Dec 10, 2014 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\$– CarsonCommented Dec 10, 2014 at 16:00
-
\$\begingroup\$ That's way more complicated (and resource intensive) than what you need. \$\endgroup\$– McAdenCommented Dec 10, 2014 at 21:38