# Starting a timer with a collision?

**URL:** https://discussions.unity.com/t/starting-a-timer-with-a-collision/414399
**Category:** Physics
**Tags:** Scripting
**Created:** [May 24, 2010, 4:02am UTC](https://discussions.unity.com/t/starting-a-timer-with-a-collision/414399 "2010-05-24T04:02:42Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![anon\_34109467](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@anon\_34109467](https://discussions.unity.com/u/anon_34109467)
#### Post date: [May 24, 2010, 4:02am UTC](https://discussions.unity.com/t/starting-a-timer-with-a-collision/414399/1 "2010-05-24T04:02:42Z")

</div>

OK, this is confusing me, and I"m sure it has some ridiculously simple solution that I’m incapable of seeing right now.

I’d like to start a timer when a character controller hits a certain object. The problem is that if the charactercontroller continues to collide with that object (this variable would be set inside of an OnControllerColliderHit() function), the timer variable continues to set itself to Time.time.

Basic code is:

Function OnControllerColliderHit(hit : ControllerColliderHit) {  
if (hit.collider.tag == “Clingable”){  
timer = Time.time;  
}  
}

Obviously my actual code is more complex than that. Currently, what happens is that if a player jumps into a wall that’s tagged the right way, they cling to it, and don’t move, and have restricted movement (can only jump up, or fall down). What I’d like to do is, 3 seconds after attaching to the wall, they automatically fall off, and have to move back towards the wall to cling again. Easiest way to do that with the rest of my code is have a timer that grabs Time.time only the first time a player touches that wall, and then starts again only if they’ve either jumped, or are falling (basically the timer hitting a limit would set a varaible to false).

Man I hope this makes sense, I get a bit rambly…

---

<div class="post-metadata">

### Author: ![anon\_77129249](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@anon\_77129249](https://discussions.unity.com/u/anon_77129249)
#### Post date: [May 24, 2010, 4:14am UTC](https://discussions.unity.com/t/starting-a-timer-with-a-collision/414399/2 "2010-05-24T04:14:27Z")

</div>

add some bools… if you attach to a wall, set a variable. then check for the variable, if it’s true… don’t set the value of timer.

---

<div class="post-metadata">

### Author: ![anon\_34109467](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@anon\_34109467](https://discussions.unity.com/u/anon_34109467)
#### Post date: [May 24, 2010, 4:17am UTC](https://discussions.unity.com/t/starting-a-timer-with-a-collision/414399/3 "2010-05-24T04:17:56Z")

</div>

Hah, like I figured… ridiculously simple! I’ll try that, thanks!

---

<div class="post-metadata">

### Author: ![anon\_72365031](https://avatars.discourse-cdn.com/v4/letter/a/6a8cbe/32.png) [@anon\_72365031](https://discussions.unity.com/u/anon_72365031)
#### Post date: [May 24, 2010, 6:48am UTC](https://discussions.unity.com/t/starting-a-timer-with-a-collision/414399/4 "2010-05-24T06:48:32Z")

</div>

You could also just check your counter itself. If not 0, then it is running already. Just set it back to 0 when you fall off the wall.
