x


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.

more ▼

asked Jul 25 '12 at 06:30 AM

Xatoku gravatar image

Xatoku
152 73 81 88

finally... player or enemy is an object that should not stop falling?

Jul 27 '12 at 08:27 AM ScroodgeM

Yeah, if it were to touch the top of someone's head, then they should be told isFlying = true;

Jul 27 '12 at 08:36 AM Xatoku
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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
    }

more ▼

answered Jul 27 '12 at 08:27 AM

ScroodgeM gravatar image

ScroodgeM
7.6k 2 6

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.

Jul 27 '12 at 08:38 AM Xatoku

I'm a little explained solution 3

Jul 27 '12 at 08:50 AM ScroodgeM
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2482
x1682
x1040
x32
x5

asked: Jul 25 '12 at 06:30 AM

Seen: 333 times

Last Updated: Jul 27 '12 at 08:50 AM