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 /
avatar image
5
Question by Teague · Dec 05, 2010 at 07:49 PM · gravitychange

Gravity Direction Change

Hey there everyone!

I'm currently having a problem in Unity figuring out code to change the direction of gravity on a Character Controller. By this I mean the gravity normally is facing downwards, as gravity normally does, so my character can run left, right and jump as I want him to. But I want to be able to change it so the gravity faces upwards so the character can walk and jump on the roof.

Any help of guidance would truly be brilliant and much appreciated!!!

Many Thanks,

Matthew

http://www.youtube.com/watch?v=Fxuac...eature=related This in essence is what I am attempting to achieve from the game VVVVVV

Comment
Add comment · Show 2
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 spaceshooter · Dec 19, 2010 at 11:21 PM 1
Share

The link seems to be a bit borked.

avatar image ina · Dec 20, 2010 at 12:51 AM 0
Share

fix the youtube link please

4 Replies

· Add your reply
  • Sort: 
avatar image
8

Answer by poncho · Mar 22, 2011 at 05:56 PM

didnt see the video but to change gravity just need to change its physics.gravity property like this

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

this make a gravity same on y but changes on x axis, just play a little with the values and there you go hope this helps you

Comment
Add comment · Show 2 · 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 gqferbs · Feb 03, 2013 at 09:16 PM 0
Share

Can you achieve that effect locally on an object? Cause i'm looking to make a planet that you can walk 360 degrees around.

avatar image poncho · Feb 11, 2013 at 05:30 PM 0
Share

no, if you want that kind of "Gravity" use force for the object outside the planet, i mean, use the Universal Gravity Formula with some tweeks that suits you best another explanation every update add force to the walker depending on how close to the planet is, the vector of the force would be something like the distance between planet and walker, and a force calculated value to be able to keep it "walking" no mather it possision in the planet, good luck gqferbs

avatar image
7

Answer by GlitchEnzo2 · Dec 05, 2010 at 08:57 PM

It should be fairly easy to do. Instead of using the built-in gravity, just implement your own. You first need to set the Use Gravity to false.

Then, you would just apply this script to your objects:

using UnityEngine; public class ReversibleGravity : MonoBehaviour { float gravity = -9.8f;

 void Update () 
 {
     rigidbody.velocity.y += gravity * Time.deltaTime;
 }

 public void ReverseGravity()
 {
     gravity = -gravity;
 }

}

The slightly harder part would be orienting your character correctly without it "snapping" to the new gravity orientation. I would use spherical interpolation on the Up vector of your objects, like so:

Vector3 targetUp = new Vector3(0, -1, 0);
float damping = 8;
transform.up = Vector3.Slerp(transform.up, targetUp, Time.deltaTime * damping);
Comment
Add comment · Show 2 · 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:47 PM 0
Share

Only one correction, change Update to FixedUpdate nad Time.deltaTime to Time.fixedDeltaTime. There are functions with fixed time step and are not framerate dependant.

avatar image harko12 · Nov 09, 2014 at 06:20 AM 0
Share

This really helped me out, Thanks!

avatar image
0

Answer by DubstepDragon · Jun 07, 2013 at 08:13 PM

The answer is most simple - you do not create your own gravity settings, you can just modify the direction using this:

Changing the direction of Gravity using the built-in gravity system in Unity3D

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 26, 2016 at 10:14 PM

@Teague

One of the following lines changes gravity based on whether your game is 2d or 3d.

 //3d games
 Physics.gravity = new Vector3(0f,-10f,0f);
 
 //2d games
 Physics2d.gravity = new Vector3(0f,10f,0f);

Once you change the gravity in your code, you could also set a boolean to false. isGravityNormal could be the boolean variable name.

Then, in your Jump() function you could check that boolean in order to determine which way to jump upwards or downwards.

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 Awanonimik · Aug 03, 2017 at 05:36 PM 0
Share

One thing incorrect there, the 'd' in Physics2d needs capitalizing, like so: Physics2D.gravity = new Vector3(0f, 10f, 0f);

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

9 People are following this question.

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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Changing gravity on collision 4 Answers

Planet gravity help need guidance no code 1 Answer

Turning on and off Rigidbody programmatically? 3 Answers

Changing a materials color in C# 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