RNG
RNG

Reputation: 101

How to detect collision between moving rigidbody cube and static empty object?

I am new to unity and working on collision detection.

I have a rigidbody cube and an empty object with a cube mesh. The rigidbody cube moves with arrow keys and the empty object is static. Both have a box collider.

How do I detect collision between this empty object and the rigidbody cube?

I am wondering whether it should be OnCollisionEnter or OnTriggerEnter and how to use the correct command.

Thank you for your help.

Upvotes: 0

Views: 1707

Answers (1)

Hardyk Mahendru
Hardyk Mahendru

Reputation: 333

First you need to set the box collider component to the cube and to the empty object with cube mesh. Then you can use this code:

using UnityEngine;

public class PlayerCollision : MonoBehaviour

{
    public PlayerMovement movement;
    public GAME_MANAGER GameManager;

    private void OnCollisionEnter(Collision collisionInfo)
    {
        if (collisionInfo.collider.tag == "Obstacle")
        {
            movement.enabled = false;
        }
    }

}

Make sure you have set the tag of the obstacle to “Obstacle” and add this script to the player or the cube. Here,

movement.enabled = false;

You can apply you own logic according to your need.

Thanks

Upvotes: 2

Related Questions