1

I am a beginner in unity and I am having a problem with how to do the OnCollisionExit in OnControllerColliderHit. What I want to do is to detect the collision and make the object appear by setting the SetActive to true and then setting the SetActive to false to make it disappear again upon exiting the collision or contact with the object. Generally, I just want to know how to do the OnCollisionEnter and OnCollisionExit when using OnControllerColliderHit.

Here is my code: using UnityEngine;

public class PlayerCollision : MonoBehaviour
{
    public PlayerMovement movement;
    [SerializeField] GameObject _toggleObject;

    private void OnControllerColliderHit(ControllerColliderHit hit) {
        if(hit.gameObject.tag == "Obstacle")
        {
            _toggleObject.SetActive(true);
        }else
        {
            _toggleObject.SetActive(false);
        }
    }
Richard Johnson
  • 309
  • 4
  • 17
  • If a thing isn’t active. It can’t be hit. So wont be able to be activated. You can turn off the renderer to “hide” it but not effect the colliders. I’m not sure what oncontrollercolliderhit is supposed to be – BugFinder Sep 17 '22 at 16:04
  • it is the collision detector callback for Character Controller. – Richard Johnson Sep 18 '22 at 03:41

0 Answers0