Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mhmhmhm · Aug 30, 2020 at 09:48 PM · action

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;
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

132 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges