0
\$\begingroup\$

This script is attached to the wall(3D Cube) with a box collider :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LockedRoomTrigger : MonoBehaviour
{
    private void OnCollisionEnter(Collision collision)
    {
        Debug.Log(collision.transform.name);
    }
}

But when the player is moving and hit the cube it's not getting to the Debug.Log I used a break point on the Debug.Log line.

A screenshot of the cube wall with the box collider component and the script attached to it. The problem is not with the player since if I'm moving with the player near doors they open so the player does trigger things and also not walking through walls.

Not Collide

This is a screenshot of NAVI. NAVI is a bit in front of the player and moving with the player and part of the player in the game like a friend helper.

NAVI is the one that collide and hit stuff if doors walls not the player since the NAVI is a bit in front of the player.

NAVI have only a box collider :

NAVI

And this is the Inspector screenshots of the player. I will add two screenshots since it's not getting inside in one screenshot :

The player is a first person view :

Player

Player

\$\endgroup\$
8
  • \$\begingroup\$ It's working only if I'm adding to the cube wall also a rigidibody and uncheking the gravity and is kinematic but then when the player hit the cube the cube start to float in air from the hit. So it's not working good yet. \$\endgroup\$
    – Daniel Lip
    Mar 28, 2020 at 15:35
  • \$\begingroup\$ I want to use the cube wall object only for a trigger in the game. When the player enter the door and hit the cube something else should happen. The cube is only for triggering. \$\endgroup\$
    – Daniel Lip
    Mar 28, 2020 at 15:35
  • 1
    \$\begingroup\$ Do you have a rigidbody on your player object? \$\endgroup\$
    – DMGregory
    Mar 28, 2020 at 15:44
  • \$\begingroup\$ Yes I checked now there is a rigidbody also on the player and on the player both is kinematic and gravity are checked. \$\endgroup\$
    – Daniel Lip
    Mar 28, 2020 at 15:48
  • 1
    \$\begingroup\$ On Collision Enter messages are sent from a static collider only when a collider with a rigidbody touches it. So, if you're not getting the message, then either the thing that touches the collider doesn't have a rigidbody, or you're not actually touching it. It's possible your first person controller script is configured to move up to - but not actually touch - the surface of a wall. This wouldn't stop your triggers from firing because you can still get inside them. But this is all speculation because you still haven't shown the code used to move your player. \$\endgroup\$
    – DMGregory
    Mar 28, 2020 at 16:17

2 Answers 2

1
\$\begingroup\$

The OnCollision* events are for Rigidbody-Rigidbody collisions. They get called when two solid rigidbodies "bump" into each other.

When you want a "non-corporeal" trigger area, like an invisible zone which opens a door when the player enters it, then mark the "Is Trigger?" checkbox in that collider and implement the OnTrigger* events in its script (Like OnTriggerEnter). Keep in mind that the signature of the OnTrigger methods is different than of the OnCollision methods. While the OnCollision methods receive a Collision object with more information about how the collision occured, the OnTrigger methods only receive the Collider component which triggered it.

Further, take note of the collision action matrix in the documentation. Trigger events are only generated when at least one of the objects involved in the collision has a Rigidbody and the other is either marked as Static or also has a Rigidbody. Usual practice is to add no rigidbodies to trigger areas but rigidbodies to the objects which are supposed to trigger them.

tl;dr:

  • Mark the "Locked Room Trigger" game object as "Static" or give it a Rigidbody component
  • Mark the "Is Trigger?" checkbox of the collider of the Locked Room Trigger
  • Make sure the object which is supposed to trigger the Locked Room Trigger has a Rigidbody component
  • Use OnTriggerEnter instead of OnCollisionEnter:

 

public class LockedRoomTrigger : MonoBehaviour
{
    private void OnTriggerEnter(Collider other)
    {
        Debug.Log(other.name + " entered the trigger " + name);
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ thank u Phillipp sooooooooooo much \$\endgroup\$
    – PurifyCode
    Apr 16 at 23:24
0
\$\begingroup\$

Is the Locked Room Trigger object supposed to be a trigger or a collider? From your question, it sounds like you intend it to be a trigger so you need to check "Is Trigger" on its box collider and use OnTriggerEnter instead of OnColliderEnter.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .