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!!!
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: