- Home /
Player appears to teleport instead of adding force
I tried to add a simple jumping mechanic, The player does seem "jump", But instead of going up smoothly it just teleports up, Here's the code, Any help is appreciated.
public class PlayerMovement : MonoBehaviour
{
public Rigidbody2D RigidBody;
Vector2 Movement;
public float Speed = 10f;
public Animator Anim;
public float JumpForce;
public bool IsGrounded;
public Transform GroundCheck;
public float CheckRadius;
public LayerMask Ground;
void Start()
{
RigidBody = GetComponent<Rigidbody2D>();
}
void Update()
{
Movement.x = Input.GetAxisRaw("Horizontal");
Anim.SetFloat("Horizontal", Movement.x);
Anim.SetFloat("Speed", Movement.sqrMagnitude);
if (Movement.x > 0) {
transform.localScale = new Vector2 (10.0f, 10.0f);
} else if (Movement.x < 0) {
transform.localScale = new Vector2 (-10.0f, 10.0f);
}
if (Input.GetButtonDown("Jump") && IsGrounded == true) {
RigidBody.AddForce(transform.up * Time.deltaTime * JumpForce); //Here is the thing.
}
}
void FixedUpdate()
{
IsGrounded = Physics2D.OverlapCircle(GroundCheck.position, CheckRadius, Ground);
RigidBody.MovePosition(RigidBody.position + Movement * Speed * Time.fixedDeltaTime);
if (Input.GetButtonDown("Jump") && IsGrounded == true) {
RigidBody.AddForce(transform.up * JumpForce);
}
}
}
Answer by N-8-D-e-v · Jul 21, 2020 at 05:46 PM
You add force twice, once in update, and once in fixed update, this is probably causing your problems. FixedUpdate is called every physics step, so you want to use fixedupdate for using rigidbody/physics related things.
Answer by davidcox70 · Jul 21, 2020 at 05:54 PM
Apart from being added twice as @N8- pointed out, the addition in FixedUpdate has not been multiplied by time.deltaTime. For a jump, you don't really need to have this multiple, because a jump height is not dependent on frame rate. However, since time.deltaTime is likely to be a low fraction like 1/60, not having it in your fixed update section means you are actually adding a huge amount more force than perhaps you are expecting. DC
$$anonymous$$ore importantly, Time.deltaTime shouldn't be multiplied into either of those regardless.
First of all, it already is when using the default Force$$anonymous$$ode for Rigidbody(2D).AddForce().
Second, an instantaneous force (such as jumping) should be applied at its full strength:
// Apply an immediate force using Force$$anonymous$$ode2D.Impulse
// $$anonymous$$ultiply by the Rigidbody's mass to guarantee a fixed impulse
rb.AddForce(transform.up * jumpForce * rb.mass, Force$$anonymous$$ode2D.Impulse);
Third, "Rigidbody" is actually a pretty terrible name for your Rigidbody2D variable, since it conflicts with the 3D Rigidbody class name. (It doesn't mean DON'T use it, but that it would make things easier on you if you make a 3D game later)
Anyway, if you defined your jumping force based on putting it in Update(), then you'll want to divide the value by ~3000 (50 physics updates per second, 60 frames per second?) to get it into the range you're expecting.