My character can only jump when he is grounded, but he can only grounded in one direction I want him to jump in every direction, can anyone help?
CAN JUMP :
CAN'T JUMP :
Code:
using UnityEngine;
using System.Collections;
public class Player: MonoBehaviour {
public float maxspeed = 10f;
bool facingRight = true;
Animator anim;
bool grounded = false;
public Transform groundCheck;
float groundRadius = 0.2f;
public LayerMask whatIsGround;
public Vector2 jumpDirection = new Vector2(0f, 0f);
public float jumpFocre = 500f;
bool doubleJump = false;
void Start ()
{
anim = GetComponent ();
}
void FixedUpdate () {
grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
anim.SetBool ("Ground", grounded);
if (grounded)
doubleJump = false;
float move = Input.GetAxis ("Horizontal");
anim.SetFloat ("Speed", Mathf.Abs (move));
GetComponent().velocity = new Vector2(move * maxspeed, GetComponent().velocity.y);
}
void Update()
{
if((!doubleJump) && Input.GetKeyDown ("up"))
{
anim.SetBool("Ground", false);
float move = Input.GetAxis ("Horizontal");
jumpDirection.x = move;
GetComponent().AddForce(jumpDirection.normalized * jumpFocre);
if(!doubleJump && !grounded)
doubleJump = true;
}
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
No comments:
Post a Comment