i've been working on a game for university for some time now and when it came to making an enemy detecting player in order to attack him i hit a wall. Enemy just can't see the player and in fact he can not see anything. I tried with all the layers and objects but he just stands still.(I still have not implemented any kind of movement to him since i want to fix that issue first). Only way to make him attack was to negate 'if (PlayerInSight())' which makes sense since it returns true all the time now so that doesn't really help. Here is the whole script for melee enemy at the moment. Help appreciated.
using UnityEngine;
public class Melee : MonoBehaviour
{
[SerializeField] private float attackCooldown;
[SerializeField] private float range;
[SerializeField] private float colliderDistance;
[SerializeField] private int damage;
[SerializeField] private BoxCollider2D boxCollider;
[SerializeField] private LayerMask playerLayer;
private float cooldownTimer = Mathf.Infinity;
private Animator anim;
private Health playerHealth;
private void Awake()
{
anim = GetComponent<Animator>();
}
private void Update()
{
cooldownTimer = Time.deltaTime;
//atakuj tylko kiedy przeciwnik widzi gracza
if (PlayerInSight())
{
if (cooldownTimer >= attackCooldown)
{
cooldownTimer = 0;
anim.SetTrigger("Attack");
}
}
}
private bool PlayerInSight()
{
RaycastHit2D hit =
Physics2D.BoxCast(boxCollider.bounds.center transform.right * range * transform.localScale.x * colliderDistance,
new Vector3(boxCollider.bounds.size.x * range, boxCollider.bounds.size.y, boxCollider.bounds.size.z),
0, Vector2.left, 0, playerLayer);
if (hit.collider != null)
playerHealth = hit.transform.GetComponent<Health>();
return hit.collider != null;
}
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawWireCube(boxCollider.bounds.center transform.right * range * transform.localScale.x * colliderDistance,
new Vector3(boxCollider.bounds.size.x * range, boxCollider.bounds.size.y, boxCollider.bounds.size.z));
}
private void DamagePlayer()
{
if (PlayerInSight())
playerHealth.TakeDamage(damage);
}
}
CodePudding user response:
The parameters of Physics2D.BoxCast
and what you are passing in are
origin = boxCollider.bounds.center transform.right * range * transform.localScale.x * colliderDistance,
size = new Vector3(boxCollider.bounds.size.x * range, boxCollider.bounds.size.y, boxCollider.bounds.size.z),
angle = 0,
direction = Vector2.left,
distance = 0,
layerMask = playerLayer
So as you can see your cast always has a maximum distance of 0
=> quite clear that you don't hit anything with that.
You might be looking for Physics2D.OverlapBox
for checking only a single box shape at a fix position
CodePudding user response:
At first i thought that i may just change the distance then but that does not work either. I've been following a tutorial and the distance there is set to 0 as well but it works just fine. On the other hand changing that to OverlapBox wouldn't mean that the box stays still no matter what? I wanted it to move with the enemy that is why i used BoxCast. I tried to change it to OverlapBox anyway but wasn't successful but i'm pretty sure i got something wrong there. I'm a big beginner so there is still a lot of stuff i don't understand. Here is the fragment with OverlapBox
private bool PlayerInSight()
{
Collider2D hit =
Physics2D.OverlapBox(boxCollider.bounds.center transform.right * range * transform.localScale.x * colliderDistance,
new Vector3(boxCollider.bounds.size.x * range, boxCollider.bounds.size.y, boxCollider.bounds.size.z),
0, playerLayer);
if (hit.IsTouching(boxCollider))
playerHealth = hit.transform.GetComponent<Health>();
return hit.IsTouching(boxCollider);
}