Okay this topic confuse the hell out of me since weeks. We have now a code: Code (CSharp): if (Input.GetButtonDown("Jump")) { myRigidbody.AddForce(new Vector2(0.0f, jumpForce), ForceMode2D.Impulse); } 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): float moveInput = Input.GetAxis("Horizontal"); 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): using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerInput : MonoBehaviour { Rigidbody2D myRigidbody; [SerializeField] float speed = 10; [SerializeField] float jumpForce = 10; [SerializeField] float groundCheckRadius = 0.01f; [SerializeField] Transform groundCheck; [SerializeField] LayerMask whatIsGround; bool isGrounded; bool canJump; bool jumpInput; Vector2 movement; void Start() { myRigidbody = GetComponent<Rigidbody2D>(); } private void Update() { float moveInput = Input.GetAxis("Horizontal"); movement = new Vector2(moveInput, 0.0f); myRigidbody.velocity = new Vector2(moveInput * (speed * Time.deltaTime * 100), myRigidbody.velocity.y); if (Input.GetButtonDown("Jump")) { jumpInput = true; } else { jumpInput = false; } } private void FixedUpdate() { //myRigidbody.AddForce(movement * speed); isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround); if (isGrounded) { canJump = true; } else { canJump = false; } if (canJump && jumpInput) { myRigidbody.AddForce(new Vector2(0.0f, jumpForce), ForceMode2D.Impulse); } Debug.Log(canJump); } }
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
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?
@Unity-Artcraft Think of FixedUpdate as the physics engine updates, and Update as the rendering engine updates. The physics engine usually needs to update more times than the rendering to have a better/consistent simulation of the world. Have you taken a look at Unity's tutorial where he whips up a controller for jumping, and he separates input (Update) and simulation (FixedUpdate)? https://unity3d.com/learn/tutorials/topics/2d-game-creation/player-controller-script?playlist=17093