I want to make it so the player can only attack while in mid-air and is not grounded. So when you first tap, he only jumps, but when you are in mid-air, he will attack. I tried and tried to figure out but could never solve it.
Player Script
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public float jumpPower = 555f;
public bool grounded = false;
private Rigidbody2D rb2d;
private playerAttack player;
void Start ()
{
rb2d = gameObject.GetComponent<Rigidbody2D>();
anim = gameObject.GetComponent<Animator>();
player = GameObject.Find("Player").GetComponent<playerAttack>();
}
void Update()
{
anim.SetBool("Grounded",grounded);
anim.SetFloat("Speed", Mathf.Abs(rb2d.velocity.x));
if (Input.GetMouseButtonDown(0) && grounded)
{
rb2d.AddForce(Vector2.up * jumpPower);
}
}
}
playerAttack Script
using UnityEngine;
using System.Collections;
public class playerAttack : MonoBehaviour {
private bool attacking = false;
private float attackTimer = 0;
private float attackCd = 0.125f;
public bool grounded = true;
public Collider2D attackTrigger;
private Animator anim;
private Player player;
void Start()
{
player = GameObject.Find("Player").GetComponent<Player>();
}
void Awake()
{
anim = gameObject.GetComponent<Animator>();
attackTrigger.enabled = false;
}
void Update()
{
if (Input.GetMouseButtonDown(0) && !attacking && // possibly another bool)
{
attacking = true;
attackTimer = attackCd;
attackTrigger.enabled = true;
}
if (attacking)
{
if(attackTimer > 0)
{
attackTimer -= Time.deltaTime;
}
else
{
attacking = false;
attackTrigger.enabled = false;
}
}
anim.SetBool("Attacking", attacking);
}
}
Ground Check Script
using UnityEngine;
using System.Collections;
public class GroundCheck : MonoBehaviour {
private Player player;
void Start()
{
player = gameObject.GetComponentInParent<Player>();
}
void OnTriggerEnter2D(Collider2D col)
{
player.grounded = true;
}
void OnTriggerStay2D(Collider2D col)
{
player.grounded = true;
}
void OnTriggerExit2D(Collider2D col)
{
player.grounded = false;
}
}