I am trying to detect if an object is still colliding with another object after one second. This is my script so far but I’m not sure how to detect if it is still colliding maybe using an if statement after the one second is up:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DetectIfParked : MonoBehaviour
{
void OnTriggerEnter()
{
StartCoroutine(DetectIfStillColliding());
}
IEnumerator DetectIfStillColliding()
{
yield return new WaitForSeconds(1);
}
}
Is there a better way to do this (I’m making a parking simulator and I need to see if the car is in the parking space for one second)
Also which game object do I attach this script to - the car or the objective it’s colliding with.
Another one, sorry, is there a way to detect if the car is in all four corners of the objective (parking space).
Thank You and any help is appreciated
OnTriggerStay is another unity event which you can check, it sends continuously feedback in case there is anyone in its collider
Thanks. How would I implement that?
There you go:
Start by copy pasting the example and adjust your behaviour onto it 
An alternative to this is to use both OnTriggerEnter() and OnTriggerExit() in combination.
When you enter, set an isColliding bool and run your timer or coroutine. Stop and reset the timer or coroutine and your isColliding flag when you exit or when your time expires depending on your goals.
This way you don’t have to check which objects you’re colliding with every physics tick, only when you enter or exit something.
To keep track of multiple colliding objects at the same time, add the hit objects to some sort of collection when the collision starts and remove them when collision ends. The Collider parameter you see in all the OnTrigger and OnCollider functions carries a lot of information you can use, especially the transform or the gameObject you hit.
It’s pretty easy to check the tag (or some other identifier) of the other gameObject you hit if you want discern categories of objects.
void OnTriggerEnter(Collider other)
{
other.gameObject.tag; //the tag of the other gameObject you hit
other.whateverYouNeedToUse;
}
Seeing as you want to know what state the car is in, it’ll be easier to keep track of all the things the car has collided with, rather than having the colliding things keep track of hitting the car. So track collisions on the car itself, so that’s where your script should probably go.