- Home /
Change the gravity direction for CharacterController
like what the title says .. i have a CharacterController .. i made a code to change the rotation of the player so he would look like walking on the roof .. but he's walking on the floor upside down .. how can i change the gravity ?
Answer by Madeck · Dec 12, 2014 at 08:45 PM
If you have a CharacterController and you want to change the gravity, you have to access the component’s CharacterMotor.movement.gravity
variable and change it. By default the CharacterController will only work with positive and negative gravity, you will have to build something different if you want to have complete control over gravity direction rather than simply switching its direction along the y axis.
I built my gravity inverted, I had to make sure to set CharacterMotor.grounded = false
in order for the character not to be stuck on the roof.
I know your question is over two years old, but in case someone has a similar one someday, they might find this answer helpful.
I usually script in UnityScript, but here’s what I basically do–except I added a short animation to make it smoother:
void OnTriggerEnter (Collider other) {
if(other.tag == "Player") {
other.transform.Rotate(0,0,180);
Character$$anonymous$$otor.grounded = false;
Character$$anonymous$$otor.movement.gravity *= -1f;
}
}
Just for your information, there are several problems that arise when you’re using inverted gravity, as the Character$$anonymous$$otor is never “grounded” when it’s on a roof. I’ll probably post some more information if I figure out properly how to make everything work fine, especially with slopes or stairs.
Thank you, $$anonymous$$adeck. I think this is what I needed.
Your answer
Follow this Question
Related Questions
Gravity with Character Controller? 4 Answers
Double Jump 1 Answer
A node in a childnode? 1 Answer
Collision Detection 0 Answers