Course Content
Fighting Game in Unity
Fighting Game in Unity
Move Your Player
FixedUpdate and Update
Update()
The Update()
method, called once per frame, handles regular updates like player input and user interaction. Its execution frequency varies with the frame rate, leading to potential inconsistencies across devices.
FixedUpdate()
The FixedUpdate()
method, called at fixed intervals, is ideal for physics-related updates, providing reliable and predictable handling of forces, collisions, and movement.
Use Update()
for regular updates that don't involve physics, like checking for user input or updating animations. Use FixedUpdate()
for physics-related updates, like moving objects with Rigidbody or handling collisions, to ensure smoother and more reliable behavior.
Movement Code
The code snippet involves various checks and assignments to control the movement of a game character. It starts by checking if the horizontal movement variable x
is not zero and then creates a Vector2
variable to store the Rigidbody component's current velocity. It also verifies if the current animation is not "attack" to determine if the character is in an attacking state. If not attacking, it sets the horizontal velocity based on the input, elapsed time, and speed; otherwise, it stops horizontal movement. Finally, it updates the Rigidbody's velocity with the new values.
Thanks for your feedback!