- Home /
Question by
DerSeb · Jun 08, 2015 at 07:32 AM ·
jumpjumpingplayer movementkey
Player cannot jump
Hey Guys. could you please help me because my player doesnt want to jump if I press a key. Thanks.
using UnityEngine; using System.Collections;
public class PlayerMove : MonoBehaviour {
public float maxSpeed = 4;
public float jumpForce = 550;
public Transform groundCheck;
public bool lookingRight = true;
public LayerMask whatIsGround;
private Rigidbody2D rb2d;
private Animator anim;
private bool IsGrounded = true;
private bool Jump = false;
void Start () {
rb2d = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
void Upate() {
if (Input.GetButtonDown ("Jump") && IsGrounded) {
Jump = true;
}
}
void FixedUpdate() {
float hor = Input.GetAxis ("Horizontal");
anim.SetFloat ("Speed", Mathf.Abs (hor));
rb2d.velocity = new Vector2 (hor * maxSpeed, rb2d.velocity.y);
IsGrounded = Physics2D.OverlapCircle (groundCheck.position, 0.15F, whatIsGround);
anim.SetBool("IsGrounded", IsGrounded);
if ((hor > 0 && !lookingRight) || (hor < 0 && lookingRight))
Flip ();
if (Jump) {
rb2d.AddForce(new Vector2 (0,jumpForce));
Jump = false;
}
}
public void Flip() {
lookingRight = !lookingRight;
Vector3 myScale = transform.localScale;
myScale.x *= -1;
transform.localScale = myScale;
}
}
Comment