Hi everyone. I have an animated character who is supposed to walk to a certain spot when something happens. The problem is that the walk speed changes depending on where I play it. Inside the Unity app, it walks fine since that what I’ve been testing on, but when I export to web player, it walks twice as slow.
You should make the translation relatively to the Time.deltaTime or use the FixedUpdate instead of the Update.
The editor and the player in the browser have different FPS (the player in the browser is limited). You will notice different translation speed in differents machines too if don’t change the code.
Don’t use FixedUpdate for anything except physics; it can cause problems if you’re trying to use it for consistent timing. (For example, you decide to change the physics timestep later, then all your code is wrong). Use Time.deltaTime.
That’s not quite correct. Time.deltaTime returns the correct amount of time in a physics timestep during Fixed update. It probably should be used, in this case.
To do the movement smoothly, work out how many metres per second the character should be walking, and then multiply that by Time.deltaTime in the Update function.
E.G. for 2 metres per second
transform.position.x -= 2.0 * Time.deltaTime;
This will give you a constant speed.
The reason you we’re getting variable speeds before is because the Update function is called every frame. If your frame rate was 30fps, your character would be moving 0.2 units 30 times a second (6 units per second), whereas at 60 fps it would move 0.2 units 60 times per second (12 units per second). Time.deltaTime gives you a float equal to the time in seconds since the last frame, so by multiplying the characters speed by this amount, you get the right distance to move each frame no matter what the framerate is.
Yes, if you used Time.deltaTime in a FixedUpdate. But if you’re doing that, then you might as well use Update, which is better for performance in low-framerate situations, because you’re not trying to force it to run 50fps (or whatever you have it set to). FixedUpdate is only for applying physics; everything else should be in Update or coroutines for best results.