Preciso de uma ajuda, o procedimento que fizemos para detectar a presença do player, acusa que está true mesmo que o player não esteja tocando a área do detector.
Vou deixar aqui todos os códigos, que estão iguais aos da aula, não sei por que isso está ocorrendo:
BaseEnemy.cs
using UnityEngine;
public abstract class BaseEnemy : MonoBehaviour
{
protected Animator animator;
protected virtual void Awake()
{
animator = GetComponent<Animator>();
}
protected abstract void Update();
}
MeleeEnemy.cs
using UnityEngine;
public class MeleeEnemy : BaseEnemy
{
[SerializeField] private Transform detectPosition;
[SerializeField] private Vector2 detectBoxSize;
[SerializeField] private LayerMask playerLayer;
protected override void Update()
{
print("is in sight? " + PlayerInSight());
}
private bool PlayerInSight()
{
Collider2D playerCollider = Physics2D.OverlapBox(detectPosition.position, detectBoxSize, 0f, playerLayer);
return playerCollider != null;
}
private void OnDrawGizmos()
{
if (detectPosition == null) return;
Gizmos.color = Color.red;
Gizmos.DrawWireCube(detectPosition.position, detectBoxSize);
}
}
InputManager
using System;
using UnityEngine.InputSystem;
public class InputManager
{
private PlayerControls playerControls;
public float Movement => playerControls.Gameplay.Movement.ReadValue<float>();
public event Action OnJump;
public InputManager()
{
playerControls = new PlayerControls();
playerControls.Gameplay.Enable();
playerControls.Gameplay.Jump.performed += OnJumpPerformed;
}
private void OnJumpPerformed(InputAction.CallbackContext context)
{
OnJump?.Invoke();
}
}
PlayerBehavior.cs
using UnityEngine;
public class PlayerBehavior : MonoBehaviour
{
[SerializeField] private float moveSpeed = 5;
[SerializeField] private float jumpForce = 3;
private float moveDirection;
private Rigidbody2D rigidbody;
private IsGroundedChecker isGroundedChecker;
private void Awake()
{
rigidbody = GetComponent<Rigidbody2D>();
isGroundedChecker = GetComponent<IsGroundedChecker>();
}
private void Start()
{
GameManager.Instance.InputManager.OnJump += HandleJump;
}
private void Update()
{
MovePlayer();
FlipSpriteAccordingToMoveDirection();
}
private void MovePlayer()
{
moveDirection = GameManager.Instance.InputManager.Movement;
transform.Translate(moveDirection * Time.deltaTime * moveSpeed, 0, 0);
}
private void FlipSpriteAccordingToMoveDirection()
{
if (moveDirection < 0)
{
transform.localScale = new Vector3(-1, 1, 1);
}
else if (moveDirection > 0)
{
transform.localScale = Vector3.one;
}
}
private void HandleJump()
{
if (isGroundedChecker.IsGrounded() == false) return;
rigidbody.velocity += Vector2.up * jumpForce;
}
}
PlayerAnim.cs
using UnityEngine;
public class PlayerAnim : MonoBehaviour
{
private Animator animator;
private IsGroundedChecker groundedChecker;
private void Awake()
{
animator = GetComponent<Animator>();
groundedChecker = GetComponent<IsGroundedChecker>();
}
private void Update()
{
bool isMoving = GameManager.Instance.InputManager.Movement != 0;
animator.SetBool("isMoving", isMoving);
animator.SetBool("isJumping", !groundedChecker.IsGrounded());
}
}
IsGroundedChecker.Cs
using UnityEngine;
public class IsGroundedChecker : MonoBehaviour
{
[SerializeField] private Transform checkerPosition;
[SerializeField] private Vector2 checkerSize;
[SerializeField] private LayerMask groundLayer;
public bool IsGrounded()
{
return Physics2D.OverlapBox(checkerPosition.position, checkerSize, 0f, groundLayer);
}
private void OnDrawGizmos()
{
if (checkerPosition == null) return;
if (IsGrounded())
{
Gizmos.color = Color.red;
}
else
{
Gizmos.color = Color.green;
}
Gizmos.DrawWireCube(checkerPosition.position, checkerSize);
}
}
Aclaro que ambos, player, e inimigo, estão na layer do Player, conforme visto no vídeo do professor! Alguém sabe o que pode estar errado? Desde já, muito obrigado! <3