- Home /
Update function Problem...very odd
There is a problem I am having with this code. Basically it ramps up the speed of movement the farther you are in the level, but there is one problem...
private var currentPosition : float;
private var startTimer : float = 0;
private var offset : float;
private var scrollSpeed : float = 1;
function Start () {
offset = 0;
startTimer = 0;
}
function Update () {
startTimer += 0.0175;
offset = startTimer * scrollSpeed;
if(offset >= 25 && offset < 40) {
scrollSpeed=1.1;
}
else if(offset >= 40 && offset < 65) {
scrollSpeed=1.2;
}
else if(offset >= 65 && offset < 95) {
scrollSpeed=1.3;
}
else if(offset >= 95 && offset < 110){
scrollSpeed=1.4;
}
else if(offset >= 110) {
scrollSpeed=1.5;
}
print("sdfsdf: " + startTimer);
var speed : float = scrollSpeed * 2;
transform.position.z -= speed/6;
}
(This code moves objects to the player at an increasing rate)
What goes wrong is that startTimer variable stops increasing about 2 seconds into the level. I have NO idea why it just stops.
However, when commenting out the line "transform.position.z -= speed/6", it increases just fine.
What am I doing wrong?
That can't work regardless, since you're adding .0175 to startTimer per frame, so the amount added will vary as the framerate varies.
But how would I fix the problem of it just stopping the increase? Should I use FixedUpdate for the increment?
No, FixedUpdate is for physics. You can use Time.deltaTime.
Your answer
![](https://koobas.hobune.stream/wayback/20220613064212im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
"Unable to find Halo" error message. 1 Answer
[2D] Get the position of an object outside the scope 2 Answers
Update instantiates more than 1 object 2 Answers
Define execution order of same script on multiple gameobjects 2 Answers
Does it help performance to first check if some transform attribute has changed before setting it? 1 Answer