0
votes

I'm developing a simple 2D game in Unity (balls hitting bricks), so i have a ball and i want to fire the ball to hit bricks, now to fire the ball i have to set Velocity on the RigidBody component attached to the ball's GameObject.

There are two ways to do that:

Method 1:

Set the velocity inside the Start() method

private void Start()
{
    rb.velocity = direction * speed;
}

Method 2:

Write a FixedUpdate() method and put the statement in there. because:

Use FixedUpdate when using Rigidbody.(Unity Docs)

private void Start()
{
    _setVelocity = true;
}

private void FixedUpdate()
{
    if (!_setVelocity) return;

    rb.velocity = direction * speed;
    rb.angularVelocity = 0;
    rb.gravityScale = 0;
    _setVelocity = false;
}

But the thing is: i don't need the FixedUpdate() method for anything else, because i only fire the ball once in its script's lifespan, so after i fire the ball unity will continue to run the FixedUpdate()method and check for _setVelocity's value and return (exit) every time because it will always be false after i shoot the ball the first time.

And this will happen every fixed frame-rate frame which is expensive i guess for just setting the velocity only once.

SO:

  1. Is it expensive to have a FixedUpdate() method that checks for false value and returns every frame?
  2. Which of the two methods performs better in this case?
1
Is there any particular reason you considered doing it in FixedUpdate? For anything you only do once at the start of the GameObject's life you want to be thinking about using Start or Awake. Doing a pointless if statement would theoretically have a performance impact but it would be miniscule. Only issue is why did it seem to you that it might be necessary?Rocketman Dan
I know what Fixed update is. Point is you should normally only be thinking of using FixedUpdate for physics stuff that is happening throughout the lifespan of the object, rather than one time velocity initialisations.Rocketman Dan

1 Answers

1
votes

Is it expensive to have a FixedUpdate() method that checks for false value and returns every frame ?

A simple boolean condition such as yours is not that greedy so do not worry about that. However, any line of code that runs decrease your performances. The Update() method itself is more greedy in terms of performance than your "trivial" condition. Disabling your component script or removing the object itself is more optimize than keep it running for nothing. Just keep that in mind :)

Which of the two methods performs better in this case ?

The first one is more efficient because your velocity is fixed, so there is no point of using an update method (Update() or FixedUpdate()). You should initialize your rigidbody's velocity directly in your Start() method as you did in your post, so you can get rid of your boolean and its condition.

Unity's documentation is pretty clear about rigidbody and FixedUpdate() but I have the impression that you misunderstood some aspects. They tell you that if you want to modify any data of your rigidbody during the run-time, prefer to use FixedUpdate() instead of Update() to avoid weird behavior.

But why should you use FixedUpdate() instead of Update() ?

Because Update() is called at each frame, so if your pc runs at 100 fps, Update() will be called 100 times. And if your pc runs at 200 fps, it'll be called 200 times. It is kind of problematic in some cases, especially when you interact with physics components.
In order to understand, lets admit that we want to apply a force to our rigidbody (rb.AddForce(100)). If you call it in your Update() method and your pc runs at 100 fps, you'll apply 10000 force in one second. However, if your pc runs at 50 fps, then you'll apply 5000 force in one second. It'll cause you some weird behavior that could be avoided thanks to FixedUpdate(), which is called at a fix number of times per second (see unity doc).
However, there is no point of using FixedUpdate() with your rigidbody component if you do not want to modify it. They tell you that if you want to update your rigidbody component, you should do it inside the FixedUpdate method instead of Update().

So for your question, you can simply do the following:

Awake() {
    // getting your components
    rb = GetComponent<Rigidbody2D>();
}

Start() {
    // initiliaze your components
    rb.velocity = direction * speed;
    rb.angularVelocity = 0;
    rb.gravityScale = 0;
}

Hoping that I've helped you, good luck with your project !