using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
public class PlayerCtrl : MonoBehaviour
{
private Transform tr;
public float moveSpeed = 10.0f;
public float turnSpeed = 80.0f;
private Animation anim;
private readonly float initHp = 100f;
public float currentHp;
public delegate void PlayerDieHandler(); //대리자 정의
public static event PlayerDieHandler OnPlayerDie; //이벤트 변수 정의
private void Awake()
{
//제일먼저 호출되는 함수
//스크리브가 비활성화되있어도 호출되는 함수
}
private void OnEnable()
{
//두번째로 호출되는 함수
//스크립트가 활성화 될때마다 호출되는 함수
}
IEnumerator Start()
{
this.currentHp = this.initHp;
// 세번째로 호출되는 함수
//Update함수가 호출되기 전에 호출되는 함수
//코루틴으로 호출될수 있는 함수
//예)
//IEnumerator Start(){}
tr = GetComponent<Transform>();
this.anim = GetComponent<Animation>();
//이름으로 실행 하거나
this.anim.Play("Idle");
//이름으로 가져와서 플레이 하거나
//this.anim.clip = this.anim.GetClip("Idle");
//this.anim.Play();
this.turnSpeed = 0f;
yield return new WaitForSeconds(0.3f);
this.turnSpeed = 80f;
}
private void FixedUpdate()
{
//일정간격으로 호출되는 함수 (기본값은 0.02초)
//물리엔진의 계산 주기와 일치
}
void Update()
{
//프레임마다 호출되는 함수
//호출 간격이 불규칙하다
//화면의 렌더링 주기와 일치 함
float h = Input.GetAxis("Horizontal"); // -1.0 ~ 0 ~ 1
float v = Input.GetAxis("Vertical"); // -1.0 ~ 0 ~ 1
float r = Input.GetAxis("Mouse X");
//Debug.Log("h=" + h);
//Debug.Log("v=" + v);
Debug.Log("r=" + r);
//this.tr.Translate(Vector3.forward * 1 * 0.01f , Space.Self); //로컬좌표 기준으로 이동
//this.tr.Translate(Vector3.forward * 1 * Time.deltaTime, Space.World); //월드좌표 기준으로 이동
//매 프레임마다 1유닛씩 이동 => 0.02초
//this.tr.Translate(Vector3.forward * 1 * Time.deltaTime);
//매 초마다 1유닛씩 이동
//프레임 레이트가 서로다른 기기에서도 개발자가 지정한 일정한 속도로 이동하기 위함
//this.tr.Translate(Vector3.forward * v * Time.deltaTime * moveSpeed); //방향 * 속도 * 시간 => forward = (0,0,1) // v => -1~ 1 사이의 값 => v를 사용해서 앞뿐만이 아닌 뒤로도 이동가능
//전후좌우 이동방향 벡터 계산
Vector3 moveDir = (Vector3.forward * v) + (Vector3.right * h); // right = (1,0,0)
//방향 * 속도 * 시간
this.tr.Translate(moveDir.normalized * moveSpeed * Time.deltaTime);
//Vector3.up 축으로 turnSpeed만큼
this.tr.Rotate(Vector3.up * r * this.turnSpeed);
//캐릭터 애니메이션 실행
PlayerAnim(h, v);
}
private void LateUpdate()
{
//Update함수가 종료된 후 호출 되는 함수
}
private void PlayerAnim(float h, float v)
{
//키보드 입력값을 기준으로 동작할 애니메이션 실행
if (v >= 0.1f)
{
//0.25f초간 이전 애니메이션과 실행할 애니메이션을 보간
this.anim.CrossFade("RunF", 0.25f); //전진 애니메이션 실행
}
else if (v <= -0.1f)
{
this.anim.CrossFade("RunB", 0.25f);//후진
}
else if (h >= 0.1f)
{
this.anim.CrossFade("RunR", 0.25f);//오른쪽
}
else if (h <= -0.1f)
{
this.anim.CrossFade("RunL", 0.25f);//왼쪽
}
else
{
this.anim.CrossFade("Idle", 0.25f);//기본
}
}
private void OnTriggerEnter(Collider other)
{
if(this.currentHp >= 0f && other.CompareTag("Punch"))
{
this.currentHp -= 10f;
Debug.Log($"Player hp = {currentHp / initHp}");
if (this.currentHp <= 0f)
{
this.PlayerDie();
}
}
}
private void PlayerDie()
{
Debug.Log("Player Die!");
////Monster 태그를 가진 오브젝트들을 검색
//GameObject[] monsters = GameObject.FindGameObjectsWithTag("Monster");
////모든 몬스터의 OnplayerDie함수실행
//foreach(GameObject monster in monsters)
//{
// monster.SendMessage("OnPlayerDie", SendMessageOptions.RequireReceiver);
//}
PlayerCtrl.OnPlayerDie(); //ㄷ대리차 호출(이벤트 발생 Dispatch)
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletCtrl : MonoBehaviour
{
public float damage = 20.0f;
public float force = 1500f;
private Rigidbody rb;
void Start()
{
this.rb = this.GetComponent<Rigidbody>();
this.rb.AddForce(this.transform.forward * force);
Destroy(gameObject, 3f);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowCam : MonoBehaviour
{
public Transform targetTr;
private Transform camTr;
//대상으로 부터 떨어질 거리
[Range(2.0f, 20.0f)]
public float distance = 10.0f;
//Y축으로 이동할 높이
[Range(0.0f, 10.0f)]
public float height = 2.0f;
//반응속도
public float damping = 10f;
//SmoothDamp에 사용할 속도 변수
private Vector3 velocity = Vector3.zero;
//카메라 LookAt OffSet
public float targetOffset = 2.0f;
void Start()
{
this.camTr = GetComponent<Transform>(); //Transform 객체 캐싱
}
private void LateUpdate()
{
//추적할 대상의 뒤쪽 distance만큼 이동
//높이를 height만큼 이동
Vector3 pos = this.targetTr.position +
(-targetTr.forward * distance) +
(Vector3.up * height);
//구면 선형 보간 함수를 사용해 부드럽게 위치를 변경
//시작위치, 목표 위치, 시간
//this.camTr.position = Vector3.Slerp(this.camTr.position, pos, Time.deltaTime * damping);
//시작위치, 목표위치, 현재속도, 목표 위치까지 도달할 시간
this.camTr.position = Vector3.SmoothDamp(this.camTr.position, pos, ref velocity, damping);
//Camera를 피벗 좌표를 향해 회전
this.camTr.LookAt(this.targetTr.position + (targetTr.up * targetOffset));
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
public class PlayerCtrl : MonoBehaviour
{
private Transform tr;
public float moveSpeed = 10.0f;
public float turnSpeed = 80.0f;
private Animation anim;
void Awake()
{
//제일먼저 호출되는 함수
//스크리브가 비활성화되있어도 호출되는 함수
}
private void OnEnable()
{
//두번째로 호출되는 함수
//스크립트가 활성화 될때마다 호출되는 함수
}
private void Start()
{
//세번째로 호출되는 함수
//Update함수가 호출되기 전에 호출되는 함수
//코루틴으로 호출될 수 있는 함수
//예)
//IEnumerator Start(){}
tr = GetComponent<Transform>();
anim = GetComponent<Animation>();
this.anim.Play("Idle");
//this.anim.clip = this.anim.GetClip("Idle");
//anim.Play();
}
private void FixedUpdate()
{
//일정간격으로 호출되는 함수 (기본값은 0.02초)
//물리엔진의 계산 주기와 일치
}
void Update()
{
//프레임마다 호출되는 함수
//호출 간격이 불규칙하다
//화면의 렌더링 주기와 일치 함
float h = Input.GetAxis("Horizontal"); // -1.0 ~ 0 ~ 1
float v = Input.GetAxis("Vertical"); // -1.0 ~ 0 ~ 1
float r = Input.GetAxis("Mouse X");
//Debug.Log("h=" + h);
//Debug.Log("v=" + v);
Debug.Log("r=" + r);
//this.tr.Translate(Vector3.forward * 1 * 0.01f , Space.Self); //로컬좌표 기준으로 이동
//this.tr.Translate(Vector3.forward * 1 * Time.deltaTime, Space.World); //월드좌표 기준으로 이동
//매 프레임마다 1유닛씩 이동 => 0.02초
//this.tr.Translate(Vector3.forward * 1 * Time.deltaTime);
//매 초마다 1유닛씩 이동
//프레임 레이트가 서로다른 기기에서도 개발자가 지정한 일정한 속도로 이동하기 위함
//this.tr.Translate(Vector3.forward * v * Time.deltaTime * moveSpeed); //방향 * 속도 * 시간 => forward = (0,0,1) // v => -1~ 1 사이의 값 => v를 사용해서 앞뿐만이 아닌 뒤로도 이동가능
//전후좌우 이동방향 벡터 계산
Vector3 moveDir = (Vector3.forward * v) + (Vector3.right * h); // right = (1,0,0)
//방향 * 속도 * 시간
this.tr.Translate(moveDir.normalized * moveSpeed * Time.deltaTime);
//Vector3.up 축으로 turnSpeed만큼
this.tr.Rotate(Vector3.up * r * this.turnSpeed);
//캐릭터 애니메이션 실행
PlayerAnim(h, v);
}
private void LateUpdate()
{
//Update함수가 종료된 후 호출 되는 함수
}
private void PlayerAnim(float h, float v)
{
//키보드 입력값을 기준으로 동작할 애니메이션 실행
if (v >= 0.1f)
{
//0.25f초간 이전 애니메이션과 실행할 애니메이션을 보간
this.anim.CrossFade("RunF", 0.25f); //전진 애니메이션 실행
}
else if (v <= -0.1f)
{
this.anim.CrossFade("RunB", 0.25f);//후진
}
else if (h >= 0.1f)
{
this.anim.CrossFade("RunR", 0.25f);//오른쪽
}
else if (h <= -0.1f)
{
this.anim.CrossFade("RunL", 0.25f);//왼쪽
}
else
{
this.anim.CrossFade("Idle", 0.25f);//기본
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletCtrl : MonoBehaviour
{
public float damage = 20.0f;
public float force = 1500f;
private Rigidbody rb;
void Start()
{
this.rb = this.GetComponent<Rigidbody>();
this.rb.AddForce(this.transform.forward * force);
Destroy(gameObject, 3f);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowCam : MonoBehaviour
{
public Transform targetTr;
private Transform camTr;
//대상으로 부터 떨어질 거리
[Range(2.0f, 20.0f)]
public float distance = 10.0f;
//Y축으로 이동할 높이
[Range(0.0f, 10.0f)]
public float height = 2.0f;
//반응속도
public float damping = 10f;
//SmoothDamp에 사용할 속도 변수
private Vector3 velocity = Vector3.zero;
//카메라 LookAt OffSet
public float targetOffset = 2.0f;
void Start()
{
this.camTr = GetComponent<Transform>(); //Transform 객체 캐싱
}
private void LateUpdate()
{
//추적할 대상의 뒤쪽 distance만큼 이동
//높이를 height만큼 이동
Vector3 pos = this.targetTr.position +
(-targetTr.forward * distance) +
(Vector3.up * height);
//구면 선형 보간 함수를 사용해 부드럽게 위치를 변경
//시작위치, 목표 위치, 시간
//this.camTr.position = Vector3.Slerp(this.camTr.position, pos, Time.deltaTime * damping);
//시작위치, 목표위치, 현재속도, 목표 위치까지 도달할 시간
this.camTr.position = Vector3.SmoothDamp(this.camTr.position, pos, ref velocity, damping);
//Camera를 피벗 좌표를 향해 회전
this.camTr.LookAt(this.targetTr.position + (targetTr.up * targetOffset));
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace yjShin
{
public class Woman : MonoBehaviour
{
private void OnAnimatorIK(int layerIndex)
{
Debug.Log("OnAnimatorIK");
}
}
}
오른쪽 팔꿈치의 좌표를 확인
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace yjShin
{
public class Woman : MonoBehaviour
{
private Animator anim;
private void Start()
{
anim = GetComponent<Animator>();
}
private void OnAnimatorIK(int layerIndex)
{
//Debug.Log("OnAnimatorIK");
Vector3 rightElbowPosition = anim.GetIKHintPosition(AvatarIKHint.RightElbow);
Debug.Log(rightElbowPosition);
}
}
}
애니메이터에 저장되어 있는 피봇들
AvatarIKHint.RightElbow는 애니메이션 시스템에서 제공되는 미리 정의된 상수로, 이를 통해 오른쪽 팔꿈치의 위치를 얻을 수 있습니다. 이 값은 애니메이션 시스템 내부에서 이미 정의되어 있으며, 개발자가 직접 오른쪽 팔꿈치를 찾거나 계산할 필요가 없습니다.
using UnityEngine;
// 주어진 Gun 오브젝트를 쏘거나 재장전
// 알맞은 애니메이션을 재생하고 IK를 사용해 캐릭터 양손이 총에 위치하도록 조정
public class PlayerShooter : MonoBehaviour {
public Gun gun; // 사용할 총
public Transform gunPivot; // 총 배치의 기준점
public Transform leftHandMount; // 총의 왼쪽 손잡이, 왼손이 위치할 지점
public Transform rightHandMount; // 총의 오른쪽 손잡이, 오른손이 위치할 지점
private PlayerInput playerInput; // 플레이어의 입력
private Animator playerAnimator; // 애니메이터 컴포넌트
private void Start() {
// 사용할 컴포넌트들을 가져오기
playerInput = GetComponent<PlayerInput>();
playerAnimator = GetComponent<Animator>();
}
private void OnEnable() {
// 슈터가 활성화될 때 총도 함께 활성화
gun.gameObject.SetActive(true);
}
private void OnDisable() {
// 슈터가 비활성화될 때 총도 함께 비활성화
gun.gameObject.SetActive(false);
}
private void Update() {
// 입력을 감지하고 총 발사하거나 재장전
if (playerInput.fire)
{
gun.Fire();
}
else if (playerInput.reload)
{
{
if (gun.Reload())
{
}
playerAnimator.SetTrigger("Reload");
}
}
UpdateUI();
}
// 탄약 UI 갱신
private void UpdateUI() {
if (gun != null && UIManager.instance != null)
{
// UI 매니저의 탄약 텍스트에 탄창의 탄약과 남은 전체 탄약을 표시
UIManager.instance.UpdateAmmoText(gun.magAmmo, gun.ammoRemain);
}
}
// 애니메이터의 IK 갱신
private void OnAnimatorIK(int layerIndex) {
//총의 기준점 gunPivot을 3D 모델의 오른쪽 팔꿈치 위치로 이동
gunPivot.position = playerAnimator.GetIKHintPosition(AvatarIKHint.RightElbow);
//IK를 사용하여 왼손의 위치와 회전을 총의 왼쪽 손잡이에 맞춤
playerAnimator.SetIKPositionWeight(AvatarIKGoal.LeftHand, 1.0f);
playerAnimator.SetIKRotationWeight(AvatarIKGoal.LeftHand, 1.0f);
playerAnimator.SetIKPosition(AvatarIKGoal.LeftHand, leftHandMount.position);
playerAnimator.SetIKRotation(AvatarIKGoal.LeftHand, leftHandMount.rotation);
//IK를 사용하여 오른손의 위치와 회전을 총의 오른쪽 손잡이에 맞춤
playerAnimator.SetIKPositionWeight(AvatarIKGoal.RightHand, 1.0f);
playerAnimator.SetIKRotationWeight(AvatarIKGoal.RightHand, 1.0f);
playerAnimator.SetIKPosition(AvatarIKGoal.RightHand, rightHandMount.position);
playerAnimator.SetIKRotation(AvatarIKGoal.RightHand, rightHandMount.rotation);
}
}
using System.Collections;
using UnityEngine;
using UnityEngine.UIElements;
// 총을 구현
public class Gun : MonoBehaviour
{
// 총의 상태를 표현하는 데 사용할 타입을 선언
public enum State
{
Ready, // 발사 준비됨
Empty, // 탄알집이 빔
Reloading // 재장전 중
}
public State state { get; private set; } // 현재 총의 상태
public Transform fireTransform; // 탄알이 발사될 위치
public ParticleSystem muzzleFlashEffect; // 총구 화염 효과
public ParticleSystem shellEjectEffect; // 탄피 배출 효과
private LineRenderer bulletLineRenderer; // 탄알 궤적을 그리기 위한 렌더러
private AudioSource gunAudioPlayer; // 총 소리 재생기
public GunData gunData; // 총의 현재 데이터
private float fireDistance = 50f; // 사정거리
public int ammoRemain = 100; // 남은 전체 탄알
public int magAmmo; // 현재 탄알집에 남아 있는 탄알
private float lastFireTime; // 총을 마지막으로 발사한 시점
private void Awake() {
// 사용할 컴포넌트의 참조 가져오기
gunAudioPlayer = GetComponent<AudioSource>();
this.bulletLineRenderer = GetComponent<LineRenderer>();
this.bulletLineRenderer.positionCount = 2;
this.bulletLineRenderer.enabled = false;
}
private void OnEnable() {
ammoRemain = gunData.startAmmoRemain;
magAmmo = gunData.magCapacity;
// 총 상태 초기화
this.state = State.Ready;
lastFireTime = 0;
}
// 발사 시도
public void Fire() {
if(this.state == State.Ready && Time.time >= lastFireTime + gunData.timeBetFire)
{
lastFireTime = Time.time;
Shot();
}
}
// 실제 발사 처리
private void Shot() {
RaycastHit hit;
Vector3 hitPosition =Vector3.zero;
//시작점, 방향, 히트정보, 거리
if(Physics.Raycast(fireTransform.position, fireTransform.forward, out hit, fireDistance))
{
//충돌한 콜라이더의 게임오브젝트에 붙어 있는 IDamageable 인터페이스를 상속받고 있는 컴포넌트를 가져온다.
IDamageable target = hit.collider.GetComponent<IDamageable>();
if (target != null)
{
target.OnDamage(gunData.damage, hit.point, hit.normal);
}
hitPosition = hit.point;
}
else
{
//탄알이 최대 사정거리까지 날아갔을 때의 위치를 충돌 위치로 사용 => out hit가 빠진다.
hitPosition = fireTransform.position + fireTransform.forward * fireDistance;
}
StartCoroutine(ShotEffect(hitPosition));
magAmmo--;
if(magAmmo <= 0)
{
state = State.Empty;
}
}
// 발사 이펙트와 소리를 재생하고 탄알 궤적을 그림
private IEnumerator ShotEffect(Vector3 hitPosition) {
muzzleFlashEffect.Play();
shellEjectEffect.Play();
//총격 소리 재생
gunAudioPlayer.PlayOneShot(gunData.shotClip);
//라인랜더러의 시작 위치
this.bulletLineRenderer.SetPosition(0, fireTransform.position);
//라인랜더러의 끝 위치
this.bulletLineRenderer.SetPosition(1, hitPosition);
// 라인 렌더러를 활성화하여 탄알 궤적을 그림
bulletLineRenderer.enabled = true;
// 0.03초 동안 잠시 처리를 대기
yield return new WaitForSeconds(0.03f);
// 라인 렌더러를 비활성화하여 탄알 궤적을 지움
bulletLineRenderer.enabled = false;
}
// 재장전 시도
public bool Reload()
{
if (state == State.Reloading || ammoRemain <= 0 || magAmmo >= gunData.magCapacity)
{
return false;
}
StartCoroutine(ReloadRoutine());
return true;
}
// 실제 재장전 처리를 진행
private IEnumerator ReloadRoutine() {
// 현재 상태를 재장전 중 상태로 전환
state = State.Reloading;
gunAudioPlayer.PlayOneShot(gunData.reloadClip);
// 재장전 소요 시간 만큼 처리 쉬기
yield return new WaitForSeconds(gunData.reloadTime);
int ammoToFill = gunData.magCapacity - magAmmo;
if(ammoRemain < ammoToFill)
{
ammoToFill = ammoRemain;
}
magAmmo += ammoToFill;
ammoRemain -= ammoToFill;
// 총의 현재 상태를 발사 준비된 상태로 변경
state = State.Ready;
}
private void Update()
{
//test
if(Input.GetMouseButtonDown(0))
{
this.Fire();
}
}
}
using UnityEngine;
// 데미지를 입을 수 있는 타입들이 공통적으로 가져야 하는 인터페이스
public interface IDamageable {
// 데미지를 입을 수 있는 타입들은 IDamageable을 상속하고 OnDamage 메서드를 반드시 구현해야 한다
// OnDamage 메서드는 입력으로 데미지 크기(damage), 맞은 지점(hitPoint), 맞은 표면의 방향(hitNormal)을 받는다
void OnDamage(float damage, Vector3 hitPoint, Vector3 hitNormal);
}
using UnityEngine;
[CreateAssetMenu(menuName = "Scriptable/GunData", fileName = "Gun Data")]
public class GunData : ScriptableObject
{
public AudioClip shotClip; // 발사 소리
public AudioClip reloadClip; // 재장전 소리
public float damage = 25; // 공격력
public int startAmmoRemain = 100; // 처음에 주어질 전체 탄약
public int magCapacity = 25; // 탄창 용량
public float timeBetFire = 0.12f; // 총알 발사 간격
public float reloadTime = 1.8f; // 재장전 소요 시간
}
해당 타입의 에셋을 생성할 수 있는 버튼을 Assets와 Create(+버튼) 메뉴에 추가
"(경로)" "(기본 파일명)", order =메뉴 상에서 순서
이번에 중점적으로 볼 것은 라인렌더이다. => Gun.cs
라인 렌더러 사이즈 2로 바꾼후 수치를 입력하면 좌표로 쏜다는 걸 알 수 있다.
this.bulletLineRenderer.enabled = false;
선을 그려서 총을 쏠때 방향을 확인해야 한다.
hit.normal => 노말을 쓴다는 건 애니메이션 방향을 정해주기 위해?
else 문은 타겟이 맞지 않았을 때 ex)허공에 쏠 때
탄알의 궤적을 잡아주려하기 위해
실행을 하면 Null 오류가 날 것인데 그러면 if로 조건을 넣어주면 된다.
using UnityEngine;
[CreateAssetMenu(menuName = "Scriptable/GunData", fileName = "Gun Data")]
public class GunData : ScriptableObject
{
public AudioClip shotClip; // 발사 소리
public AudioClip reloadClip; // 재장전 소리
public float damage = 25; // 공격력
public int startAmmoRemain = 100; // 처음에 주어질 전체 탄약
public int magCapacity = 25; // 탄창 용량
public float timeBetFire = 0.12f; // 총알 발사 간격
public float reloadTime = 1.8f; // 재장전 소요 시간
}
using System.Collections;
using UnityEngine;
using UnityEngine.UIElements;
// 총을 구현
public class Gun : MonoBehaviour
{
// 총의 상태를 표현하는 데 사용할 타입을 선언
public enum State
{
Ready, // 발사 준비됨
Empty, // 탄알집이 빔
Reloading // 재장전 중
}
public State state { get; private set; } // 현재 총의 상태
public Transform fireTransform; // 탄알이 발사될 위치
public ParticleSystem muzzleFlashEffect; // 총구 화염 효과
public ParticleSystem shellEjectEffect; // 탄피 배출 효과
private LineRenderer bulletLineRenderer; // 탄알 궤적을 그리기 위한 렌더러
private AudioSource gunAudioPlayer; // 총 소리 재생기
public GunData gunData; // 총의 현재 데이터
private float fireDistance = 50f; // 사정거리
public int ammoRemain = 100; // 남은 전체 탄알
public int magAmmo; // 현재 탄알집에 남아 있는 탄알
private float lastFireTime; // 총을 마지막으로 발사한 시점
private void Awake() {
// 사용할 컴포넌트의 참조 가져오기
gunAudioPlayer = GetComponent<AudioSource>();
this.bulletLineRenderer = GetComponent<LineRenderer>();
this.bulletLineRenderer.positionCount = 2;
this.bulletLineRenderer.enabled = false;
}
private void OnEnable() {
ammoRemain = gunData.startAmmoRemain;
magAmmo = gunData.magCapacity;
// 총 상태 초기화
this.state = State.Ready;
lastFireTime = 0;
}
// 발사 시도
public void Fire() {
if(this.state == State.Ready && Time.time >= lastFireTime + gunData.timeBetFire)
{
lastFireTime = Time.time;
Shot();
}
}
// 실제 발사 처리
private void Shot() {
RaycastHit hit;
Vector3 hitPosition =Vector3.zero;
//시작점, 방향, 히트정보, 거리
if(Physics.Raycast(fireTransform.position, fireTransform.forward, out hit, fireDistance))
{
//충돌한 콜라이더의 게임오브젝트에 붙어 있는 IDamageable 인터페이스를 상속받고 있는 컴포넌트를 가져온다.
IDamageable target = hit.collider.GetComponent<IDamageable>();
if (target != null)
{
target.OnDamage(gunData.damage, hit.point, hit.normal);
}
hitPosition = hit.point;
}
else
{
//탄알이 최대 사정거리까지 날아갔을 때의 위치를 충돌 위치로 사용 => out hit가 빠진다.
hitPosition = fireTransform.position + fireTransform.forward * fireDistance;
}
StartCoroutine(ShotEffect(hitPosition));
magAmmo--;
if(magAmmo <= 0)
{
state = State.Empty;
}
}
// 발사 이펙트와 소리를 재생하고 탄알 궤적을 그림
private IEnumerator ShotEffect(Vector3 hitPosition) {
muzzleFlashEffect.Play();
shellEjectEffect.Play();
//총격 소리 재생
gunAudioPlayer.PlayOneShot(gunData.shotClip);
//라인랜더러의 시작 위치
this.bulletLineRenderer.SetPosition(0, fireTransform.position);
//라인랜더러의 끝 위치
this.bulletLineRenderer.SetPosition(1, hitPosition);
// 라인 렌더러를 활성화하여 탄알 궤적을 그림
bulletLineRenderer.enabled = true;
// 0.03초 동안 잠시 처리를 대기
yield return new WaitForSeconds(0.03f);
// 라인 렌더러를 비활성화하여 탄알 궤적을 지움
bulletLineRenderer.enabled = false;
}
// 재장전 시도
public bool Reload()
{
if (state == State.Reloading || ammoRemain <= 0 || magAmmo >= gunData.magCapacity)
{
return false;
}
StartCoroutine(ReloadRoutine());
return true;
}
// 실제 재장전 처리를 진행
private IEnumerator ReloadRoutine() {
// 현재 상태를 재장전 중 상태로 전환
state = State.Reloading;
gunAudioPlayer.PlayOneShot(gunData.reloadClip);
// 재장전 소요 시간 만큼 처리 쉬기
yield return new WaitForSeconds(gunData.reloadTime);
int ammoToFill = gunData.magCapacity - magAmmo;
if(ammoRemain < ammoToFill)
{
ammoToFill = ammoRemain;
}
magAmmo += ammoToFill;
ammoRemain -= ammoToFill;
// 총의 현재 상태를 발사 준비된 상태로 변경
state = State.Ready;
}
private void Update()
{
//test
if(Input.GetMouseButtonDown(0))
{
this.Fire();
}
}
}
테스트를 해보려는 도중 점프가 되지 않아서 원인을 찾아보니 Simulated를 체크하지 않았다.
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class TokoController : MonoBehaviour
{
public AudioClip deathClip;
public float jumpForce = 700f;
private int jumpCount = 0;
private bool isGrounded = false;
private bool isDead = false;
private Rigidbody2D playerRigidbody;
private Animator animator;
private AudioSource playerAudio;
void Start()
{
//게임 오브젝트로부터 사용할 컴포넌트들을 가져와 변수에 할당
playerRigidbody = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
playerAudio = GetComponent<AudioSource>();
}
void Update()
{
if (isDead)
{
//사망시 더 이상 처리를 하지 않고 종료
return;
}
if (Input.GetMouseButtonDown(0) && jumpCount < 2)
{
jumpCount++; //점프가 두번됨
//점프 직전 속도를 순간적으로 제로(0,0)으로 변경
playerRigidbody.velocity = Vector2.zero;
//리지드바디에 위쪽으로 힘 주기
playerRigidbody.AddForce(new Vector2(0, jumpForce));
//오디오 소스 재생
playerAudio.Play();
Debug.Log("마우스가 클릭됨");
}
else if(Input.GetMouseButtonUp(0) && playerRigidbody.velocity.y > 0) // 마우스에서 손을 떼는 순간 속도의 y값이 양수라면(위로 상승) => 현재 속도를 절반으로 변경
{
playerRigidbody.velocity = playerRigidbody.velocity * 0.5f;
}
//애니메이터의 Grounded 파라미터를 isGrounded 값으로 갱신
animator.SetBool("Grounded", isGrounded);
Debug.LogFormat("{0}", playerRigidbody.velocity.y);
}
private void Die()
{
}
private void OnTriggerEnter2D(Collider2D collision)
{
//트리거 콜라이더를 가진 물체와 충돌 감지
}
private void OnCollisionEnter2D(Collision2D collision)
{
//바닥에 닿았음을 감지
//어떤 콜라이더와 닿았으며, 충돌표면이 위쪽을 보고 있으면
if (collision.contacts[0].normal.y > 0.7f)
{
//isGrouded가 true로 변경하고, 누적 점프횟수를 0으로 초기화
isGrounded = true;
jumpCount = 0;
//Debug.Log("충돌 발생");
}
}
private void OnCollisionExit2D(Collision2D collision)
{
//바닥에서 벗어났음을 감지하는 처리
//어떤 콜라이더에서 떼어진 경우
isGrounded = false;
}
}
velocity.y의 값을 출력해 보았는데 음수가 나왔다.
Rigidbody의 velocity.y 값이 음수가 나오는 이유는 중력의 영향 때문이다.
Rigidbody의 velocity는 해당 객체의 현재 속도를 나타내며, 유니티에서는 아래쪽 방향을 음의 방향으로 간주한다..
따라서 캐릭터가 점프 후에 일정 높이에 도달하면 중력에 의해 아래로 가속됩니다. 이 때 velocity.y 값은 음수가 된다.
그리고 바닥에 닿으면 다시 상승하기 시작할 때 velocity.y 값은 양수가 될 것이다.
즉, velocity.y 값이 음수가 되는 것은 캐릭터가 점프 후 중력에 의해 아래로 가속되는 정상적인 동작이다.
private void Die()
{
animator.SetTrigger("Die");
playerAudio.clip = deathClip;
playerAudio.Play();
//속도를 제로로 변경
playerRigidbody.velocity = Vector2.zero;
//사망 상태를 true로 변경
isDead = true;
}
다른 Set계열 메서드와 달리 파라미터에 할당할 새로운 값은 입력하지 않는다.
SetTrigger()메서드는 '방아쇠''를 당길 뿐이다.
트리거 타입의 파라미터는 즉시 true가 되었다가 곧바로 false가 되기 때문에 별도의 값을 지정하지 않는다.
animator.SetTrigger("Die")가 실행되면 곧바로 Die상태러 전환되는 애니메이션 클립이 재생된다.
배경을 설정할 것인데 태그에다 이름을 지정하면 order in layer 처럼
레이어의 순서가 정해진다.
배경을 스크롤링 할것이므로 스크립트 추가
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScrollingObject : MonoBehaviour
{
public float speed = 10f;
void Update()
{
this.transform.Translate(Vector3.left * speed * Time.deltaTime);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BackgroudLoop : MonoBehaviour
{
private float width; //배경의 가로 길이
private void Awake()
{
BoxCollider2D col = GetComponent<BoxCollider2D>();
this.width = col.size.x;
Debug.Log(this.width);
}
void Update()
{
if(transform.position.x <= -this.width)
{
this.Reposition();
}
}
void Reposition()
{
//현재 위치에서 오른쪽으로 가로 길이 * 2 만큼 이동
Vector2 offset = new Vector2(this.width * 2f, 0);
transform.position = (Vector2)this.transform.position + offset;
}
}
sky를 복사해서 기존에 있는 스크린에 넣는다면
예를 들어 화면이 sky -> sky(1) -> sky -> sky(1)이런식으로 두배 간격으로 복사된다.
이제 발판을 더 만들 것인데 프리팹으로 만들었고
프리팹을 가져와서 생성된 게임오브젝트에 설정을 바꾸었다.
만들어진 발판 프리팹에 Obstacles를 추가할건데 이것을 추가할 때는
프리팹에서 만드는 것이 좋다.
그후 발판은 fore 방해물은 middle로 설정하였다.
발판 스크립트를 만들었고 방해물 3개가 각각 랜덤으로 나올수 있게 설정
그리고 방해물 회피 성공시 +1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Platform : MonoBehaviour
{
public GameObject[] obstacles;
private bool stepped = false;
private void OnEnable()
{
stepped = false;
for (int i = 0; i < obstacles.Length; i++)
{
if (Random.Range(0, 3) == 0)
{
obstacles[i].SetActive(true);
}
else
{
obstacles[i].SetActive(false);
}
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.collider.CompareTag("Player") && !stepped)
{
stepped = true;
Debug.Log("점수 1점 추가");
}
}
}
그후 아까 했던 플랫폼의 모든 프리팹에 적용 되도록 Apply All
UI를 만들진 않았지만 UI라던지 게임을 관리할 게임 매니저를 만들었다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
public static GameManager instance;
public bool isGameover = false; //게임오버 상태
public GameObject gameoverUI; //게임 오버 시 활성화 할 UI 게임 오브젝트
private int score = 0; //게임점수
void Awake()
{//싱글턴 변수 instance가 비어있는가?
if (instance == null)
{
//instance가 비어있다면 그곳에 자기 자신을 할당
instance = this;
}
else
{
Destroy(this.gameObject);
}
}
private void Update()
{
if (isGameover && Input.GetMouseButtonDown(0))
{
//게임오버 상태에서 마우스 왼쪽 버튼 클릭하면 현재 씬 재시작
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
//현재 활성화된 씬의 정보를Scene 타입의 오브젝트로 가져오는 메서드 씬의 이름을 변수 name으로 제공
}
}
public void AddScore(int newScore)
{
//게임오버가 아니라면
if (!isGameover)
{
//점수를 증가
score += newScore;
//scoreTect.text = "Score : " + score;
}
}
public void OnPlayerDead()
{
isGameover = true;
// gameoverUI.SetActive(true);
}
}
이제 platform이 랜덤으로 생성될 것이니 하이어라이키에 있는 플랫폼프리팹을 지워준후
많아질 데이터를 관리하기 위해 오브젝트 풀링사용하여
플랫폼 스폰서를 만들어서 복제된 sky(1)에 할당
오브젝트 풀링
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlatformSpawner : MonoBehaviour
{
public GameObject platformPrefab;
public int count = 3;
private GameObject[] platforms;
private float lastSpawnTime = 0;
private float timeBetSpawn = 0;
private int currentIndex = 0;
private void Start()
{
platforms = new GameObject[count];
//미리만들자
for (int i = 0; i < count; i++)
{
platforms[i] = Instantiate(platformPrefab, new Vector3(0, -25, 0), Quaternion.identity);
}
}
private void Update()
{
if (Time.time >= lastSpawnTime + timeBetSpawn)
{
lastSpawnTime = Time.time;
timeBetSpawn = Random.Range(1.25f, 2.25f);
float yPos = Random.Range(-3.5f, 1.5f);
//OnEnable호출을 위해
this.platforms[currentIndex].SetActive(false);
this.platforms[currentIndex].SetActive(true);
//재배치
this.platforms[currentIndex].transform.position = new Vector2(20, yPos);
//인덱스 증가
currentIndex++;
//마지막 순번에 도달 했다면 순번 리셋
if (currentIndex >= count)
{
currentIndex = 0; //0, 1, 2, 0, 1, 2, 0, 1, 2....
}
}
}
}
오브젝트 풀링 사용하는 이유
유니티에서 오브젝트를 생성하기 위해서는 Instantiate를 사용하고 삭제할 때는 Destroy를 사용
하지만 Instantiate, Destroy 이 두 함수는 무게가 상당히 크다.
Instantiate(오브젝트 생성)은 메모리를 새로 할당하고 리소스를 로드하는 등의 초기화 과정이 필요하고, Destroy(오브젝트 파괴)는 파괴 이후에 발생하는 가비지 컬렉팅으로 인한 프레임 드랍이 발생할 수 있다.
쉽게 말해 "재사용" 이라고 볼 수 있다.
메모리를 할당 해두기 때문 메모리를 희생하여 성능을 높이는 것이지만, 실시간 작동하는 게임에서 프레임 때문에 선호된다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CSightVisualizer : MonoBehaviour
{
public float radius = 3;
private void OnDrawGizmos()
{
GizmosExtensions.DrawWireArc(this.transform.position, this.transform.forward, 360, radius);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
public class Capsule : MonoBehaviour
{
public float speed = 5f;
void Start()
{
}
void Update()
{
Move();
}
private void Move()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(h, 0, v);
this.transform.Translate(movement * speed * Time.deltaTime);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Sphere : MonoBehaviour
{
public float radius = 3f; // 원의 반지름
public float rotationSpeed = 30f; // 캡슐의 회전 속도 (각도/초)
private Transform capsuleTransform; // 부모 오브젝트인 캡슐의 Transform
private void Start()
{
// 부모 오브젝트인 캡슐의 Transform 가져오기
capsuleTransform = transform.parent;
}
private void Update()
{
if (capsuleTransform != null)
{
// 현재 시간에 따른 회전 각도 계산
float rotationAngle = Time.time * rotationSpeed;
// 원 주위를 도는 위치 계산
float x = Mathf.Cos(rotationAngle) * radius;
float z = Mathf.Sin(rotationAngle) * radius;
// 부모 오브젝트의 위치를 기준으로 위치 설정
transform.position = capsuleTransform.position + new Vector3(x, 0f, z);
}
}
}
using UnityEngine;
public class Sphere : MonoBehaviour
{
public float radius = 3f; // 원의 반지름
public float rotationSpeed = 30f; // 캡슐의 회전 속도 (각도/초)
private Transform capsuleTransform; // 부모 오브젝트인 캡슐의 Transform
private void Start()
{
// 부모 오브젝트인 캡슐의 Transform 가져오기
capsuleTransform = transform.parent;
}
private void Update()
{
if (capsuleTransform != null)
{
// 캡슐의 회전 각도 계산
float rotationAngle = Time.time * rotationSpeed;
// 쿼터니언으로 캡슐의 회전을 설정
Quaternion rotation = Quaternion.Euler(0f, rotationAngle, 0f);
// 캡슐의 회전을 기반으로 원 주위를 도는 위치 계산
Vector3 positionOffset = rotation * (Vector3.forward * radius);
// 부모 오브젝트의 위치를 기준으로 위치 설정
transform.position = capsuleTransform.position + positionOffset;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public float speed = 1f;
public Rigidbody playerRigidbody;
public GameObject bulletPrefab;
void Update()
{
PlayerMove();
}
private void PlayerMove()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(h, 0, v) * speed;
transform.Translate(movement * Time.deltaTime);
}
private void OnTriggerEnter(Collider other)
{
// 충돌한 오브젝트가 총알인지 확인
if (other.CompareTag("Bullet"))
{
// 플레이어를 파괴
Destroy(gameObject);
Debug.Log("Game over");
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
[SerializeField] GameObject player;
[SerializeField] public GameObject bulletPrefab;
private float spawnInterval = 3f; // 총알 생성 간격
private Vector3 spawnPosition; // 총알이 생성될 위치
void Start()
{
spawnPosition = this.gameObject.transform.position; // 총알이 생성될 위치 초기화
StartCoroutine(SpawnBullet()); // 코루틴 시작
}
IEnumerator SpawnBullet()
{
while (player != null)
{
// 총알 생성
Instantiate(bulletPrefab, spawnPosition, Quaternion.identity);
// 일정한 간격 기다리기
yield return new WaitForSeconds(spawnInterval);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
public float speed = 2f;
public GameObject player;
void Start()
{
player = GameObject.FindGameObjectWithTag("Player");
// Rigidbody 컴포넌트 가져오기
Rigidbody bullets = GetComponent<Rigidbody>();
Vector3 direction = (player.transform.position - transform.position).normalized;
transform.forward = direction;
// 총알의 이동 방향 설정 및 속도 적용
bullets.velocity = transform.forward * speed;
// 5초 후에 총알 파괴
Destroy(gameObject, 4.5f);
}
private void OnTriggerEnter(Collider other)
{
// 충돌한 오브젝트가 총알인지 확인
if (other.CompareTag("Player"))
{
Destroy(gameObject);
}
if (other.CompareTag("Wall"))
{
Destroy(gameObject);
}
}
}
using System.CodeDom.Compiler;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.SearchService;
public class coTest : MonoBehaviour
{
void Start()
{
//StartCoroutine(this.Sol1());
//StartCoroutine(this.Sol2());
}
void Update()
{
}
//1. 주어진 시간(예: 5초) 동안 특정 작업을 반복 실행하고 그 후에 작업을 중지하는 코루틴을 작성하세요.
//IEnumerator Sol1()
//{
// Debug.LogFormat("시작"); // 처음에 0초 출력
// yield return new WaitForSeconds(1); // 1초 대기
// for (int i = 1; i <= 5; i++)
// {
// Debug.LogFormat("{0}초 경과", i);
// yield return new WaitForSeconds(1); // 각 반복마다 1초씩 대기
// }
// Debug.Log("정지");
//}
//2. 리스트에 있는 항목을 하나씩 출력하고 각 항목을 출력한 후에 잠시 대기하는 코루틴을 만들어보세요.
//IEnumerator Sol2()
//{
// string[] items = { "cap", "clothes", "pants", "shoes" };
// foreach (string item in items)
// {
// Debug.Log(item);
// yield return new WaitForSeconds(1);
// }
// Debug.Log("모든 아이템 출력 완료");
//}
//3.플레이어가 특정 키를 누를 때까지 게임을 일시 중지하고, 그 키를 누르면 다시 게임을 계속하는 코루틴을 작성하세요.
}
using System.Collections;
using UnityEngine;
public class GameLogic : MonoBehaviour
{
private bool gamePaused = false;
void Start()
{
StartCoroutine(GameLoop());
}
//3. 플레이어가 특정 키를 누를 때까지 게임을 일시 중지하고, 그 키를 누르면 다시 게임을 계속하는 코루틴을 작성하세요.
IEnumerator GameLoop()
{
while (true)
{
if (!gamePaused)
{
// 게임이 진행 중인 경우 여기에 원하는 게임 로직을 추가
Debug.Log("게임 진행 중");
}
// 플레이어가 스페이스바를 누르면 일시 중지 상태를 변경
if (Input.GetKeyDown(KeyCode.Space))
{
gamePaused = !gamePaused;
if (gamePaused)
{
Debug.Log("게임 일시 중지");
Time.timeScale = 0f; // 게임 시간 정지
}
else
{
Debug.Log("게임 재개");
Time.timeScale = 1f; // 게임 시간 재개
}
}
yield return null;
}
}
}