- Home /
Incrimenting a variable when in a certain area
I am trying to make a sneaking game and I cant find a way to increment the variable that tells me if you are detected when you're in the view of a person/camera/whatever.
For the view of the "detector" I have a cone that I have made a trigger and on my character controller I have a cube that is also a trigger
This is what I have but I'm assuming its horribly wrong, I got it from another question on here.(I'm new to this)
var detect:float; var incrementTime:float = 0.3; var incrementBy: float = 1; var time:double =0;
public function OnTriggerEnter(other: Collider) { time+=Time.deltaTime; while(time>incrementTime) { time-=incrementTime; detect+=incrementBy; } }
Answer by syclamoth · Mar 21, 2012 at 12:53 AM
Almost, but not quite. If you want the variable to increment constantly over time, do it more like this:
var curValue : float = 0;
var valueIncrease : float = 2;
function OnTriggerStay(other : Collider)
{
// put some conditionals here to make sure the right things are triggering it
curValue += Time.deltaTime * valueIncrease;
}
This will increase 'curValue' by at a rate of 'valueIncrease' every second.
Of course, you will need to add a rigidbody to your character (it can be kinematic) if you want it to activate triggers properly (look up any question at all that involves collisions, they almost all discuss this).
Your answer
![](https://koobas.hobune.stream/wayback/20220613065349im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Destroy Gameobject once 0 health 2 Answers
Why is not assigning new value ? 1 Answer
c# destroy Gameobject on 0 hp 1 Answer