Infamous Jox
Infamous Jox

Reputation: 95

Is it right to use FixedUpdate in my case?

I hope my question isn't too dumb, I am not very familiar with FixedUpdate. I know FixedUpdate is usually used with physics, but in my game, I have the following problem : when it runs at a low fps rate, the enemies skip checkpoints (because they move too much in a frame). The movements method :

    void FixedUpdate()
    {
        TestIfCloseToOtherZombie();
        //chopper la direction dans laquelle aller
        Vector3 dir = target.position - transform.position;
        //déplacer l'enemy
        transform.Translate(dir.normalized * enemy.speed * Time.fixedDeltaTime, Space.World);
        Vector3 rotDir = new Vector3(dir.x, 0, dir.z);
        if(rotDir != Vector3.zero)
        {
            Quaternion lookRotation =  Quaternion.LookRotation(rotDir);
            transform.rotation = Quaternion.Lerp(transform.rotation, lookRotation, turnSpeed*Time.fixedDeltaTime);
        }

        //marge d'erreur pour éviter qu'il bug dans le waypoint (+ de speed = + de  marge sinon bug)
        if(Vector3.Distance(transform.position, target.position) <= enemy.wayPointPrecision){
            GetNextWaypoint();
        }

        if(!isToClose)
        {
            enemy.speed = enemy.baseSpeed;
        }
        
        GetCurrentPosition();
    }

Target is the checkpoint the enemy is going to. Previously, I used a wayPointPrecision value, to solve the problem. But when the fps are way to low, it's not enough.

I just want to know if FixedUpdate could be used to make sure the movements are calculated independently from framerate, even if it does not use physics.

Upvotes: 0

Views: 595

Answers (3)

VPellen
VPellen

Reputation: 1099

The purpose of FixedUpdate is not "Physics" per se - The purpose of FixedUpdate determinism. If you're moving an object in a way that is mechanically significant (That is, the movement of that object has an impact on how the game plays, rather than simply how the game looks or feels), then that behavior probably belongs in FixedUpdate. This always applies to physics, but it also applies to basically anything that might involve a collision check of any kind.

Upvotes: 0

Infamous Jox
Infamous Jox

Reputation: 95

After trying some different ways, everything works fine using InvokeRepeating() instead of update or fixedupdate.

Upvotes: 0

rokgin
rokgin

Reputation: 156

FixedUpdate in your situation will not solve the issue, however you could use MoveTowards() instead that will not overshoot the target, in your example it could be as simple as:

transform.position = Vector3.MoveTowards(transform.position, target.position, Time.deltaTime * 2);

Upvotes: 1

Related Questions