I'm new to Unity.
I have Rigidbody2D
and I want to add constant velocity
to my player.
I want to know whats are differences or advantages/disadvantages or bast practice in applying velocity to Rigidbody2D
in Start
or Update
(or FixedUpate
)
When I apply velocity on start everything works fine, so why we add velocity every frame ( Update
method ) ?
I want to add constant velocity to my player.
The Start
function is eliminated in this case since it's called once only when the script is GameObject and script enabled. If you need to apply force over and over again then the Start
function is not something you should use.
The Update
function is used to do anything every frame. For example, manually moving object every frame.
The FixedUpdate
function is used to do something to Rigidbody
and this includes adding force to it every fixed frame. This is what you need to use since you're doing something to a Rigidbody
object.
The other one you didn't mention is the LateUpdate
function. This is used when you want to make a GameObject follow a camera since it's calld after all Update
functions have been called.
Unity also has a ConstantForce
utility to simplifies adding constant force to Rigidbody
. With it, you can add force once to ConstantForce
in the Start
function and it will handle the rest until you change the force.
ConstantForce2D cForce = targetObj.GetComponent<ConstantForce2D>();
cForce.force = new Vector2(0, 100);