- Home /
Character Controller Gravity
Hi; I'm just wondering if there is a way to turn off the character controller gravity. Its causing my character to slip when walking up walls. I read some ware to change the Character Motor script but am unable to find any way to change the pre-set classes though. Any help would be appreciated (it is too late to change to a ridgidbody it would involve re-writing the entire game).
Answer by aldonaletto · Oct 14, 2011 at 09:36 PM
The CharacterMotor script is terribly complex: you can get completely crazy trying to modify it - I know, I was one of its victims... but in the madhouse I had time to create some tricks, like this function to set the gravity value:
function SetGravity(g: float){ var chMotor: CharacterMotor = GetComponent(CharacterMotor); chMotor.movement.gravity = g; }Place this function in another script attached to the character, and use it to modify the gravity value (not the direction, unfortunately) when you need. In a few tests, this function effectively can turn the gravity off when you set it to 0. If the character is in the middle of a jump, for instance, it goes up forever unless you set the gravity to some reasonable value (default is 10).
I got Unity to accept the script but it gave me the errors "ArgumentException : You are not allowed to call GetComponent when declaring a variable." and another telling me the motor could not be found. I managed to get rid of the motor error by importing a character controller asset, it runs but I still get the "ArgumentException" error and it doesn't seem to work.
Have you changed something? I tested this function, and it worked fine. This error could occur if you moved the first instruction outside the function: the variable would be public, and due to some reason that I ignore you can't declare a public variable and assign GetComponent to it in the same instruction - but you can use GetComponent in any instruction after. If you want to save ch$$anonymous$$otor in a public variable, do it this way:
var ch$$anonymous$$otor: Character$$anonymous$$otor;
function Start(){ ch$$anonymous$$otor = GetComponent(Character$$anonymous$$otor); }
function SetGravity(g: float){ ch$$anonymous$$otor.movement.gravity = g; } And remember that the script containing this code must be attached to the character (the First Person Controller, not a child of it!), or Character$$anonymous$$otor will not be found.
This function doesn't work for me. It throws a null reference exception since the script can't find "ch$$anonymous$$otor" in the gameobject. Unity 4
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Help with Colliders? 0 Answers
Gravity and Jumping for a Character Controller 1 Answer
Character acceleration problem [Closed] 1 Answer
Character Flying around uncontrollably 2 Answers