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:
- Is it expensive to have a
FixedUpdate()
method that checks forfalse
value and returns every frame? - Which of the two methods performs better in this case?