- Home /
Attack inputs like in Smash Bros
I'm trying to make a fighting game in Unity with directional input for attacks. I tried to make a dash attack and jab but both triggered whenever I tried to do the other one. Here is the code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour { public float speed; public float jumpForce; public Rigidbody2D rb; private float moveInput;
public Collider2D[] attackHitboxes; public static bool facingRight = true; public Animator animator; public float jabRecov;
public static bool isGrounded; public Transform groundCheck; public float checkRadius; public LayerMask whatIsGround;
public static int extraJumps; public int extraJumpsValue;
public float currentVitality; public bool isMoving = false; public bool Hook = false;
// Start is called before the first frame update
void Start()
{
extraJumps = extraJumpsValue;
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate(){
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
moveInput = Input.GetAxis("Horizontal");
this.transform.Translate(Input.GetAxis("Horizontal"),0,0);
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if(facingRight == false&&moveInput>0) {Flip(); } else if(facingRight == true && moveInput<0) { Flip();
} }
void Update(){
if(isGrounded == true){
extraJumps = extraJumpsValue;
} if(Input.GetKeyDown(KeyCode.W) && extraJumps > 0){ StartCoroutine("jumpSquat");
} else if(Input.GetKeyDown(KeyCode.W) && extraJumps == 0 && isGrounded == true){
extraJumps+=extraJumpsValue;
}
if(Input.GetButtonDown("Fire1")&&isGrounded == true&&moveInput == 0){
jab(attackHitboxes[0]);
//StartCoroutine("jabAnimate");
}
if(Input.GetButtonDown("Fire1")&&isGrounded == true&&moveInput != 0){
hook(attackHitboxes[1]);
}
if(Input.GetKeyDown(KeyCode.S)&&isGrounded == false){
rb.gravityScale = 25;
}
if(Input.GetKeyDown(KeyCode.W)&&isGrounded == false){
rb.gravityScale = 5;
}
if(isGrounded == true){
rb.gravityScale = 10;
}
if(Input.GetKeyUp(KeyCode.S)&&isGrounded == true && Input.GetKeyUp(KeyCode.S)&&isGrounded == true){
rb.gravityScale = 10;
}
}
void jab(Collider2D col)
{
Collider2D[] cols = Physics2D.OverlapBoxAll(col.bounds.center, col.bounds.extents, LayerMask.GetMask("Hitbox"));
foreach(Collider2D c in cols)
{ Debug.Log(c.name); if(c.transform.parent.parent == transform){ continue; } float jabDamage = 1; float knockback = 1; switch(c.name) { case "Body": jabDamage = 1; knockback = 1; break; } c.SendMessageUpwards("TakeDamage", jabDamage); c.SendMessageUpwards("GetHit", knockback);
}
} void hook(Collider2D col) {
Collider2D[] cols = Physics2D.OverlapBoxAll(col.bounds.center, col.bounds.extents, LayerMask.GetMask("Hitbox"));
foreach(Collider2D c in cols)
{ Debug.Log(c.name); if(c.transform.parent.parent == transform){ continue; } float hookDamage = 1; float hookKnockback = 1; switch(c.name) { case "Body": hookDamage = 5; hookKnockback = 1; break; } c.SendMessageUpwards("TakeDamage", hookDamage); c.SendMessageUpwards("GetHit", hookKnockback);
}
}
IEnumerator jumpSquat(){
yield return new WaitForSeconds(7*Time.deltaTime); rb.velocity = Vector2.up * jumpForce; isGrounded = false; extraJumps--;
}
void Flip(){
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
}
Answer by mhmhmhm · Sep 03, 2020 at 05:56 PM
mhmhmhm, your hitbox code is in a foreach loop. You rerun the code for your array and that is why your code is janky.
Your answer
Follow this Question
Related Questions
How to give the knife or sword to the human. 1 Answer
Text do action when clicked 1 Answer
Unityaction vs Method 1 Answer
Destroy action deleted the original gameObject 1 Answer
Blender Actions Import 1 Answer