Files
Knight_Hollow/Assets/Scripts/Enemies/BossEnemyMove.cs
Mingu Kim 5b48bad730 플랫폼별 입력 화장성을 위해 Inputs구조체 추가 및 InputControoller 추가
- 캐릭터 이동이 정상적으로 작동하지 않는 문제 수정
2025-08-30 21:06:16 +09:00

91 lines
2.6 KiB
C#

using UnityEngine;
// public class AIInputController
// {
// public override void UpdateInput(ref Inputs inputs)
// {
// // Monsters 배열 정보 받아
// // 주변 환경 정보 받아오기
// // 환경 정보에 따라서 inputs 변경
// }
// }
public class BossEnemyMove : MonoBehaviour
{
// Start is called once before the first execution of Update after the MonoBehaviour is created
Rigidbody2D _rigidbody;
public int nextMove;
private SpriteRenderer _childSpriteRenderer;
void Start()
{
// 자식 오브젝트 찾기
Transform childTransform = transform.Find("Renderer");
if (childTransform != null)
{
// 자식 오브젝트에서 스프라이트 렌더러 컴포넌트 가져오기
_childSpriteRenderer = childTransform.GetComponent<SpriteRenderer>();
if (_childSpriteRenderer != null)
{
Debug.Log("자식 스프라이트 렌더러 찾음: " + _childSpriteRenderer.name);
}
else
{
Debug.LogWarning("자식 오브젝트에 스프라이트 렌더러 컴포넌트가 없습니다: ");
}
}
else
{
Debug.LogError("자식 오브젝트를 찾을 수 없습니다: ");
}
}
void Awake()
{
_rigidbody = GetComponent<Rigidbody2D>();
// GC Alloc 최소화
Invoke(nameof(Think), 5f); // 5초 후에 다시 생각 시작
}
private void FixedUpdate()
{
// 스프라이트 방향 이동 방향에 맞도록 좌우 플립
if (nextMove == 1)
{
_childSpriteRenderer.flipX = false;
}
else
{
_childSpriteRenderer.flipX = true;
}
// 이동
_rigidbody.linearVelocity = new Vector2(nextMove, _rigidbody.linearVelocityY);
// 지형 체크 (떨어짐 방지)
Vector2 frontVec = new Vector2(_rigidbody.position.x + nextMove, _rigidbody.position.y);
Debug.DrawRay(frontVec, Vector3.down, Color.green);
RaycastHit2D rayHit = Physics2D.Raycast(frontVec, Vector3.down, 5, LayerMask.GetMask("Platform"));
// 낭떠러지 체크
if(rayHit.collider == null)
{
Debug.Log("낭떠러지!");
}
}
// 생각 반복 재귀
void Think()
{
// random 값 범위 = -1, 0, 1
nextMove = Random.Range(-1, 2);
Invoke("Think", 5f); // 5초 후에 다시 생각 시작
}
}