Question by
gettinjacob · May 07, 2021 at 10:04 PM ·
bug-perhapsnotworking
Attack Overlapbox Not working
My Attack doesnt work like it should be, when i clicked the attack it damages the enemy even not being inside the collider. any help would be appreciated this bugged me for days
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pointweapon : MonoBehaviour
{
[Header("Weapon Hand")]
public GameObject _Weaponhand;
public Animator anim;
[Header("Dash")]
public Rigidbody2D playerds;
public float dashspeed = 500f;
[Header("Attack")]
public LayerMask _Whatenemy;
public float _AttackrangeX;
public float _AttackrangeY;
public int _Damage = 10;
public float _Gizmorange;
[Header("Attack Timer")]
public float _TimeBtwAttk;
public float _StartTimeBtwAttk;
private void Update()
{
if (_TimeBtwAttk <= 0)
{
Attack();
_TimeBtwAttk = _StartTimeBtwAttk;
}
else
{
_TimeBtwAttk -= Time.deltaTime;
}
}
private void FixedUpdate()
{
Pointer();
}
private void Pointer()
{
Vector3 _Difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
_Difference.Normalize();
float _rotationz = Mathf.Atan2(-_Difference.x, _Difference.y) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, _rotationz);
}
private void Attack()
{
if (Input.GetKey(KeyCode.X))
{
Collider2D[] _enemiestohurt = Physics2D.OverlapBoxAll(new Vector3(0,_Gizmorange,0), new Vector2(_AttackrangeX,_AttackrangeY), 0, _Whatenemy);
for (int i = 0; i < _enemiestohurt.Length; i++)
{
_enemiestohurt[i].GetComponent<Enemy>().Takedamage(_Damage);
}
}
anim.SetTrigger("idle");
}
private void dash()
{
Vector3 Dashpos = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
playerds.AddForce(Dashpos * dashspeed);
anim.SetTrigger("swing");
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.yellow;
Gizmos.matrix = this.transform.localToWorldMatrix;
Gizmos.DrawWireCube(new Vector3(0,_Gizmorange,0), new Vector3(_AttackrangeX,_AttackrangeY, 1));
//Gizmos.DrawWireCube(_Attackpos.position, new Vector3(_AttackrangeX,_AttackrangeY, 1));
}
}
help.png
(163.3 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613033422im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Switching shader back and forth fixes transparency issue? 0 Answers
Terrain Disappear 0 Answers
Trouble with rotating player when gravity is shifted 0 Answers
Issue when terrain or ground is not plane, a bug or programming? 0 Answers
How to close debuggers/decompilers/reflectors, including IDEs? 0 Answers