- Home /
Problem detecting ground with collision flags and controller.move (vector3.down)
I am using a character controller and after the character controller double jumps if you press jump a third time he does an "air smash." Kind of mario style. If the ground is visible it works fine. But if I can't see the ground below the character he will perform the air smash but will not detect a collision when he hits the ground and therefore goes right through the ground... I'm stuck trying to figure out why. My script is very long so here is the bits pertaining to what I'm trying to accomplish.
if (!airSmashing) {
collisionFlags = controller.Move (direction * speed + Vector3 (0, verticalSpeed, 0) + inAirVelocity;);
}
if (airSmashing) {
canControl = false;
controller.Move (Vector3.down* 100 * Time.deltaTime);
if (controller.isGrounded) {
airSmashing = false;
canControl = true;
}
}
Any suggestions?
What do you mean by 'can't see the ground'? Is it off the edge of the camera? Does this only happen when the character is a certain distance away from the ground?
Yes this only happens when the ground is off the edge of the camera... even if it's just so slightly out of view of the camera.
The collisionFlags variable in your script above, is that just your own variable? It's not, for example the character controller's collisionFlags, which you should not set yourself, right?
yes it's my own variable... but I've been messing around a bit and it turns out it happens when he's above a specific distance away from the ground. The camera view doesn't actually have anything to do with it. Seems to be about 10 meters he collides with the ground... anything beyond that he falls through
Answer by stingman · May 18, 2012 at 11:12 PM
Alright, so I solved the problem. The issue the character controller was having was not being able to detect a collision based on other aspects of my script. I'm still somewhat shaky on this. BUT, I did get an air smash to work. Not as smooth as mario yet but I'm sure this will definately help others trying to do the same thing. Enjoy: (put this in your update function:
if (airSmashing) {
canControl = false; // disable any controls for the player
controller.Move (Vector3.down * 10000 * Time.deltaTime);
if (CollisionFlags.CollidedBelow) {
airSmashing = false;
canControl = true;
controller.Move (//whatever your normal move code is);
}
It's fine that you kind of fixed your problems, but this doesn't make much sense ;)
CollisionFlags.CollidedBelow is just a constant value (actually it's just a value of 4). That means your whole if statement equals:
if (true)
{
which makes the whole "if" useless so the code is executed anyway.
As far as i know controller.isGrounded
and controller.collisionFlags == CollisionFlags.CollidedBelow
is actually exactly the same. Usually it should work, are you sure that you don't move the ground upwards or something like that so that not the character goes through the ground but the ground through the player?
I can't think of much other reasons except that you manually disabled the collision (layerbased collision matrix or IgnoreCollision).
Note that CharacterController.detectCollisions has no effect on the collision detection of the controller itself. It just disables the collider capsule for other objects. $$anonymous$$ove or Simple$$anonymous$$ove will detect collsions anyway.
ya I don't fully understand why my solution works... it just does. Your post helped me understand this a little more though. Anyhow the problem was not that the ground was moving through the player. the player went through the ground and somehow his position was transformed up on the y axis by an immense amount of units at which point gravity was reactivated and the player would fall at his ter$$anonymous$$al velocity back to the ground. By immense units I mean at least 1000 units each time.
Your answer
Follow this Question
Related Questions
Ceiling collision detection 0 Answers
How to determine a collision incase of two Character-Controller. 0 Answers
How does unity character controller work? 0 Answers
Disable collision between mesh collider and character controller 3 Answers
Voxel engine physics 3rd person character controller 1 Answer