简体   繁体   中英

Movement is dependent to frame-rate. How can i make it independent from frame-rate

i am pretty new to coding. till now i searched lots of things yet i still couldn't solve my problem. I am trying to make a gameObject follow a line/graph. i tried to give him a negative-Y velocity and adding small numbers(something like; update{mybody.velocity += 0.005}) each update to make it move. but i encountered a problem which is at low frame-rate my object is taking a super wide path and at higher frame-rate it take super tight path. how can i make my movement independent.

private float curve = 0;
private float curvemax = 2.3f;
[SerializeField]
private float curveupdate = 0.05f;
if (tracking.found && !hareketeBaşlandı)
        {
            curve = -(curvemax);
            triggered1 = true;
            hareketeBaşlandı = true;
            print(curve);

        }
        if (transform.position.y != y1-curvemax && tracking.found)
        {
            curve += curveupdate;

            print(curve+"Curving");
            
        }
        
        if (curve >= (curvemax))
        {

            curve = 0;
            y2 = transform.position.y;
            transform.position = new Vector3(transform.position.x, y1, transform.position.z);
            tracking.found = false;
            tracking.shouldsearch = false;
            StartCoroutine("trackCD");
            print("track off");
            myBody.velocity = new Vector2(myBody.velocity.x, curve);
            hareketeBaşlandı = false;

        }

Note that using DateTime.Now and TimeSpan calculations as in this answer are quite expensive... especially if you use it every frame just to calculate a delta in seconds.

This is actually exactly what Unity already provides in Time.deltaTime the time in seconds passed since last few was rendered.. so why calculate it complicated if we already know the value;)

curve += curveupdate * Time.deltaTime;

In simple words multiplication by Time.deltaTime converts any value from "value per frame" into a frame-rate independent "value per second"

Instead of thinking about "rate of change," you must think about "rate of change per unit of time ".

For example, you seem to be updating curve based on curveupdate = 0.05f; , which has a rate of change of 0.05f . You need to be thinking about how much rate of change per second . Assuming you currently get 0.05f change per frame, and let's say 10 frames per second, that means your rate of change per second is 0.5f (ten times higher) per second.

You then apply this amount multiplied by the number of seconds elapsed. Here's how to do it.

First when you kick off the animation, you need to remember when it started:

var lastUpdateTime = DateTime.Now;

Then change this:

curve += curveupdate;

To this:

DateTime currentTime = DateTime.Now;
float secondsElapsed = (float)(currentTime - lastUpdateTime).TotalMilliseconds / 1000F;
curve += (curveupdate * secondsElaped);
lastUpdateTime = currentTime;

This way, the amount of change scales depending on how much time has passed. If no time has passed, it will scale all the way down to 0, and if hours have passed, it will end up very very large. To an end user, the rate of change should seem consistent over time as a result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM