From 61425df3cdba84c6eaf3a3803119c4cc4aa24f4d Mon Sep 17 00:00:00 2001 From: hublemon <113337611+hublemon@users.noreply.github.com> Date: Tue, 27 Sep 2022 23:54:12 +0900 Subject: [PATCH] Add files via upload --- CameraController.cs | 38 +++++++++++++++++++++ CapsuleController.cs | 43 ++++++++++++++++++++++++ CapsuleSpawner.cs | 47 ++++++++++++++++++++++++++ DamageMessage.cs | 10 ++++++ IDamageable.cs | 9 +++++ PlayerController.cs | 62 +++++++++++++++++++++++++++++++++++ PlayerInput.cs | 25 ++++++++++++++ Shoot.cs | 78 ++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 312 insertions(+) create mode 100644 CameraController.cs create mode 100644 CapsuleController.cs create mode 100644 CapsuleSpawner.cs create mode 100644 DamageMessage.cs create mode 100644 IDamageable.cs create mode 100644 PlayerController.cs create mode 100644 PlayerInput.cs create mode 100644 Shoot.cs diff --git a/CameraController.cs b/CameraController.cs new file mode 100644 index 00000000..19f232fb --- /dev/null +++ b/CameraController.cs @@ -0,0 +1,38 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class CameraController : MonoBehaviour +{ + public GameObject player; + + public float turnSpeed = 0.5f; // ¸¶¿ì½º ȸÀü ¼Óµµ + private float xRotate = 0.0f; // ³»ºÎ »ç¿ëÇÒ XÃà ȸÀü·®Àº º°µµ Á¤ÀÇ + + // Start is called before the first frame update + void Start() + { + + } + + // Update is called once per frame + void Update() + { + float yRotateSize = Input.GetAxis("Mouse X") * turnSpeed; + float yRotate = transform.eulerAngles.y + yRotateSize; + + float xRotateSize = -Input.GetAxis("Mouse Y") * turnSpeed; + // À§¾Æ·¡ ȸÀü·®À» ´õÇØÁÖÁö¸¸ -45µµ ~ 80µµ·Î Á¦ÇÑ (-45:ÇϴùæÇâ, 60:¹Ù´Ú¹æÇâ) + // Clamp ´Â °ªÀÇ ¹üÀ§¸¦ Á¦ÇÑÇÏ´Â ÇÔ¼ö + xRotate = Mathf.Clamp(xRotate + xRotateSize, -45, 60); + + // Ä«¸Þ¶ó ȸÀü·®À» Ä«¸Þ¶ó¿¡ ¹Ý¿µ(X, YÃุ ȸÀü) + transform.eulerAngles = new Vector3(xRotate, yRotate, 0); + + //°°Àº ¹æÇ⠹ٶ󺸱â + Vector3 dir = player.transform.position - transform.position; + dir.y = 0; + Quaternion rot = Quaternion.LookRotation(dir.normalized); + transform.rotation = rot; + } +} diff --git a/CapsuleController.cs b/CapsuleController.cs new file mode 100644 index 00000000..678b72c7 --- /dev/null +++ b/CapsuleController.cs @@ -0,0 +1,43 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class CapsuleController : MonoBehaviour, IDamageable +{ + float health = 100f; + bool dead = false; + public virtual bool ApplyDamage(DamageMessage damageMessage) + { + if (dead) return false; //Á×Àº »óÅÂ¸é µ¥¹ÌÁö ¾È ÀÔÀ½ + + health -= damageMessage.amount; + + if (health <= 0) Die_effect(gameObject); + + return true; + } + + void Die() + { + gameObject.SetActive(false); + dead = true; + } + + void Die_effect(GameObject capsule) + { + gameObject.GetComponent().Play(); + Invoke("Die", 0.5f); + } + + // Start is called before the first frame update + void Start() + { + + } + + // Update is called once per frame + void Update() + { + + } +} diff --git a/CapsuleSpawner.cs b/CapsuleSpawner.cs new file mode 100644 index 00000000..09e10422 --- /dev/null +++ b/CapsuleSpawner.cs @@ -0,0 +1,47 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class CapsuleSpawner : MonoBehaviour +{ + public GameObject spawner; + BoxCollider spawnerCollider; + + private void Awake() + { + spawnerCollider = spawner.GetComponent(); + //ÄݶóÀÌ´õ À§¿¡ ·£´ýÀ¸·Î ĸ½¶ »ý¼ºÇϱâ + } + + Vector3 RandomPosition() + { + Vector3 originPosition =spawner.transform.position; + // ÄݶóÀÌ´õÀÇ »çÀÌÁ °¡Á®¿À´Â bound.size »ç¿ë + float range_X = spawnerCollider.bounds.size.x; + float range_Z = spawnerCollider.bounds.size.z; + + range_X = Random.Range((range_X / 2) * -1, range_X / 2); + range_Z = Random.Range((range_Z / 2) * -1, range_Z / 2); + float range_Y = Random.Range(2f, 4f); + Vector3 RandomPostion = new Vector3(range_X, range_Y, range_Z); + + Vector3 spawnPosition = originPosition + RandomPostion; + return spawnPosition; + } + public GameObject capsule; + private void Start() + { + StartCoroutine(RandomRespawn_Coroutine()); //3ÃÊ °£°ÝÀ¸·Î ĸ½¶ »ý¼º + } + + IEnumerator RandomRespawn_Coroutine() + { + while (true) + { + yield return new WaitForSeconds(3f); + + Instantiate(capsule, RandomPosition(), Quaternion.identity); + + } + } +} diff --git a/DamageMessage.cs b/DamageMessage.cs new file mode 100644 index 00000000..709471dc --- /dev/null +++ b/DamageMessage.cs @@ -0,0 +1,10 @@ +using UnityEngine; + +public struct DamageMessage +{ + public GameObject damager; + public float amount; + + public Vector3 hitPoint; + public Vector3 hitNormal; +} \ No newline at end of file diff --git a/IDamageable.cs b/IDamageable.cs new file mode 100644 index 00000000..a9c49b81 --- /dev/null +++ b/IDamageable.cs @@ -0,0 +1,9 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public interface IDamageable +{ + //°ø°Ý ´çÇÏ´Â ¾ÖµéÇÑÅ× ÀϰýÀûÀ¸·Î DamageMessage ÁÖ±â + bool ApplyDamage(DamageMessage damageMessage); +} diff --git a/PlayerController.cs b/PlayerController.cs new file mode 100644 index 00000000..591d9892 --- /dev/null +++ b/PlayerController.cs @@ -0,0 +1,62 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class PlayerController : MonoBehaviour +{// ¸¶¿ì½º¿¡ µû¶ó ¹æÇ⠹ٲٰí bulletGenerator¿ªÇÒ ¼öÇà + + public GameObject bulletPrefab; + public int weight; //¼Óµµ °¡ÁßÄ¡ + + RaycastHit hit; + public int turnSpeed; + Quaternion dr; + + public float moveSpeed; //±×¶ó¿îµå¿¡¼­ÀÇ ¿òÁ÷ÀÓ + private float xRotate = 0.0f; //»óÇÏ ¹Ù¶óº¸±â + + PlayerInput playerInput; //ÄÄÆ÷³ÍÆ®´Â start¿¡¼­ ÇÒ´ç½ÃŰÀÚ + + public void Move(Vector3 dir) + { + transform.Translate(dir); + } + // Start is called before the first frame update + void Start() + { + playerInput = GetComponent(); + } + + private void FixedUpdate() + { + //¹°¸® °»½Å Áֱ⿡ ¸ÂÃç ¿òÁ÷ÀÓ + + float move_X = playerInput.move_x; + float move_Z = playerInput.move_z; + Vector3 move = new Vector3(move_X, 0, move_Z); + + Move(transform.TransformDirection(move) * Time.deltaTime*10); + + + } + + // Update is called once per frame + void Update() + { + + + if (Input.GetMouseButton(1)) //¿À¸¥ÂÊ ¹öưÀ¸·Î ¹æÇ⠹ٲٱâ + { + Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit); //hit¿¡ Ãæµ¹Á¤º¸ ÀúÀå + Vector3 click = hit.point; + + dr = Quaternion.LookRotation((click - transform.position).normalized); //Ŭ¸¯ÇÑ ¹æÇâÀ¸·Î ȸÀüÇØ¶ó + transform.rotation = Quaternion.Slerp(transform.rotation, dr, turnSpeed * Time.deltaTime); //´ÜÀ§ ½Ã°£´ç ȸÀü + + } + + } + + +} + diff --git a/PlayerInput.cs b/PlayerInput.cs new file mode 100644 index 00000000..008c614a --- /dev/null +++ b/PlayerInput.cs @@ -0,0 +1,25 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class PlayerInput : MonoBehaviour +{ + public string moveX = "Horizontal"; + public string moveZ = "Vertical"; + public string mouseX = "Mouse X"; + + // Start is called before the first frame update + //¿ÜºÎ¿¡¼­ °ª Àб⸸ °¡´É + public float move_x { get; private set; } + public float move_z { get; private set; } + public float mouse_x { get; private set; } //ÀÏ´Ü ³²°ÜµÎÀÚ + + + // Update is called once per frame + void Update() + { + move_x = Input.GetAxis(moveX); + move_z = Input.GetAxis(moveZ); + mouse_x = Input.GetAxis(mouseX); + } +} \ No newline at end of file diff --git a/Shoot.cs b/Shoot.cs new file mode 100644 index 00000000..5dd4f91e --- /dev/null +++ b/Shoot.cs @@ -0,0 +1,78 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Shoot : MonoBehaviour +{ + public GameObject Bullet; + public Transform FirePos; //Ãѱ¸ + + //ÃÑ¾Ë ¹ß»ç + public Camera mainCamera; + RaycastHit hit; + + // Start is called before the first frame update + void Start() + { + + } + + // Update is called once per frame + void FixedUpdate() + { + if (Input.GetMouseButtonDown(0)) + { + Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); + + //Ãæµ¹°ú »ó°ü¾øÀÌ ¸¸µé¾îÁö´Â ÃÑ¾Ë + Instantiate(Bullet, FirePos.transform.position, FirePos.transform.rotation); + + if (Physics.Raycast(ray, out hit,100f)) + { + transform.LookAt(hit.point); //Ŭ¸¯ÇÑ ¹æÇ⠹ٶ󺸱â + + // Ãæµ¹ÇÑ »ó´ë¹æÀ¸·ÎºÎÅÍ IDamageable ¿ÀºêÁ§Æ®¸¦ °¡Á®¿À±â ½Ãµµ + var target = + hit.collider.GetComponent(); + + // »ó´ë¹æÀ¸·Î ºÎÅÍ IDamageable ¿ÀºêÁ§Æ®¸¦ °¡Á®¿À´Âµ¥ ¼º°øÇß´Ù¸é + if (target != null) + { + DamageMessage damageMessage; + + damageMessage.damager = this.gameObject; + damageMessage.amount = 25f; + damageMessage.hitPoint = hit.point; + damageMessage.hitNormal = hit.normal; + + // »ó´ë¹æÀÇ OnDamage ÇÔ¼ö¸¦ ½ÇÇà½ÃÄѼ­ »ó´ë¹æ¿¡°Ô µ¥¹ÌÁö ÁÖ±â + target.ApplyDamage(damageMessage); + + //ÃÑ¾Ë À̵¿: ±Ùµ¥ ÃѾËÀÌ ¾È º¸ÀÓ(¼Óµµ 1f·Î ÇØµµ ¸¶Âù°¡Áö) + Bullet.transform.position = Vector3.MoveTowards(Bullet.transform.position, hit.point, 1f); + } + } + //Ãæµ¹ ¾ÈÇØµµ Àß°¡¾ß ÇÔ + else + { + Bullet.transform.position = Vector3.MoveTowards(Bullet.transform.position, ray.direction * 100f, 1f); + } + + if (Mathf.Abs(Bullet.transform.position.x) >= 100 || Mathf.Abs(Bullet.transform.position.z) >= 100) + { + Bullet.SetActive(false); + Destroy(Bullet); + } + + } + } + private void OnTriggerEnter(Collider other) //Ãæµ¹ÇÏ¸é »ç¶óÁö±â + { + if (other.tag == "Enemy") + { + Bullet.GetComponent().enabled = false; + Bullet.SetActive(false); + Destroy(Bullet); + } + } +}