0

I have a gameObject called player in the game and player can run, attack, roll, block etc. Some of them requires rigidbody interaction e.g, running, I'm changing rb.velocity if user pressed left or right arrow. But some of actions do not required rigidbody interaction such as blocking. So my Update and FixedUpdate functions look like this:

void Update()
    {
        direction = Input.GetAxisRaw("Horizontal");
        if (Input.GetMouseButtonDown(0))
        {
            Attack();
        }
        if (Input.GetMouseButtonDown(1))
        {
            Block();
        }
        if (Input.GetMouseButtonUp(1))
        {
            StopBlocking();
        }
        if (Input.GetKeyDown(KeyCode.Space))
        {
            isRolling = true;
        }
    }
    void FixedUpdate()
    {
        Flip();
        Run();
        Roll();
    }

So my question is, it this the right way of using Update and FixedUpdate?

Jantoma21
  • 395
  • 4
  • 10
  • 1
    Does this answer your question? [what is the difference between Update & FixedUpdate in Unity?](https://stackoverflow.com/questions/34447682/what-is-the-difference-between-update-fixedupdate-in-unity) – DevAm00 Jun 23 '23 at 11:40

3 Answers3

1
  1. Changing rigidbody.velocity directly is a bad practice. Use .AddForce() instead.
  2. From what you've described, you're using Update and FixedUpdate in right way.
  3. Your code has a lot to improve (of course). Maybe, to begin with, try to split the functionality to different classes (components), like MovementBahavior, AttackBehavior, BlockBehaviour, and also splitting each one from the input. In that way you'll be able to re-use behaviuors for NPCs and monsters. Build your game as a Lego, where every brick does something valuable, yet independent of others.

Be strong and patient and you'll get it. Good luck!

Xander
  • 378
  • 1
  • 6
  • 1
    Thanks for the answer, what is the reason of changing velocity is bad practice? Do you have any resource suggestion about topic 3 to improve myself? – Jantoma21 Jun 24 '23 at 09:00
  • Changing rigidbody's velocity means that you're interveining to the physics engine's work. It is already calculating everything for you, so in 99.99% of cases you just don't need to change the result, you'd rather must tune your input (forces and masses). And regarding the programming approach, there is a plenty of videos on YT on how to build project hierarchy specifically in Unity, and in C# in general, just start with the most viewed stuff like CodeMonkey, Brackeys etc, and you'll be able to better formulate your requests in the process. Also look: software architecture patterns. – Xander Jun 24 '23 at 10:25
  • If it happens so that you've don't need the movement physics in your game and only want to use trigger detection, you might want to set rigidbody to Kinematic and not touching the velocity, but instead calling rigidbody.MovePosition() right away. Unity has plenty of documentation and tutorials explaining how and when to use certain methods of object translation. – Xander Jun 24 '23 at 10:27
0

Any code written inside the Update() method will execute just before the next frame is rendered and the time between each execution csn vary greatly and is stored in Time.deltaTime.

Any code written inside the FixedUpdate() will be executed before each physics cycle/iteration and the time between them is fairly consistent and stored in Time.fixedDeltaTime

J. Murray
  • 1,460
  • 11
  • 19
Ashton Way
  • 46
  • 4
0

Update(): The Update() function is called every frame and is commonly used for regular updates and input handling. This function is ideal for code that doesn't require strict frame rate independence and needs to respond to player input or perform actions that are tied to frame updates. Examples include character movement, animation updates, or basic gameplay logic. It's important to note that the frequency of Update() calls may vary based on the frame rate of the game, so it's not suitable for physics-related calculations.

FixedUpdate(): The FixedUpdate() function is called at fixed intervals, typically synchronized with the physics system. It is mainly used for physics-related calculations and updates that need to be consistent regardless of the frame rate. FixedUpdate() is called in fixed time steps, making it suitable for applying forces, performing physics calculations, and updating rigidbodies. This function ensures that physics simulations are consistent across different platforms and frame rates.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 05 '23 at 12:24