Sunday, June 21, 2015

c# - How do I make my character jump in every direction?


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 :


enter image description here


CAN'T JUMP :



enter image description here


enter image description here


enter image description here


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

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...