응용프로그램/유니티(Unity)

Unity - 데미지 시스템

zelkova 2016. 11. 14. 16:13

<목차로 돌아가기>

 

 

  데미지 시스템

출처 : Sebastian Lague

 

상속과 오버라이드 virtual, override

base.start();

 

변수의 접근제한 protected

 

interface

 

IDamageable.cs

 

  1.  using UnityEngine; 
  2.  public interface IDamageable {
  3.     void TakeHit(float damage, RaycastHit hit);
  4.  } 

 

 

LivingEntity.cs

 

  1. using UnityEngine;
  2.  using System.Collections;
  3.  
  4.  public class LivingEntity : MonoBehaviour, IDamageable {
  5.  
  6.     public float startingHealth;
  7.     protected float health;
  8.     protected bool dead;
  9.  
  10.     protected virtual void Start()
  11.     {
  12.        health = startingHealth;
  13.     }
  14.  
  15.     public void TakeHit(float damage, RaycastHit hit)
  16.     {
  17.         health -= damage;
  18.  
  19.         if(health <=0 && !dead)
  20.         {
  21.             Die();
  22.         }
  23.     }
  24.  
  25.     protected void Die()
  26.     {
  27.        dead = true;
  28.        GameObject.Destroy(gameObject);
  29.     }
  30.  }

 

 

Player.cs

  1. using UnityEngine; using System.Collections;
  2.  
  3.  [RequireComponent (typeof (PlayerController))]
  4.  [RequireComponent(typeof(GunController))]
  5.  public class Player : LivingEntity {
  6.  
  7.     public float moveSpeed = 5;
  8.     PlayerController controller;
  9.     GunController gunController;
  10.  
  11.     Camera viewCamera;
  12.  
  13.     protected override void Start () {
  14.         base.Start();
  15.         controller = GetComponent();
  16.         gunController = GetComponent();
  17.         viewCamera = Camera.main;
  18.  
  19.     }
  20.  
  21.    void Update ()
  22.    {
  23.         //movement input
  24.         Vector3 moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 
  25.             0, Input.GetAxisRaw("Vertical"));
  26.         Vector3 moveVelocity = moveInput.normalized * moveSpeed;
  27.         controller.Move(moveVelocity);
  28.  
  29.         //look input
  30.         Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition);
  31.         Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
  32.         float rayDistance;
  33.  
  34.         if (groundPlane.Raycast(ray,out rayDistance))
  35.         {
  36.            Vector3 point = ray.GetPoint(rayDistance);
  37.            //Debug.DrawLine(ray.origin, point, Color.red);
  38.            controller.LookAt(point);
  39.         }
  40.    
  41.         //weapon input
  42.         if (Input.GetMouseButton(0))
  43.         {
  44.            gunController.Shoot();
  45.         }
  46.     }
  47.  }

Enemy.cs

 

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. [RequireComponent (typeof (NavMeshAgent))]
  5. public class Enemy : LivingEntity
  6. {
  7.  
  8.     NavMeshAgent pathfinder;
  9.     Transform playertargeting;
  10.  
  11.     protected override void Start () {
  12.         base.Start();
  13.         pathfinder = GetComponent();
  14.         playertargeting = GameObject.FindWithTag("Player").GetComponent();
  15.  
  16.         StartCoroutine(UpdatePath());
  17.     }
  18.  
  19.     void Update () {
  20.  
  21.     }
  22.  
  23.     IEnumerator UpdatePath()
  24.     {
  25.        float refreshRate = 0.25f;
  26.  
  27.     while (playertargeting != null)
  28.     {
  29.        Vector3 targetPosition = new Vector3(playertargeting.position.x, 0, 
  30.               playertargeting.position.z);
  31.        if(!dead)
  32.        {
  33.        pathfinder.SetDestination(targetPosition);
  34.        }
  35.        yield return new WaitForSeconds(refreshRate);
  36.        }
  37.     }
  38. }

 

 

 

반응형