using UnityEngine;
public class catContoller : MonoBehaviour
{
public GameDirector gameDirector;
public float radius = 1;
void Update()
{
if (gameDirector.isGameOver) return; // 게임 오버라면 움직임x
if (this.transform.position.x <= -8)
{
if (Input.GetKeyDown(KeyCode.RightArrow))
{
//오른쪽 화살표를 눌렀다면 x축으로 2유닛만큼 이동
this.transform.Translate(2, 0, 0);
}
}
else if (this.transform.position.x >= 7)
{
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
//왼쪽 화살표를 눌렀다면 x축으로 -2유닛만큼 이동
this.transform.Translate(-2, 0, 0);
}
}
else
{
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
//왼쪽 화살표를 눌렀다면 x축으로 -2유닛만큼 이동
this.transform.Translate(-2, 0, 0);
}
else if (Input.GetKeyDown(KeyCode.RightArrow))
{
//오른쪽 화살표를 눌렀다면 x축으로 2유닛만큼 이동
this.transform.Translate(2, 0, 0);
}
}
//키 입력 받기
}
public void MoveRight()
{
if (gameDirector.isGameOver || this.transform.position.x >= 7) return;
this.transform.Translate(2, 0, 0);
}
public void MoveLeft()
{
if (gameDirector.isGameOver || this.transform.position.x <= -8) return;
this.transform.Translate(-2, 0, 0);
}
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(this.transform.position, this.radius);
}
}
using UnityEngine;
using UnityEngine.UI;
public class GameDirector : MonoBehaviour
{
private GameObject catGo;
private GameObject hpGageGo;
public bool isGameOver = false;
public GameObject gameOverText;
public Text scoreText; // 점수를 표시할 텍스트
private int score = 0; // 현재 점수
void Start()
{
this.catGo = GameObject.Find("cat");
this.hpGageGo = GameObject.Find("hpGage");
gameOverText.SetActive(false); // 초기에 게임 오버 텍스트를 숨김
UpdateScoreText();
}
public void DecreaseHp()
{
Image hpGage = this.hpGageGo.GetComponent<Image>();
hpGage.fillAmount -= 0.1f;
if (hpGage.fillAmount <= 0)
{
GameOver();
}
}
public void IncreaseScore() // 점수 증가 함수
{
if (isGameOver) return;
score += 100;
UpdateScoreText();
}
private void UpdateScoreText() // 점수 텍스트 업데이트 함수
{
scoreText.text = "Score: " + score.ToString();
}
private void GameOver()
{
isGameOver = true;
gameOverText.SetActive(true); // 게임 오버 텍스트를 활성화
}
}
using UnityEngine;
public class ArrowGenerator : MonoBehaviour
{
public GameObject arrowPrefab; // 화살 프리팹
public GameDirector gameDirector;
private float elapsedTime; // 경과 시간
void Update()
{
if (gameDirector.isGameOver) return; // 게임 오버라면 화살을 생성x
this.elapsedTime += Time.deltaTime;
if (this.elapsedTime > 1f)
{
this.CreateArrow();
this.elapsedTime = 0;
}
}
private void CreateArrow()
{
GameObject arrowGo = Instantiate(this.arrowPrefab);
arrowGo.transform.position = new Vector3(Random.Range(-7, 8), 6f, 0);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class arrowController : MonoBehaviour
{
[SerializeField] //SerializeField 애트리뷰트를 사용하면 private 맴버도 인스펙터에 노출
private float speed;
private GameObject catGo;
// Start is called before the first frame update
void Start()
{
this.catGo = GameObject.Find("cat");
Debug.Log("catGo: {0}", this.catGo);
}
private bool IsCollide()
{
//두 원사이의 거리
//Vector3 p1 = this.transform.position;//화살의 중점
//Vector3 p2 = this.catGo.transform.position; //고양이의 중점
//Vector3 dir = p1 - p2; //물리벡터 (크기와 방향)
//float distance = dir.magnitude;
//Debug.Log(distance);
var dis = Vector3.Distance(this.transform.position, this.catGo.transform.position);
Debug.LogFormat("두 원사이의 거리 : {0}", dis);
//반지름의 합
catContoller catController = this.catGo.GetComponent<catContoller>();
var sumRadius = this.radius + catController.radius;
Debug.LogFormat("두 원의 반지름의 합 : {0}", sumRadius);
//두 원사이의 거리가 두 원의 반지름의 합보다 크면 false (부딪히지 않았다)
//두 원의 반지름의 합 이 두 원사이의 거리보다 크다면 부딪혔다
return dis < sumRadius;
}
// Update is called once per frame
void Update()
{
//Time.deltaTime : 이전 프레임과 현제 프레임 사이의 간격 시간
//Debug.Log(Time.deltaTime);
//프레임 기반 이동
//this.gameObject.transform.Translate(0, -1, 0);
//시간기반 이동
//속도 * 방향 * 시간
//1f * new Vector3(0, -1, 0) * Time.deltaTime
//this.gameObject.transform.Translate(this.speed * Vector3.down * Time.deltaTime);
this.transform.Translate(this.speed * Vector3.down * Time.deltaTime);
if (this.transform.position.y <= -3.9f)
{
GameDirector gameDirector = GameObject.Find("GameDirector").GetComponent<GameDirector>();
gameDirector.IncreaseScore(); // 점수 증가 함수 호출
Destroy(this.gameObject);
}
if (this.IsCollide())
{
Debug.LogFormat("충돌했다!");
GameObject gameDirectorGo = GameObject.Find("GameDirector");
GameDirector gameDirector = gameDirectorGo .GetComponent<GameDirector>();
gameDirector.DecreaseHp();
Destroy(this.gameObject); //ArrowController컴포넌트가 붙어있는 게임오브젝트를 씬에서 제거한다
}
else
{
Debug.LogFormat("충돌하지 않았다!");
}
}
public float radius = 1;
//이벤트 함수
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(this.transform.position, this.radius);
}
}
반응형
'KDT > 유니티 기초' 카테고리의 다른 글
[Pixel Sword] 캐릭터 동작과 애니메이션 구현 + 캐릭터 위의 땅 충돌시 오류 수정 (0) | 2023.08.04 |
---|---|
[ClimbCloud] 제작 (0) | 2023.08.02 |
[CatEscape] 화살표와 고양이 충돌 판정 (0) | 2023.08.01 |
[SwipeCar] SwipeCar 만들기 + 오디오 출력 (0) | 2023.08.01 |
수리검 던지기 게임 만들기 (0) | 2023.08.01 |