0
\$\begingroup\$

I am fairly new to Unity2D and am making a 2D platformer to get used to the engine. I am trying to create a platform that moves when the player jumps on it, however, on the script for the platform the OnCollisionEnter2D method is just not being called. This is the script:

public class ThrustPlatform : MonoBehaviour

[SerializeField] float thrustSpeed;
[SerializeField] float thrustDistance;

Animator thrustAnim;
Rigidbody2D rb;

private bool thrust = false;
private Vector2 startPos;
private Vector2 targetPos;


void Start()
{
    thrustAnim = GetComponent<Animator>();
    rb = GetComponent<Rigidbody2D>();

    startPos = transform.position;
    float targetX = startPos.x + thrustDistance;
    targetPos = new Vector2(targetX, startPos.y);
}


private void OnCollisionEnter2D(Collider2D coll)
{
    Debug.Log("COLLISION");
    thrust = true;
}

private void FixedUpdate()
{
    if(thrust)
    {
        rb.velocity = new Vector2(thrustSpeed, 0);
    }
}   

The player and platform are on layers that can collide with each other in the collision matrix, the player still lands on the platform, and the player has a dynamic rigid body and the platform a kinematic rigid body. As far as I am aware kinematic rigid bodies detect collisions from dynamic rigid body, I have watched tutorials where this works. Even if I change the platform rigid body to dynamic there is no detection. I have also tried using continuous collision detection on both rigid bodies but still it is not calling. If I change it to a trigger and use OnTriggerEnter2D however, the Debug line appears.

This is the rigidbody for the platform. Rigidbody details for the platform

This is the rigidbody details for the player. Rigidbody details for the player

I am very confused as to why this isn't working and I can't find a solution anywhere else (unless I am missing something very obvious).

\$\endgroup\$
2
  • \$\begingroup\$ Is IsTrigger enabled on the Floor? Because if it is OnCollisionEnter doesn't get called, but OnTriggerEnter instead. \$\endgroup\$
    – MathewHD
    Jul 16 '20 at 13:30
  • \$\begingroup\$ No I only enabled IsTrigger to test if OnTriggerEnter worked, which it did, then changed it back to try OnCollisionEnter \$\endgroup\$
    – scalmark
    Jul 16 '20 at 13:33
0
\$\begingroup\$

Your method signature must match the one given in the documentation.

OnCollisionEnter2D takes a Collision2D argument, not a Collider2D argument as you've used here.

Because your signature doesn't match, Unity finds no matching OnCollisionEnter2D(Collision2D collision) function to call, so it doesn't call it.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.