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.
This is the 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).
IsTrigger
enabled on the Floor? Because if it is OnCollisionEnter doesn't get called, but OnTriggerEnter instead. \$\endgroup\$