- Home /
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
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
Can you achieve that effect locally on an object? Cause i'm looking to make a planet that you can walk 360 degrees around.
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
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);
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.
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
Answer by FalsAlarm · Dec 26, 2016 at 10:14 PM
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.
One thing incorrect there, the 'd' in Physics2d needs capitalizing, like so: Physics2D.gravity = new Vector3(0f, 10f, 0f);
Your answer
Follow this Question
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