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();
}
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);
}
}
}
↧