D
D
Dmitry2015-11-30 07:42:30
Unity
Dmitry, 2015-11-30 07:42:30

Unity buggy detecting Grounded?

Hello.
I'm writing a 2D platformer.
On the Internet in classic scripts for the character of the 2D controller, the definition of the player on the ground in the classics, the following code is used everywhere:

grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);

Rigidbody2D, BoxCollider2d, CircleCollider2d (so as not to stop on an uneven floor), CharacterController (Script), Animator hang on the Persian.
And everything works well, we jump on the platforms, we run, we pray all chikimoni. But there is one thing: when I started watching the grounded variable, I noticed such a moment, for example, if the player falls from the platform into the abyss, the grounded variable occasionally becomes true (blinks it), although there is naturally no ground underfoot. The same is observed when the object falls down after the jump.
What kind of glitch is that?
I decided to put a crutch in the form of code:
//вертикальная скорость
    vSpeed = GetComponent<Rigidbody2D>().velocity.y;

    //определяем, на земле ли персонаж
    if (vSpeed == 0) {
      grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
    } else {
      grounded = false;
    }

then everything became decorous and correct, when grounded falls, it never becomes true.
Just a question:
Please tell me, dear experts, is this a glitch of the engine? Or am I misunderstanding something in the logic of dvigun and groundCheck ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2015-11-30
@Dimusikus

Shorty decided.
Created a layer (Layer) =Ground, marked all platforms with this layer.
In the CharacterController, in the WhatIsGround field, unchecked the Default checkbox.
And that trick is gone.
It was necessary to do everything exactly like this, according to the mind, but I didn’t know.

S
Sergey, 2015-11-30
@sergey_kzn

Physics2D.OverlapCircle returns you a Collider2D. Look at what kind of collider it is, maybe you will understand what your character intersects with.
something like this:

Collider2D collider = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
Debug.Log(collider.name);

docs.unity3d.com/ScriptReference/Physics2D.Overlap...
docs.unity3d.com/ScriptReference/Collider2D.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question