etc-posts/Unity :: C# 튜토리얼

[유니티C#][기초] 17. 튜토리얼 SurvivalShooter 정리.6

플라즈밍 2018. 7. 14. 16:24
반응형

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
 
 
2-6 EnemyHealth
using UnityEngine;
 
public class EnemyHealth : MonoBehaviour
{
    public int startingHealth = 100;
    public int currentHealth;
    public float sinkSpeed = 2.5f;
    public int scoreValue = 10;
    public AudioClip deathClip;
 
 
    Animator anim;
    AudioSource enemyAudio;
    ParticleSystem hitParticles;
    CapsuleCollider capsuleCollider;
    bool isDead;
    bool isSinking;
 
 
    void Awake ()
    {
        anim = GetComponent <Animator> ();
        enemyAudio = GetComponent <AudioSource> ();
        // 모든 자식을 검토하며 첫번째 컴포넌트가 나오면 그것을 반환
        hitParticles = GetComponentInChildren <ParticleSystem> ();
        capsuleCollider = GetComponent <CapsuleCollider> ();
 
        currentHealth = startingHealth;
    }
 
 
    void Update ()
    {
        if(isSinking)
        {
            //Translate(Vector 값) 값을 이동시킨다.
            //Update에 Time.deltaTime이라면 1초에 움직이는 량을 나타낸다. 
            //여기서는 (0,-1,0)*2.5f만큼 초당 움직인다.
            transform.Translate (-Vector3.up * sinkSpeed * Time.deltaTime);
        }
    }
 
 
    public void TakeDamage (int amount, Vector3 hitPoint)
    {
        if(isDead)
            return;
 
        enemyAudio.Play ();
 
        currentHealth -= amount;
            
        hitParticles.transform.position = hitPoint;
        hitParticles.Play();
 
        if(currentHealth <= 0)
        {
            Death ();
        }
    }
 
 
    void Death ()
    {
        isDead = true;
 
        capsuleCollider.isTrigger = true;
 
        anim.SetTrigger ("Dead");
 
        enemyAudio.clip = deathClip;
        enemyAudio.Play ();
    }
 
 
    public void StartSinking ()
    {
        //.SetActive -> 게임오브젝트 껏/켜 
        //.enalbed -> 컴포넌트 껏/켜
        GetComponent <UnityEngine.AI.NavMeshAgent> ().enabled = false;
        // 이걸 키면 물리적 영향을 안받는다네.. 반대 아닌가 ..>?
        GetComponent <Rigidbody> ().isKinematic = true;
        isSinking = true;
        ScoreManager.score += scoreValue;
        Destroy (gameObject, 2f);
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
 
2-7 EnemyAttack
 
using UnityEngine;
using System.Collections;
 
public class EnemyAttack : MonoBehaviour
{
    public float timeBetweenAttacks = 0.5f;
    public int attackDamage = 10;
 
 
    Animator anim;
    GameObject player;
    PlayerHealth playerHealth;
    EnemyHealth enemyHealth;
    bool playerInRange;
    float timer;
 
 
    void Awake ()
    {
        player = GameObject.FindGameObjectWithTag ("Player");
        playerHealth = player.GetComponent <PlayerHealth> ();
        enemyHealth = GetComponent<EnemyHealth>();
        anim = GetComponent <Animator> ();
    }
 
    // 콜리전 엔터와 트리어 엔터 두 종류가 있음
    void OnTriggerEnter (Collider other)
    {
        if(other.gameObject == player)
        {
            playerInRange = true;
        }
    }
 
 
    void OnTriggerExit (Collider other)
    {
        if(other.gameObject == player)
        {
            playerInRange = false;
        }
    }
 
 
    void Update ()
    {
        timer += Time.deltaTime;
 
        if(timer >= timeBetweenAttacks && playerInRange && enemyHealth.currentHealth > 0)
        {
            Attack ();
        }
 
        if(playerHealth.currentHealth <= 0)
        {
            anim.SetTrigger ("PlayerDead");
        }
    }
 
 
    void Attack ()
    {
        timer = 0f;
 
        if(playerHealth.currentHealth > 0)
        {
            playerHealth.TakeDamage (attackDamage);
        }
    }
}
 
 
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 
2-8 EnemyMovement
 
using UnityEngine;
using System.Collections;
 
public class EnemyMovement : MonoBehaviour
{
    Transform player;
    PlayerHealth playerHealth;
    EnemyHealth enemyHealth;
    UnityEngine.AI.NavMeshAgent nav;
 
 
    void Awake ()
    {
        player = GameObject.FindGameObjectWithTag ("Player").transform;
        playerHealth = player.GetComponent<PlayerHealth>();
        enemyHealth = GetComponent<EnemyHealth>();
        nav = GetComponent <UnityEngine.AI.NavMeshAgent> ();
    }
 
 
    void Update ()
    {
        if (enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0)
        {
            nav.SetDestination(player.position);
        }
        else
        {
            nav.enabled = false;
        }
    }
}
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 
2-9 ScoreManager.cs
 
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
 
public class ScoreManager : MonoBehaviour
{
    public static int score;
 
 
    Text text;
 
 
    void Awake ()
    {
        text = GetComponent <Text> ();
        score = 0;
    }
 
 
    void Update ()
    {
        text.text = "Score: " + score;
    }
}
 
 
 
cs


반응형