유니티
04/09 유니티 슈팅게임 연습
박준희
2021. 4. 9. 15:28
미구현
게임오버 화면
점수
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public Rigidbody playerRigidbody;
public GameObject player;
public float speed = 8f;
public int hp = 100;
public UIController uiController;
private Animation animation;
private float delta;
// Start is called before the first frame update
void Start()
{
animation = player.GetComponent<Animation>();
}
// Update is called once per frame
void Update()
{
//if (Input.GetKey(KeyCode.UpArrow)) {
// this.playerRigidbody.AddForce(0, 0, this.speed);
//}
//if (Input.GetKey(KeyCode.DownArrow)) {
// this.playerRigidbody.AddForce(0, 0, -this.speed);
//}
//if (Input.GetKey(KeyCode.LeftArrow)) {
// this.playerRigidbody.AddForce(-this.speed, 0, 0);
//}
//if (Input.GetKey(KeyCode.RightArrow)) {
// this.playerRigidbody.AddForce(this.speed, 0, this.speed);
//}
//수평축과 수직축의 입력값을 감지해서 저장
float xInput = Input.GetAxis("Horizontal");
float yInput = Input.GetAxis("Vertical");
if(xInput != 0 || yInput != 0)
{
Debug.LogFormat("{0} {1}", xInput, yInput);
float xSpeed = xInput * speed;
float ySpeed = yInput * speed;
Vector3 newVelocity = new Vector3(xSpeed, 0, ySpeed);
Quaternion targetRot = Quaternion.LookRotation(newVelocity);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRot, 0.15f);
playerRigidbody.velocity = newVelocity;
if(animation.IsPlaying("idle@loop") || !animation.isPlaying)
{
animation.Play("run@loop");
}
}
else
{
if (animation.IsPlaying("run@loop") || !animation.isPlaying)
{
animation.Play("idle@loop");
}
}
if (hp <= 0)
{
delta += Time.deltaTime;
animation.Play("die");
if(delta > 0.7f)
{
Die();
}
}
}
public void Die()
{
this.gameObject.SetActive(false);
}
public void Hit()
{
animation.Play("damage");
this.hp -= 10;
Debug.Log(hp);
uiController.DecreaseHp(this.hp);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
public float speed = 8f;
private Rigidbody bulletRigidbody;
// Start is called before the first frame update
void Start()
{
//게임오브젝트에서 Rigidbody 컴포넌트를 찾아
this.bulletRigidbody = this.GetComponent<Rigidbody>();
this.bulletRigidbody.velocity = this.transform.forward * speed;
Destroy(this.gameObject, 3f);
}
private void OnTriggerEnter(Collider other)
{
if(other.tag == "Player")
{
PlayerController playerController = other.GetComponent<PlayerController>();
if(playerController != null)
{
playerController.Hit();
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletSpawner : MonoBehaviour
{
public GameObject bulletPrefab;
public float spawnRateMin = 0.5f;
public float spawnRateMax = 3;
public PlayerController player;
private Transform target;
private float spawnRate;
private float timeAfterSpawn;
// Start is called before the first frame update
void Start()
{
this.timeAfterSpawn = 0;
this.spawnRate = Random.Range(this.spawnRateMin, this.spawnRateMax);
this.target = FindObjectOfType<PlayerController>().transform;
}
// Update is called once per frame
void Update()
{
if (player.hp > 0)
{
this.timeAfterSpawn += Time.deltaTime;
if (this.timeAfterSpawn >= this.spawnRate)
{
this.timeAfterSpawn = 0;
GameObject bullet = Instantiate(this.bulletPrefab, this.transform.position, this.transform.rotation);
bullet.transform.LookAt(target);
this.spawnRate = Random.Range(this.spawnRateMin, this.spawnRateMax);
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIController : MonoBehaviour
{
// Start is called before the first frame update
public Text hpText;
public Image hpGauge;
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void DecreaseHp(int hp)
{
this.hpGauge.GetComponent<Image>().fillAmount -= 0.1f;
this.hpText.GetComponent<Text>().text = hp +"/100";
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameOver : MonoBehaviour
{
// Start is called before the first frame update
private GameOver gameOver;
void Start()
{
this.gameOver = GetComponent<GameOver>();
}
// Update is called once per frame
void Update()
{
}
public void Gameover()
{
gameOver.gameObject.SetActive(true);
}
}