- Home /
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.
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!
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
Follow this Question
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