- Home /
bizarre collision modulations with character controller
Strange predicament here, I have my character, and he's using: a character controller, the third person controller that comes in the character controller's package, and two other scripts, 'animate' which controls animation, and 'attack' which is the script giving me all this trouble. So here's what i want to do: make my character, when a button is hit, shoot down rapidly, and when he hits the ground or an enemy, attack it. I'm starting by just printing things instead of actually attacking. Here's the script:
var attackButton = "Fire1";
function Update () {
var controller : CharacterController = GetComponent(CharacterController);
var variables = GetComponent("ThirdPersonController");
if (controller.isGrounded){
print("OnGround");
if (Input.GetButton(attackButton) && variables.gravity == 500){
print("attackedGroundEnemy");
variables.gravity = 40;
}
}
if (!controller.isGrounded){
print("InAir");
if (Input.GetButton(attackButton)){
variables.gravity = 500;
}
else{
variables.gravity = 40;
}
}
}
Here's the problem, he shoots downward fine, and stops when he hits ground, however, when i'm just sitting still and looking at the debug strip at the bottom of the screen it modulates : "OnGround", "InAir", "OnGround", "InAir", "OnGround", "InAir". What this does is make it so the player can 'attack' the object beneath him while sitting still. I see this as a bizarre error. Do you guys have any suggestions as to fixing this? is there something wrong with the script? is this a Unity error? Any help is greatly appreciated! Thank you!
-Stopsecret Design
Answer by Bomadeno · Jul 19, 2012 at 10:29 AM
This problem is described here. You can solve it by applying gravity every frame, even when you're 'Grounded'.
(In our code we have something like this... it keeps the character at 'near 0' velocity when they're on the floor and stops them flickering into the air tiny amounts)
if(controller.isGrounded){
velocity.y = 0; //assuming gravity is in y
velocity += Physics.gravity * Time.deltaTime;
}
Your answer
Follow this Question
Related Questions
Animation through graphics in FPS controller, weird character controller issue, NEED help! 0 Answers
How do you make a characterController jump automatically when it reaches the edge of a platform? 0 Answers
Wind that push a character when enter in a trigger zone 0 Answers
Problem with CharacterController.isgrounded 1 Answer
CharacterController falls through or slips off moving platforms 2.0 2 Answers