Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 bucho_junior · Feb 27, 2020 at 10:18 AM · scripting problem2d gamehitboxcrouching

How can I make my character crouch and attack?

I have this code for player combat. I have it set to attack and play an animation then detect enemy colliders in the box. I have two separate hitEnemies for when chrouching and not. I can't find a way to get my character to crouch and attack and get the debug.log to activate.

Here is the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
  
 public class Player_combat : MonoBehaviour
 {
  
     public Animator animator;
  
     public Transform attackPoint;
     public Vector2 swordSize;
     public int swordCollider = 5;
     public int swordCollider2 = 5;
     public LayerMask enemyLayers;
     public Transform attackPoint2;
     public Vector2 swordSize2;
  
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.N))
         {
             Attack();
         }
     }
  
 void Attack()
     {
         //Play animation
         animator.SetTrigger("Attack");
  
         //Detect enemies in range of box
         Collider2D[] hitEnemies = Physics2D.OverlapBoxAll(attackPoint.position, swordSize, swordCollider, enemyLayers);
         Collider2D[] hitEnemies2 = Physics2D.OverlapBoxAll(attackPoint2.position, swordSize2, swordCollider, enemyLayers);
         //Damage them
         foreach (Collider2D enemy in hitEnemies)
         {
             Debug.Log("We hit" + enemy.name);
         }
         foreach (Collider2D enemy in hitEnemies2)
         if (Input.GetButtonDown("Crouch"))
         {
             Debug.Log("We hit" + enemy.name);
         }
     }
     private void OnDrawGizmosSelected()
     {
         if (attackPoint == null)
             return;
         Gizmos.DrawWireCube(attackPoint.position, swordSize);
         if (attackPoint2 == null)
             return;
         Gizmos.DrawWireCube(attackPoint2.position, swordSize2);
     }
  
 }

I have this code for player combat. I have it set to attack and play an animation then detect enemy colliders in the box. I have two separate hitEnemies for when chrouching and not. I can't find a way to get my character to crouch and attack and get the debug.log to activate.

Comment
Add comment · Show 1
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
avatar image bucho_junior · Feb 27, 2020 at 01:06 AM 0
Share

WOW! that code did not end up the way I thought.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ashkanaral · Feb 27, 2020 at 07:37 PM

I think you have two types of key actions that are conflicting the coding of the player to crouch.

One telling to crouch by pressing Key.Code N

Then, you check with a button "Crouch"

I do not think you need the second if statement with "Crouch" since method Attack() is inside Update() unless KeyCode.N is different than crouch.

Just take out the second if statement with "Crouch"

  if (Input.GetButtonDown("Crouch"))

if you have two different key actions. You are going to need to separate the two key actions or else it will not work. You are not pressing one button. You are pressing two buttons. So you have to code it separately.

Just make Attack() methods Attack1() and Attack2() with different key actions KeyCode.N

Good Luck!

Comment
Add comment · Show 1 · 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
avatar image bucho_junior · Mar 01, 2020 at 04:33 AM 0
Share

Sorry I answered so late but I have lot's of stuff in school going on. Anyway I still can't figure it out! This is my updated script.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Player_combat : $$anonymous$$onoBehaviour {

 public Animator animator;

 public Transform attackPoint;
 public Vector2 swordSize;
 public int swordCollider = 5;
 public int swordCollider2 = 5;
 public Layer$$anonymous$$ask enemyLayers;
 public Transform attackPoint2;
 public Vector2 swordSize2;

 // Update is called once per frame
 void Update()
 {
     if (Input.GetButtonDown("Attack"))
     {
         Attack();
     }
     if (Input.GetButtonDown("Attack") && Input.GetButtonDown("Crouch"))
     {
         CrouchAttack();
     }
 }
 void Attack()
 {
     //Play animation
     animator.SetTrigger("Attack");

     //Detect enemies in range of box
     Collider2D[] hitEnemies = Physics2D.OverlapBoxAll(attackPoint.position, swordSize, swordCollider, enemyLayers);
     //Damage them
     foreach (Collider2D enemy in hitEnemies)
     {
         Debug.Log("We hit" + enemy.name);
     }
 }
 void CrouchAttack()
 {
     animator.SetTrigger("Seth crouching attack animation 2");
     Collider2D[] hitEnemies2 = Physics2D.OverlapBoxAll(attackPoint2.position, swordSize2, swordCollider2, enemyLayers);
     foreach (Collider2D enemy in hitEnemies2)
     {
         Debug.Log("We hit" + enemy.name);
     }
 }
 private void OnDrawGizmosSelected()
 {
     if (attackPoint == null)
         return;
     Gizmos.DrawWireCube(attackPoint.position, swordSize);
     if (attackPoint2 == null)
         return;
     Gizmos.DrawWireCube(attackPoint2.position, swordSize2);
 }

}

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

243 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 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

Hello can anyone help me in my code i am converting some code JS to C# 1 Answer

How to check if BoxCollider2D collided with another BoxCollider2D? 2 Answers

How can i spawn and Despawn for a specific time using this script? 2 Answers

Hello can anyone help me i am new in scripting 1 Answer

Find Tilemap Player is Currently At 0 Answers


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