0
votes

My GameObject starts out resting on the floor. isKinematic is set to true and a trigger is set to true on the box collider.

When the player touches the collider. It moves the GameObject down below to floor. Then i set the isKinematic to false and the trigger to false. This forces the game object to fall until it hits the floor and stops. My problem is after the game object hits the floor and stops. I can not get the GameObject to recognize it has collided with the floor. I have a Debug.Log statement in OnCollisionEnter2D and OnCollisionStay2D. The Debug.Log does not appear in the console when they touch. Why is this?

The GameObject has a Rigidbody and box collider. The floor has a box collider and Rigidbody as well.

void OnTriggerEnter2D(Collider2D other)
{
    if (other.gameObject.tag == "Player") 
    {
        Debug.Log ("Player is touching the section");
        //sectionRigidbody.isKinematic = true;
        if (readyToDrop == false)
          //moves player
        transform.position += newPosition;
        readyToDrop = true;
        sectionRigidbody.isKinematic = false;
        sectionBoxCollider.isTrigger = false;
        sectionRigidbody.collisionDetectionMode = CollisionDetectionMode2D.Continuous;
        //sectionRigidbody.isKinematic = true;
    }
}

void OnCollisionEnter2D(Collider2D col)
{
    if (col.gameObject.tag == "Floor") 
    {
        Debug.Log ("section is touching the floor");
    }
}

void OnCollisionStay2D(Collider2D col)
{
    if (col.gameObject.tag == "Floor") 
    {
        Debug.Log ("section is touching the floor");
    }
}

enter image description here enter image description here

2
Put all your Debug.Log outside the if tag statement then test againProgrammer
moved it outside and still nothing @ProgrammerAlex Priest
Post screeenshot of components attached the GaemObject.Programmer
@Programmer I have edited the original version with the pictures you requestedAlex Priest
Hi, I checked your picture and the settings looks fine. You said is "My GameObject starts out resting on the floor. isKinematic is set to true and a trigger is set to true on the box collider" but this is not true based on the images you just uploladedProgrammer

2 Answers

1
votes

Pay attention when using the collision callback functions with parameter.

OnTriggerEnter2D takes Collider2D as parameter. You got this right.

OnCollisionEnter2D and OnCollisionStay2D takes Collision2D as parameter NOT Collider2D. This is where you failed.

Collider2D and Collision2D sounds so simlar but are not the-same.

They won't be called if you got their parameter wrong.

In the latest version of Unity, error will be thrown in the Editor when you make this mistake. It looks something like this:

Script error: OnCollisionEnter2D This message parameter has to be of type: The message will be ignored.

and

Script error: OnCollisionStay2D This message parameter has to be of type: The message will be ignored.

Solution:

Replace

void OnCollisionEnter2D(Collider2D col) and void OnCollisionStay2D(Collider2D col)

with

void OnCollisionEnter2D(Collision2D col) and void OnCollisionStay2D(Collision2D col).

1
votes

You mentioned that trigger is set to true.

A trigger doesn't register a collision with an incoming Rigidbody. Instead, it sends OnTriggerEnter, OnTriggerExit and OnTriggerStay message when a rigidbody enters or exits the trigger volume.

Source: https://docs.unity3d.com/ScriptReference/Collider-isTrigger.html