Question by
Brandoboy · Aug 23, 2016 at 01:30 AM ·
c#jumpnot working
Player in Unity refuses to Jump
ok, so I was following a tutorial on how to make a 2d platformer, and after the video that we redid the code for the jump functionality nothing happens when i press spacebar!
code in C#!!
here is the jump behavior code: using UnityEngine; using System.Collections;
public class JumpBehavior : StateMachineBehaviour {
// OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
Player.Instance.Jump = true;
}
// OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
//override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
//
//}
// OnStateExit is called when a transition ends and the state machine finishes evaluating this state
override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
Player.Instance.Jump = false;
}
// OnStateMove is called right after Animator.OnAnimatorMove(). Code that processes and affects root motion should be implemented here
//override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
//
//}
// OnStateIK is called right after Animator.OnAnimatorIK(). Code that sets up animation IK (inverse kinematics) should be implemented here.
//override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
//
//}
}
and here is the player code:
using UnityEngine;
using System.Collections;
public class Player : Character
{
private static Player instance;
public static Player Instance
{
get
{
if (instance == null)
{
instance = GameObject.FindObjectOfType<Player>();
}
return instance;
}
set
{
instance = value;
}
}
[SerializeField]
private Transform[] groundPoints;
[SerializeField]
private float groundRadius;
[SerializeField]
private LayerMask whatIsGround;
[SerializeField]
private bool airControl;
[SerializeField]
private float jumpForce;
public Rigidbody2D MyRigidbody { get; set; }
public bool Slide { get; set; }
public bool Jump { get; set; }
public bool OnGround { get; set; }
// Use this for initialization
public override void Start()
{
base.Start();
MyRigidbody = GetComponent<Rigidbody2D>();
}
void Update()
{
handleInput();
}
// Update is called once per frame
void FixedUpdate()
{
float horizontal = Input.GetAxis("Horizontal");
OnGround = IsGrounded();
HandleMovement(horizontal);
Flip(horizontal);
HandleLayers();
}
private void HandleMovement(float horizontal)
{
if (MyRigidbody.velocity.y < 0)
{
myAnimator.SetBool("land", true);
}
if (!Attack && !Slide && (OnGround || airControl))
{
MyRigidbody.velocity = new Vector2(horizontal * movementSpeed, MyRigidbody.velocity.y);
}
if (Jump && MyRigidbody.velocity.y == 0)
{
MyRigidbody.AddForce(new Vector2(0, jumpForce));
}
myAnimator.SetFloat("speed", Mathf.Abs(horizontal));
}
private void handleInput()
{
if (Input.GetKeyDown(KeyCode.Space))
{
myAnimator.SetTrigger("jump");
}
if (Input.GetKeyDown(KeyCode.Mouse0))
{
myAnimator.SetTrigger("attack");
}
if (Input.GetKeyDown(KeyCode.LeftShift))
{
myAnimator.SetTrigger("slide");
}
if (Input.GetKeyDown(KeyCode.Mouse1))
{
myAnimator.SetTrigger("throw");
}
}
private void Flip(float horizontal)
{
if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
{
ChangeDirection();
}
}
private bool IsGrounded()
{
if (MyRigidbody.velocity.y <= 0)
{
foreach (Transform point in groundPoints)
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject != gameObject)
{
return true;
}
}
}
}
return false;
}
private void HandleLayers()
{
if (!OnGround)
{
myAnimator.SetLayerWeight(1, 1);
}
else
{
myAnimator.SetLayerWeight(1, 0);
}
}
public override void ThrowKnife(int value)
{
if (!OnGround && value == 1 || OnGround && value == 0)
{
base.ThrowKnife(value);
}
}
}
Comment