- Home /
Grounded Only on One Layer
I've got my character grounded properly, but I don't want him considering standing on top of another person as ground. It's hard to explain, but basically, the character flies. So when he stops flying, he'll float down to the ground. My problem is that if he were fighting mid-air and accidentally collided above an enemy, he would act as if that enemy is the ground, and ground himself, thus stopping him from flying.
What I want him to do in that situation is to not consider that person as ground, and simply continue flying. So I essentially want to limit what the character see's as being "grounded" to a selected layer mask. If anybody got through my mess of an explanation, here's what I'm working with:
//Apply gravity moveDirection.y -= gravity Time.deltaTime; //Move controller var controller:CharacterController = GetComponent(CharacterController); var flags = controller.Move(moveDirection Time.deltaTime); grounded = (flags & CollisionFlags.Below) != 0; //^Here would be the "grounded only if colliding with layer X snippet"
Any help is greatly appreciated as always.
finally... player or enemy is an object that should not stop falling?
Yeah, if it were to touch the top of someone's head, then they should be told isFlying = true;
Answer by ScroodgeM · Jul 27, 2012 at 08:27 AM
solution 1
make objects that should not collide in the same (different) layers, then using collision matrix disable collision between these two (with itself) layers.
http://docs.unity3d.com/Documentation/Components/LayerBasedCollision.html
solution 2
if other object is enemy, so i think (depends on gameplay) player and enemy should not collide at all, enemy must have a trigger to just detect collision and make some events if it happened. trigger will not stop collider falling.
solution 3
make "OnCollisionEnter" method for character and just check other collider for it's ground flag. it can be just a Tag "Ground". so only CollisionEnter with object tagged "Ground" will set flag "Grounded" to 'true', then use this flag to fall or don't fall player down
http://docs.unity3d.com/Documentation/Components/Tags.html
http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnCollisionEnter.html
void OnCollisionEnter(Collision collision) { if (collision.gameObject.CompareTag("Ground")) { isFlying = false; } //all other collidings should not set this flag to false, so only object tagged "Ground" can stop falling }
With Solution 3, how can I ask if the ground is beneath the character though? That's why I'm using .below in the first place.
Your answer
Follow this Question
Related Questions
Simple AI and Animation 2 Answers
Colliders not working with Character VS House 0 Answers
early collision above character controller 2 Answers
Character is not grounding. 2 Answers