반응형
SMALL
PlayerContorller script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
public float radius;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
if(this.transform.position.x < -7f)
{
this.transform.position = new Vector3(-4.93f, -3.54f, 0f);
Debug.LogFormat("{0}", this.transform.position);
}
Debug.Log("왼쪽 화살표 눌림");
this.transform.Translate(-3, 0, 0); //현재 위치에서 -3 유닛만큼 이동
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
if (this.transform.position.x > 7f)
{
this.transform.position = new Vector3(4.93f, -3.54f, 0f);
Debug.LogFormat("{0}", this.transform.position);
}
Debug.Log("오른쪽 화살표 눌림");
this.transform.Translate(3, 0, 0); //현재 위치에서 +3 유닛만큼 이동
}
}
//이벤트 함수
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(this.transform.position, this.radius);
}
public void RButtonDown()
{
this.transform.Translate(3,0,0);
}
public void LButtonDown()
{
this.transform.Translate(-3, 0, 0);
}
}
AroowGenerator scripts
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()
{
//GameObject arrowGo = Instantiate(this.arrowPrefab); // Object는 빼는게 관례
//arrowGo.name = "테스트";
}
float delta;
// Update is called once per frame
void Update()
{
/*this.delta = Time.deltaTime;*/ //매프레임 간격을 누적
//Debug.LogFormat("{0", delta);
//Debug.LogFormat("{0}", Time.deltaTime);
if (isRun)
{
this.delta += Time.deltaTime; //시간누적
if (delta > 1 ) //1초지남을 뜻한다.
{
Debug.Log("화살 생성");
GameObject arrowGo = Instantiate(this.arrowPrefab);
delta = 0;
//랜덤 X 좌표로 위치 변경
// int minexclusive int maxExcluesive
//-6~(7-1)
int x = Random.Range(-6, 7);
arrowGo.transform.position = new Vector2((int)x, 5.66f);
}
}
}
public void Stop()
{
isRun= false;
}
}
ArrowController script
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
public class ArrowController : MonoBehaviour
{
public float radius;
private GameObject player;
private const float GROUND = -3.978f;
// Start is called before the first frame update
void Start()
{
this.player = GameObject.Find("player");
}
// Update is called once per frame
void Update()
{
//프레임 베이스 이동
this.transform.Translate(0, -0.1f, 0); //아래쪽으로 -0.1 유닛씩 이동
//방향*시간*속도 = 시간베이스 이동
float speed = 1f;
var movement = -1*speed*Time.deltaTime;
this.transform.Translate(0,movement,0);
Vector2 p1 = this.transform.position; //화살표의 중심 좌표
Vector2 p2 = this.player.transform.position; //플레이어의 중심좌표
var distance = Vector2.Distance(p1, p2);
var r = this.radius + this.player.GetComponent<PlayerController>().radius;
// 충돌
if (distance < r)
{
GameObject dir = GameObject.Find("GameDirector");
dir.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);
}
}
GameDirector script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameDirector : MonoBehaviour
{
public Button btnR;
public Button btnL;
private GameObject timer;
public ArrowGenerator arrowGen;
bool gameover = false;
private GameObject hpGauge;
// Start is called before the first frame update
void Start()
{
timer = GameObject.Find("timer");
this.hpGauge = GameObject.Find("hpGauge");
var player = GameObject.Find("player").GetComponent<PlayerController>();
this.btnR.onClick.AddListener(()=>
{
player.LButtonDown();
});
this.btnL.onClick.AddListener(()=>
{
player.RButtonDown();
});
}
// Update is called once per frame
float delta = 10;
void Update()
{
if (!gameover)
{
delta -= Time.unscaledDeltaTime;
if (delta <= 0)
{
gameover = true;
this.timer.GetComponent<Text>().text = "Game Over";
arrowGen.Stop();
}
}
this.timer.GetComponent<Text>().text = string.Format("{0:0}", delta);
}
public void DecreaseHp()
{
//hpGauge 게임 오브젝트의 Image 컴포넌트 가져와서
var img = this.hpGauge.GetComponent<Image>();
//fillAmount 속성값 변경
img.fillAmount -= 0.1f;
}
}
game 화면 (현재 잘되던 gameover가 뜨지 않아 보수 예정)
반응형
LIST
'유니티 엔진 클라이언트 > 수업내용' 카테고리의 다른 글
[유니티] 스테이지 UI 구현 (0) | 2023.02.17 |
---|---|
[유니티] 미션 인포 만들기 (info & data 직, 역직렬화 (0) | 2023.02.16 |
[유니티] UI 애니메이션 스크립트 제어 + 애니메이터 사용 (별던지기) (0) | 2023.02.08 |
[UNITY] 코루틴 연습 (0) | 2023.02.01 |
[유니티] 엔진 사용 충돌 판정 만들 (0) | 2023.01.30 |