Search Unity

Fixed Update and Update?

Discussion in '2D' started by Unity-Artcraft, Apr 25, 2019.

  1. Unity-Artcraft

    Unity-Artcraft

    Joined:
    Jul 28, 2018
    Posts:
    85
    Okay this topic confuse the hell out of me since weeks. We have now a code:

    Code (CSharp):
    1. if (Input.GetButtonDown("Jump"))
    2. {
    3. myRigidbody.AddForce(new Vector2(0.0f, jumpForce), ForceMode2D.Impulse);
    4. }
    Okay we have two things here. An Input who belongs in Update and an Addforce, belongs in FixedUpdate... so how do you seperate them? Because according to Unity:

    https://docs.unity3d.com/Manual/ExecutionOrder.html

    Fixed Update comes first and Update second.
    ...So you have to apply force first and then check for Inputs? What? ho... I'm really confused right now.

    Second we have this example:

    Code (CSharp):
    1.  float moveInput = Input.GetAxis("Horizontal");
    2.  
    3. myRigidbody.velocity = new Vector2(moveInput * (speed * Time.deltaTime * 100), myRigidbody.velocity.y);
    I can't put the second line in Fixed Update because moveInput is in another method...

    I tried to seperate everything nice and clean an my jump still doesn't work occasionally.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerInput : MonoBehaviour
    6. {
    7.     Rigidbody2D myRigidbody;
    8.  
    9.     [SerializeField] float speed = 10;
    10.     [SerializeField] float jumpForce = 10;
    11.     [SerializeField] float groundCheckRadius = 0.01f;
    12.     [SerializeField] Transform groundCheck;
    13.     [SerializeField] LayerMask whatIsGround;
    14.  
    15.     bool isGrounded;
    16.     bool canJump;
    17.     bool jumpInput;
    18.     Vector2 movement;
    19.  
    20.     void Start()
    21.     {
    22.         myRigidbody = GetComponent<Rigidbody2D>();
    23.     }
    24.  
    25.     private void Update()
    26.     {
    27.         float moveInput = Input.GetAxis("Horizontal");
    28.  
    29.         movement = new Vector2(moveInput, 0.0f);
    30.         myRigidbody.velocity = new Vector2(moveInput * (speed * Time.deltaTime * 100), myRigidbody.velocity.y);
    31.  
    32.         if (Input.GetButtonDown("Jump"))
    33.         {
    34.             jumpInput = true;
    35.         }
    36.         else
    37.         {
    38.             jumpInput = false;
    39.         }
    40.     }
    41.  
    42.     private void FixedUpdate()
    43.     {
    44.         //myRigidbody.AddForce(movement * speed);
    45.  
    46.         isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
    47.  
    48.         if (isGrounded)
    49.         {
    50.             canJump = true;
    51.         }
    52.         else
    53.         {
    54.             canJump = false;
    55.         }
    56.  
    57.         if (canJump && jumpInput)
    58.         {
    59.             myRigidbody.AddForce(new Vector2(0.0f, jumpForce), ForceMode2D.Impulse);
    60.         }
    61.  
    62.         Debug.Log(canJump);
    63.     }
    64. }
    65.  
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Since it's a one-time thing based on a button press, you can just put AddForce in Update in this case. FixedUpdate is for physics stuff that occurs every physics frame (FixedUpdate) that would be inappropriate to do every frame (Update).

    --Eric
     
  3. Unity-Artcraft

    Unity-Artcraft

    Joined:
    Jul 28, 2018
    Posts:
    85
    Okay, that's fine if you jump but what if you want to move? In this case you have a Input and AddForce updating every frame?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You'd use FixedUpdate.

    --Eric
     
  5. xenonmiii

    xenonmiii

    Joined:
    Aug 2, 2010
    Posts:
    147