플라즈밍
플라즈마 IT
플라즈밍
  • All (163)
    • MindSet (2)
    • Wisdom (8)
    • Book (18)
    • [Web] (6)
      • [Web]Guide (2)
      • [Web]HTML-CSS-JS (1)
      • [Web]ReactJS (0)
      • [Web]NextJS (1)
    • 퀀트주식투자 (4)
      • [리포트]포트폴리오 (4)
    • 자산배분전략 (2)
      • [리포트]자산배분전략 (1)
    • 포트폴리오 (0)
      • 발걸음 (0)
    • 개발 Note (3)
    • TipNote (5)
    • 알고리즘 (27)
      • 백준[BOJ] 오답노트 (27)
      • 백준[BOJ] 강의 정리 노트 (0)
    • etc-posts (18)
      • Unity :: C# 튜토리얼 (18)
    • Web&Know (23)
    • 끄적임 (4)
    • 세상이슈 (0)
    • Youtube 유튜브 (3)
      • Youtube 채널소개 (3)
    • 창업 Know&Idea (1)
    • Web&Dev (4)
    • 프로젝트 (6)
      • Unity5 Project (3)
      • UnrealEngine4 Project (2)
      • Web Page (1)
    • 주가차트-기술적분석 (2)
    • BlockChain (7)
    • SystemDesign (11)

인기 글

최근 글

hELLO · Designed By 정상우.
플라즈밍

플라즈마 IT

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);
    }
}
 
Colored by Color Scripter
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);
        }
    }
}
 
 
 
Colored by Color Scripter
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;
        }
    }
}
 
Colored by Color Scripter
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;
    }
}
 
 
 
Colored by Color Scripter
cs


ㅁ

반응형
저작자표시 (새창열림)

'etc-posts > Unity :: C# 튜토리얼' 카테고리의 다른 글

[유니티C#][AR] 누르면 색변하는 차 만들기.1  (0) 2019.01.12
[유니티C#][기초] 16. 튜토리얼 SurvivalShooter 정리.5  (0) 2018.07.14
[유니티C#][기초] 15. 튜토리얼 SurvivalShooter 정리.4  (0) 2018.07.14
[유니티C#][기초] 14. 튜토리얼 SurvivalShooter 정리.3  (0) 2018.07.08
[유니티C#][기초] 13. 튜토리얼 SurvivalShooter 정리.2  (0) 2018.07.08
    'etc-posts/Unity :: C# 튜토리얼' 카테고리의 다른 글
    • [유니티C#][AR] 누르면 색변하는 차 만들기.1
    • [유니티C#][기초] 16. 튜토리얼 SurvivalShooter 정리.5
    • [유니티C#][기초] 15. 튜토리얼 SurvivalShooter 정리.4
    • [유니티C#][기초] 14. 튜토리얼 SurvivalShooter 정리.3
    플라즈밍
    플라즈밍
    퀀트 주식투자 자산배분 데이터분석 정보 공유 프로그래밍,투자 주제의 책 강의 리뷰 노하우 전수

    티스토리툴바