PlayerController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float radius;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.RightArrow))
{
this.transform.Translate(3, 0, 0);
}
else if (Input.GetKeyDown(KeyCode.LeftArrow))
{
this.transform.Translate(-3, 0, 0);
}
}
public void LButtonDown()
{
this.transform.Translate(-3, 0, 0);
}
public void RButtonDown()
{
this.transform.Translate(3, 0, 0);
}
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(this.transform.position, 1f);
}
}
ArrowController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ArrowController : MonoBehaviour
{
GameObject player;
public float radius;
public const float GROUND = -5.1f;
// Start is called before the first frame update
void Start()
{
this.player = GameObject.Find("player");
}
// Update is called once per frame
void Update()
{
//프레임 베이스
transform.Translate(0, -0.1f, 0);
//시간 베이스
//방향*속도*시간
float speed = 1f;
var movement = -1 * speed * Time.deltaTime;
this.transform.Translate(0, movement, 0);
if (transform.position.y < GROUND)
{
Destroy(gameObject);
}
Vector2 p1 = transform.position;
Vector2 p2 = this.player.transform.position;
float distance = Vector2.Distance(p1, p2);
var r = this.radius + this.player.GetComponent<PlayerController>().radius;
if (distance < r)
{
//감독관에 알려주기
GameObject director = GameObject.Find("GameDirector");
director.GetComponent<GameDirector>().DecreaseHp();
//충돌
Destroy(this.gameObject); //게임 오브젝트 제거
}
if (this.transform.position.y < GROUND)
{
Destroy(this.gameObject); //게임 오브젝트 제거
//Destroy(this); //ArrowController의 인스턴스 제거 : 컴포넌트 제거
}
}
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(this.transform.position, this.radius);
}
}
ArrowGenerator
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ArrowGenerator : MonoBehaviour
{
public GameObject arrowPrefab;
bool isRun = true;
// Start is called before the first frame update
void Start()
{
Debug.LogFormat("{0}", arrowPrefab);
}
float delta;
// Update is called once per frame
void Update()
{
if (isRun==true)
{
//시간누적
this.delta += Time.deltaTime; //매 프레임 간격 시간을 누적
if (this.delta > 1) //1초 지남
{
this.delta = 0;
Debug.Log("화살 생성");
//arrowPrefab의 인스턴스 생성
GameObject go = Instantiate(this.arrowPrefab);
//랜덤 x 좌표로 위치 변경
//-6~6
var x = Random.Range(-6, 7);
//생성된 화살 오브젝트의 위치로 사용
go.transform.position = new Vector3(x, 6, 0);
}
}
}
public void Stop()
{
isRun = false;
}
}
GameDirector
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameDirector : MonoBehaviour
{
GameObject timeGo;
private GameObject hpGauge;
public ArrowGenerator arrowGen;
Text textTime;
float delta = 10;
bool gameover = false;
// Start is called before the first frame update
void Start()
{
this.timeGo = GameObject.Find("Time");
this.hpGauge = GameObject.Find("hpGauge");
textTime = this.timeGo.GetComponent<Text>();
}
// Update is called once per frame
void Update()
{
if (gameover == false)
{
delta -= Time.deltaTime;
if (delta < 0)
{
delta = 0.0f;
gameover = true;
Debug.Log("Gameover");
this.arrowGen.Stop();
}
textTime.text = string.Format("남은 시간 : {0:F2}", delta);
}
}
public void DecreaseHp()
{
var img = this.hpGauge.GetComponent<Image>();
img.fillAmount -= 0.1f;
}
}
'Unity > 수업내용' 카테고리의 다른 글
[Unity 3D / 유니티 교과서] 7장 예제 Bamsongi (0) | 2023.02.01 |
---|---|
[Unity 2D / 유니티 교과서] 6장 예제 ClimbCloud (0) | 2023.02.01 |
[Unity 2D] 스와이프 예제 변형 (0) | 2023.01.30 |
[Unity 2D / 유니티 교과서] 4장 예제 SwipeCar (0) | 2023.01.30 |
[Unity 2D / 유니티교과서] 3장 예제 Roulette (0) | 2023.01.30 |