I have a 2D square sprite I'm using as the main character. Movement left and right work which I have in another script. the jump I'm putting in its own script.
I'm using Addforce on the rigidbody2d component attached to the sprite and it works. however the way he jumps is not ideal. The square teleports up then falls back down regularly. What I want is a natural looking jump, not a teleport. I've tried so many other methods after addforce and nothing works. Here is the script. I'm new at this so sorry if the script looks silly. I would really appreciate some help on this.
The addforce happens under FixedUpdate.
public class PlayerJumpModified : MonoBehaviour {
[SerializeField]
public Vector3 jumpForce = new Vector3(0f, 10f, 0f);
private BoxCollider2D playerCollider;
private Rigidbody2D playerRigidbody2D;
private bool playerGrounded = false;
// Use this for initialization
void Start () {
playerRigidbody2D = GetComponent ();
}
// Update is called once per frame
void FixedUpdate () {
if (playerGrounded == true) {
if (Input.GetKeyDown ("space")) {
playerRigidbody2D.AddForce(jumpForce, ForceMode2D.Force);
↧