0
\$\begingroup\$

I want to be able to sense when the player collides with a door from my GameController script. The player is a public GameObject and doors are tagged as such. Here is psuedo code for what I'm looking for:

public class GameController : MonoBehaviour
{
   public GameObject player;

   void Update()
   {
      if(player collides with game object tagged "Door")
         do something
   }
}
\$\endgroup\$
2
  • \$\begingroup\$ This is a very common task and covered by every basic Unity tutorial. There are plenty of resources showing up if you type the relevant keywords in your search engine. Then you quickly find the MonoBehaviour's OnCollisionEnter and similar methods you can override to achieve your goal. \$\endgroup\$
    – schneebuzz
    Apr 28, 2020 at 6:16
  • \$\begingroup\$ @schneebuzz The problem with OnCollisionEnter is that this script would need to be attached to the player or door itself in order for that function to work. This GameController script won't be attached to either. \$\endgroup\$ Apr 28, 2020 at 18:24

2 Answers 2

1
\$\begingroup\$

Thank you @schneebuzz for helping me to fix this issue. With his advice I was able to find a function to manually check for collision with a rigidbody.

public class GameController : MonoBehaviour
{
    public GameObject player;
    public float collisionCheckDistance;

    private Rigidbody rigidbody;

    void Start()
    {
        rigidbody = player.GetComponent<Rigidbody>();
    }

    void Update()
    {
        RaycastHit raycastHit;

        if (rigidbody.SweepTest(rigidbody.transform.TransformDirection(Vector3.forward), out raycastHit, collisionCheckDistance))
        {
            if (raycastHit.transform.tag == "Door")
            {
                DoorController doorController = raycastHit.transform.gameObject.GetComponentInParent<DoorController>();
                doorController.openDoor();
                doorController.closeDoor();
            }
        }
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ You can accept your own answer so that the question is marked as solved for others stopping by \$\endgroup\$
    – schneebuzz
    Apr 29, 2020 at 8:38
  • \$\begingroup\$ @schneebuzz I have to wait 24 from the time I posted the solution to accept it unfortunately \$\endgroup\$ Apr 29, 2020 at 21:22
0
\$\begingroup\$

You need a collider (e.g. BoxCollider, CapsuleCollider etc.) on both the player and door and a script component on your Player:

public class MyPlayer : MonoBehaviour
{

   void OnCollisionEnter(Collision collision)
   {
        if(collision.gameObject.CompareTag("Door")
        {
            Debug.Log("Player Hit door, do something!");
        }
   }
}

As noted by @DMGregory: One of the two colliding components need an attached Rigidbody or CharacterController, in your case this is most likely the player.

\$\endgroup\$
3
  • \$\begingroup\$ Note that the player object also needs a Rigidbody or Character Controller component attached in order for OnCollisionEnter messages to be sent. Or their 2D equivalents if making a 2D game. If you don't have a body, you can still use physics queries like overlap tests or raycasts to detect colliders in Update, as an alternative. \$\endgroup\$
    – DMGregory
    Apr 28, 2020 at 7:44
  • \$\begingroup\$ I appreciate this, but it's not quite what I'm looking for. I know how to do this from the player or door, but I want to do it from my GameController script that isn't attached to either. If this isn't possible, just let me know. Otherwise, I'm pretty set on wanting to do it this way. \$\endgroup\$ Apr 28, 2020 at 18:22
  • \$\begingroup\$ @JusThatGuy My bad for not accurately reading the question. You could either flag an occuring collision in your player script and check the flag from the GameController. This however requires a reference to the player script anyway. Or you can manually check for collisions using Physics.BoxCast or CheckBox using one colliders bounding box (or the Sphere-equivalents). If I remember right, Rigidbody has also a way to manually check for collisions. Other than these I don't know of any other way for manual checks. \$\endgroup\$
    – schneebuzz
    Apr 28, 2020 at 18:31

You must log in to answer this question.

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