Timer issue

I need to make PlayerController script avialable again in 3 seconds after entering trigger. But my timer doesn’t work. I use the code:

 float timeNow;

void OnTriggerEnter(Collider other){
  if(other.tag=="Player"){
    GameObject Pcontrol = GameObject.FindWithTag ("Player").GetComponent<PlayerController>();
    Pcontrol.canControl = false;
    Pcontrol.gravity = Vector3.zero;
    timeNow = Time.time;
  }
}

void Update(){
  if(Time.time>timeNow+3){
    GameObject Pcontrol = GameObject.FindWithTag ("Player").GetComponent<PlayerController>();
    Pcontrol.canControl = true;
    Pcontrol.gravity = new Vector3 (-Input.acceleration.x * 80, 80, 0);
  }
}

I’ve tried to do with with bool isOnTrigger and Invoke, but it wasn’t working either.
Thanks in advance.

It could be that the OnTriggerEnter is getting called multiple times if the ‘Player’ keeps moving in and out. Try a sticking a boolean in there like so:

Bool playerInTrigger = false;

void OnTriggerEnter(Collider other){

  if(other.collider.gameObject.tag == "Player" && playerInTrigger == false){
    GameObject Pcontrol = GameObject.FindWithTag ("Player").GetComponent<PlayerController>();
    Pcontrol.canControl = false;
    Pcontrol.gravity = Vector3.zero;
    timeNow = Time.time;
    playerInTrigger = true;
  }
}

 
void Update(){

  if(timeNow + 3 < Time.time && playerInTrigger){
    GameObject Pcontrol = GameObject.FindWithTag ("Player").GetComponent<PlayerController>();
    Pcontrol.canControl = true;
    Pcontrol.gravity = new Vector3 (-Input.acceleration.x * 80, 80, 0);
  }
}

Also, I would recommend replacing your ‘GameObject.FindWithTag (“Player”).GetComponent();’ with the actual object the trigger function returns - less work.

– Update –

I also noticed that you had ‘other.tag == Player’. I’m not 100% sure if that will work - replace it with ‘other.collider.gameObject.tag == Player’