보스 스크립트 추가(랜덤 좌우이동, 낭떠러지 체크)
This commit is contained in:
79
Assets/Scripts/Enemies/BossEnemyMove.cs
Normal file
79
Assets/Scripts/Enemies/BossEnemyMove.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using UnityEngine;
|
||||
|
||||
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>();
|
||||
|
||||
Invoke("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초 후에 다시 생각 시작
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Enemies/BossEnemyMove.cs.meta
Normal file
2
Assets/Scripts/Enemies/BossEnemyMove.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 69b79b76424d246f4b6e65cea814d386
|
||||
Reference in New Issue
Block a user