void FixedUpdate()
{
if (currentState != AnimationState.Die)
{
Move();
DetectEnemies();
}
HandleAnimation();
}
void Move()
{
Vector3 moveDirection = new Vector3(-joystick.Vertical, 0, joystick.Horizontal).normalized;
if (moveDirection != Vector3.zero)
{
Quaternion toRotation = Quaternion.LookRotation(moveDirection, Vector3.up);
transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, 360f * Time.deltaTime);
rb.velocity = moveDirection * moveSpeed; // velocity를 사용하여 움직임 제어
currentState = AnimationState.Run;
}
else
{
rb.velocity = Vector3.zero; // 움직임이 없을 때는 속도를 0으로 설정
if (targetedEnemy != null)
{
currentState = AnimationState.Attack;
LookAtEnemy(targetedEnemy.transform);
}
else
{
currentState = AnimationState.Idle;
}
}
}
1. 플레이어의 움직임을 처리하기 위해 Rigidbody.velocity를 사용
2. FixedUpdate() 내에서 직접 Rigidbody의 속도(velocity)를 설정하여 움직임을 제어
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
public class Player : MonoBehaviour
{
public Joystick joystick;
public float moveSpeed = 5.0f;
public float detectionRadius = 5.0f; // 적을 탐지하는 범위
public LayerMask enemyLayer;
public LayerMask wallLayer;
public float rayHeight = 1.0f;
private AnimationState currentState;
private Animator animator;
private EnemyBase targetedEnemy = null;
// Ray의 시작점과 끝점을 저장할 변수
private Vector3 rayStart;
private Vector3 rayEnd;
public GameObject bulletPrefab;
public Transform firePos;
public float fireForce = 1000f;
public GameObject targetPlayer;
private Collider playerCollider;
private Rigidbody rb;
void Start()
{
currentState = AnimationState.Idle;
animator = GetComponent<Animator>();
targetPlayer.SetActive(true);
playerCollider = GetComponent<Collider>();
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
if (currentState != AnimationState.Die)
{
Move();
DetectEnemies();
}
HandleAnimation();
}
void Move()
{
Vector3 moveDirection = new Vector3(-joystick.Vertical, 0, joystick.Horizontal).normalized;
if (moveDirection != Vector3.zero)
{
Quaternion toRotation = Quaternion.LookRotation(moveDirection, Vector3.up);
transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, 360f * Time.deltaTime);
rb.velocity = moveDirection * moveSpeed; // velocity를 사용하여 움직임 제어
currentState = AnimationState.Run;
}
else
{
rb.velocity = Vector3.zero; // 움직임이 없을 때는 속도를 0으로 설정
if (targetedEnemy != null)
{
currentState = AnimationState.Attack;
LookAtEnemy(targetedEnemy.transform);
}
else
{
currentState = AnimationState.Idle;
}
}
}
public void FireAtEnemy() //애니메이션 이벤트
{
// 적의 중앙을 향한 방향 계산
Vector3 enemyCenter = targetedEnemy.transform.position + new Vector3(0, targetedEnemy.GetComponent<Collider>().bounds.extents.y, 0); //Enemy의 position + Enemy의 콜라이더 높이 절반
Vector3 fireDirection = (enemyCenter - firePos.position).normalized;
// 총알 방향을 설정
Quaternion fireRotation = Quaternion.LookRotation(fireDirection);
fireRotation *= Quaternion.Euler(90, 0, 0); // X축으로 90도 회전을 추가
GameObject bullet = Instantiate(bulletPrefab, firePos.position, fireRotation);
StartCoroutine(FireEffectCoroutine());
Rigidbody rb = bullet.GetComponent<Rigidbody>();
if (rb != null)
{
rb.AddForce(fireDirection * fireForce, ForceMode.Impulse);
}
}
private IEnumerator FireEffectCoroutine() // 총구 화염 효과 코루틴
{
foreach (Transform child in firePos)
{
child.gameObject.SetActive(true);
}
yield return new WaitForSeconds(0.2f);
foreach (Transform child in firePos)
{
child.gameObject.SetActive(false);
}
}
void DetectEnemies()
{
Vector3 rayDirection;
Vector3 rayStartPosition = transform.position + new Vector3(0, rayHeight, 0);
// 타겟이 범위를 벗어났는지 확인
if (targetedEnemy != null)
{
float distanceToTarget = Vector3.Distance(transform.position, targetedEnemy.transform.position);
if (distanceToTarget > detectionRadius || !targetedEnemy.targetPoint.activeSelf)
{
targetedEnemy.targetPoint.SetActive(false);
targetedEnemy = null;
}
}
if (targetedEnemy == null)
{
Collider[] detectedEnemies = Physics.OverlapSphere(transform.position, detectionRadius, enemyLayer);
EnemyBase closestVisibleEnemy = null;
float minDistance = float.MaxValue;
foreach (Collider collider in detectedEnemies)
{
rayDirection = (collider.transform.position - transform.position).normalized;
RaycastHit hit1; // 이 안에서만 사용할 것이기 때문에 여기서 선언합니다.
// Ray 발사
if (Physics.Raycast(rayStartPosition, rayDirection, out hit1, detectionRadius, enemyLayer | wallLayer))
{
if (hit1.collider.GetComponent<EnemyBase>())
{
float distance = Vector3.Distance(transform.position, collider.transform.position);
if (distance < minDistance)
{
closestVisibleEnemy = collider.GetComponent<EnemyBase>();
minDistance = distance;
}
}
}
}
if (closestVisibleEnemy && closestVisibleEnemy.enemyState != EnemyBase.EnemyState.Die)
{
rayDirection = (closestVisibleEnemy.transform.position - transform.position).normalized;
if (targetedEnemy != null)
targetedEnemy.targetPoint.SetActive(false);
targetedEnemy = closestVisibleEnemy;
targetedEnemy.targetPoint.SetActive(true);
}
else
{
return;
}
}
else
{
rayDirection = (targetedEnemy.transform.position - transform.position).normalized;
}
RaycastHit hit2; // 이 부분을 그대로 유지합니다.
if (Physics.Raycast(rayStartPosition, rayDirection, out hit2, detectionRadius, enemyLayer | wallLayer))
{
rayStart = rayStartPosition;
rayEnd = hit2.point;
if (hit2.collider.gameObject.layer == LayerMask.NameToLayer("Wall"))
{
if (targetedEnemy != null)
{
targetedEnemy.targetPoint.SetActive(false);
targetedEnemy = null;
}
}
}
}
void LookAtEnemy(Transform enemyTransform)
{
Vector3 lookDirection = enemyTransform.position - transform.position;
lookDirection.y = 0;
Quaternion rotation = Quaternion.LookRotation(lookDirection);
transform.rotation = rotation;
}
void HandleAnimation()
{
animator.SetInteger("state", (int)currentState);
}
private void OnCollisionEnter(Collision collision) // Enemy와 직접적인 충돌
{
if (collision.gameObject.CompareTag("Enemy"))
{
GameManager.Instance.PlayerHP -= 1; // HP 감소
Debug.Log("Player HP: " + GameManager.Instance.PlayerHP);
if (GameManager.Instance.PlayerHP <= 0)
{
currentState = AnimationState.Die;
targetPlayer.SetActive(false);
playerCollider.enabled = false;
}
}
}
private void OnTriggerEnter(Collider other) // Enemy 투사체에 의한 충돌
{
// EnemyBullet과 충돌 시
if (other.gameObject.CompareTag("EnemyBullet"))
{
GameManager.Instance.PlayerHP -= 1;
Destroy(other.gameObject);
Debug.Log("Player HP: " + GameManager.Instance.PlayerHP);
// HP가 0이면 Die 상태로 전환
if (GameManager.Instance.PlayerHP <= 0)
{
currentState = AnimationState.Die;
targetPlayer.SetActive(false);
playerCollider.enabled = false;
//Destroy(gameObject, 3f); // 3초 후에 Player를 Destroy (Die 애니메이션이 끝날 때까지 대기)
}
else
{
currentState = AnimationState.GetHit;
}
}
}
private void OnDrawGizmos()
{
Gizmos.color = Color.white;
Gizmos.DrawWireSphere(this.transform.position, detectionRadius);
// 저장된 Ray의 정보를 기반으로 선을 그림
Gizmos.color = Color.red;
Gizmos.DrawLine(rayStart, rayEnd);
}
}
public enum AnimationState
{
Idle,
Run,
Attack,
Reload,
GetHit,
Die
}
'KDT > 3D 콘텐츠 제작' 카테고리의 다른 글
[HeroShooter] 스테이지4: 움직이고 근거리 공격하는 몬스터 배치 구현 (0) | 2023.08.31 |
---|---|
[HeroShooter] 스테이지2~3: 움직이지 않고 원거리 공격을 하는 몬스터 구현 (1) | 2023.08.27 |
[HeroShooter] Stage2. Enemy의 피격과 사망, 인식 구현 (0) | 2023.08.24 |
[HeroShooter] Ray와 Overlap을 이용한 적 감지 구현 (0) | 2023.08.23 |
[HeroShooter] Tutorial UI 및 트리거 구현 (0) | 2023.08.22 |