Framerate and DeltaTime issues!!

This is probably a very simple problem to solve… however I’m not sure about Updates and DeltaTime things… So I need some help.

DEMO: http://download98.mediafire.com/37a2rx7ucfag/272hr4h9m7sdrg5/MiniPlanetBuild.zip
(If you can, could you try this out and see what frame rate you get?)

I have this piece of code for my player, The part about rotating the player using the mouse is the problem:

function Update()
{
var rotateSpeed = 300.0;

QuaternionRotation = Quaternion.FromToRotation (transform.up, (transform.position - planet.transform.position).normalized);
    transform.rotation = QuaternionRotation * transform.rotation;

... // other stuff happens here

//
transform.Rotate(0, Input.GetAxis ("Mouse X") * rotateSpeed * Time.deltaTime, 0); // <<< Not working right
//

... // other stuff happens here

transform.RotateAround(planet.transform.position, Vector3.up, planetRotateSpd * Time.deltaTime);
}

When I run the game in the editor I get 50fps and the player rotates fine, at a normal speed.
But when I build the game it runs at nearly 1000fps!!! And the player turns really slowly.

Here is my fps calculator encase there is something wrong with it:

function Update()
{
    timeleft -= Time.deltaTime;
    accum += Time.timeScale/Time.deltaTime;
    ++frames;
   
    // Interval ended - update GUI text and start new interval
    if( timeleft <= 0.0 )
    {
        // display two fractional digits (f2 format)
        guiText.text = "" + (accum/frames).ToString("f2");
		fps= (accum/frames);
        timeleft = updateInterval;
        accum = 0.0;
        frames = 0;
    }
}

Anyway. Any help would be greatly appreciated. :slight_smile:

try using FixedUpdate() instead of Update()

Your problem is not updates and deltaTime
your problem is that you mess the delta time by applying timeScale which is already included in deltatime

I tried that. In fact, I originall built the code with FixedUpdate. It makes no difference.

But surely this would not affect the rotation of the player? only the readings for the fps?

if accum is only used in there, then only the reading, right.

the only thing I could think of is that you either have more in the function than what we see or that it is caused by the fact that you apply totally distinct rotations to the object within the same frame.

That’s what I thought… but this 1000 fps thing still bugs me… and surely the rotation from the mouse is applied some what seperately.

There are no other pieces of code that refer to rotation in my script… but I can post the whole thing if you like.

Can you show every bit of code that affects the variables rotateSpeed and planetRotateSpd?

If you have Time.deltaTime added in to the calculations twice you would get a result similar to what you are experiencing.

Also, your quaternionRotation rotation doesn’t use Time.deltaTime which, if rotating against the other rotation could cause your problem (as it rotates faster at faster framerates).

I’ve just tried it out on a different game that doesn’t have any of the quaternion rotations or planet speed. And it does the same thing. So I don’t think it’s related to them. But the code is here if you want:

Player:

var planet : Transform;
var rotateSpeed = 300.0;


function Update()
{

...

   QuaternionRotation = Quaternion.FromToRotation(transform.up, (transform.position - planet.transform.position).normalized);
    transform.rotation = QuaternionRotation * transform.rotation;

...
 
    transform.Rotate(0, Input.GetAxis ("Mouse X") * rotateSpeed * Time.deltaTime, 0);
 
...

	planetScript = planet.GetComponent(PlanetBehaviourScript);
	planetRotateSpd = planetScript.rotateSpd;
	transform.RotateAround(planet.transform.position, Vector3.up, planetRotateSpd * Time.deltaTime);
}

Planet: (the player aquires planetRotateSpd from here)

var rotateSpd = 0;

function Update () 
{
	transform.Rotate(Vector3.forward, rotateSpd * Time.deltaTime);
}

I’ve only omitted code that controls movement such as gravity and walking. None of that deals with rotation.

Another thought, do you use Unity - Scripting API: Time.captureFramerate at any time?

Try to output the delta Time somewhere in both versions to verify that it is indeed doubling when the framerate doubles.

nope. Not using this anywhere at all… And I’ve managed to stop the framerate from being over 1000 using Unity - Scripting API: Application.targetFrameRate
but I still find that the speed of rotation is framerate dependant.

When I measure in the editor (at 60fps) moving the mouse all the way accross the screen results in 360 degree turn.

What exactly did you mean by output the deltaTime? Just read it to a GUI? (i’m trying that now)

DONE: ok yep. We’re looking at:
60fps = deltaTime at 0.01 to 0.02
120fps = deltaTime at 0.007 to 0.015

FixedUpdate is only for physics. You almost never want to use it for anything else.

Mouse input is already framerate-independent. Multiplying that by Time.deltaTime makes it framerate-dependent.

–Eric

Mouse input is already framerate-independent. Multiplying that by Time.deltaTime makes it framerate-dependent.

oh. That would be why then! :slight_smile: thanks