- Home /
How come my camera does not follow my script?
I am working on a headbob script:
1 void Update () {
2 print (headbob);
3 if (headbob > 2) {
4 transform.position += new Vector3 (0, .1f, 0);
5 headbob = headbob - Time.deltaTime;
6 }
7 if (headbob < 1) {
8 transform.position += new Vector3 (0, -.1f, 0);
9 headbob = headbob + Time.deltaTime;
10 }
11 if (headbob == 2f){
12 headbob = 0f;
13 print ("switchdown");
14 }
15 if (headbob == 1f) {
16 headbob = 3f;
17 print ("switchup");
18 }
The headbob value stays at 1.99855 (2) and does not move on to go back down, What is wrong, is it the inconstancy with the Time?
Answer by donutLaserDev · May 31, 2018 at 03:05 PM
1.99855 is not greater than 2, it is not less than 1, it is definitely not equal to 2 and it is not equal to 1. So, unless there's some code besides this that should change the value, the value will not change. From the description it looks like, you are assuming that 1.99855 is 2, which is not true. You could try using Mathf.Aproximatelly (headbob, 2), which is more suitable for comparing the float values.
Your answer
Follow this Question
Related Questions
Playing the right animation when running and not running 0 Answers
C#, Problem with If Statements and GO Position Comparisons. 1 Answer
If statement randomly not working 2 Answers
If statement inside FixedUpdate 0 Answers
How to increment score by one, every time player moves the device face down ? 2 Answers