카테고리 없음

2020-05-12 [unity]

NexTin 2020. 5. 12. 18:44

각각 객체 생성, 서로 접근. 근접하면 공격까지 진행 

 

뒤에 펫 생겼으나 같이 이동하지 않고 있음.

 

//  오늘 캐릭터를 중심으로 오브젝트(구체)를 회전 시키는 것도 했음 

오늘 생각한거는 해당 구체를 회전 시킨다는 생각을 하고있었으나, 

GameObject를 돌리면 해당 구체가 돌아감.

 

이동할때는 캐릭터포지션 = 구체오브젝트포지션 을 하면 같이 이동하는 듯이 됨. 

구조? 좀 더 생각해야 할듯 싶음. 

 

 

 

 

APP>>

더보기
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
 
public class APP : MonoBehaviour
{
    private void Awake()
    {
        DontDestroyOnLoad(this.gameObject);
    }
    // Start is called before the first frame update
    void Start()
    {
        SceneManager.sceneLoaded += SceneManager_sceneLoaded;
        //씬로드
        SceneManager.LoadScene("InGame");
        
    }
 
    private void SceneManager_sceneLoaded(Scene arg0, LoadSceneMode arg1)
    {
        //타입으로 객체 찾기
        InGame inGame = GameObject.FindObjectOfType<InGame>();
        Debug.LogFormat("InGame : {0}", inGame);
        inGame.Init();
    }
 
    // Update is called once per frame
    void Update()
    {
        
    }
}
 
cs

 

InGame > 

더보기
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System.Collections;
using System.Collections.Generic;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
 
public class InGame : MonoBehaviour
{
    public Button button;
 
    private List<Character> characterList;
    private Pet pet;
 
 
    private void Awake()
    {
        Debug.Log("InGame : Awake");
        this.characterList = new List<Character>();
 
        
    }
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("InGame : Start");
    }
 
    //InGame 씬이 로드되어 있을 경우 호출 
    //AwaKe -Init - Start 
 
    public void Init()
    {
        Debug.Log("InGame : Init");
 
        //껍데기 생성 
        var shellGO = this.CreateShell("Hero");
        var modelGO = this.CreateModel("Prefabs/ch_04_01");
        Hero hero = this.CreateHero(shellGO, modelGO);
 
        var monsterShellGO = this.CreateShell("Monster");
        var monsterModelGO = this.CreateModel("Prefabs/ch_02_01");
        Monster monster = this.CreateMonster(monsterShellGO, monsterModelGO);
 
        var petShellGO = this.CreateShell("Pet");
        var petModelGO = this.CreateModel("Prefabs/Toon Chicken");
        this.pet = this.CreatePet(petShellGO, petModelGO);
 
        hero.Init(Vector3.zero);
        monster.Init(new Vector3(206));
        this.pet.Init(new Vector3(-0.5f, 0-0.5f));
 
        //hero.transform.LookAt(monster.transform.position);
        //monster.transform.LookAt(hero.transform.position);
 
        hero.transform.LookAt(monster.transform);
        monster.transform.LookAt(hero.transform);
        this.pet.transform.LookAt(hero.transform);
 
        this.characterList.Add(hero);
        this.characterList.Add(monster);
 
        
 
       // this.button.onClick.AddListener(() => { });
        this.button.onClick.AddListener(ButtonClicked);
 
    }
 
    private void ButtonClicked()
    {
        Debug.LogFormat("list : {0}"this.characterList.Count);
        foreach(var character in this.characterList)
        {
            character.Attack();
            pet.Move();
        }
 
        //this.characterList.ForEach(x => x.Attack());
    }
 
    private Pet CreatePet(GameObject shellGo, GameObject petModelGo)
    {
        var pet = shellGo.AddComponent<Pet>();
        pet.tag = "Pet";
        petModelGo.transform.SetParent(shellGo.transform, false);
 
        return pet;
 
    }
 
    private Monster CreateMonster(GameObject shellGO, GameObject monsterModelGO)
    {
        var monster = shellGO.AddComponent<Monster>();
        //monster.gameObject.tag = "Monster";
        monster.tag = "Monster";
        monsterModelGO.transform.SetParent(shellGO.transform, false);
        
        return monster;
    }
 
 
    private Hero CreateHero(GameObject shellGO, GameObject modelGO)
    {
        //껍데기에 Hero 컴포넌트 부착 
        var hero = shellGO.AddComponent<Hero>();
        //hero.gameObject.tag = "Hero";
        hero.tag = "Hero";
        //모델을 껍데기에 넣기 
        modelGO.transform.SetParent(shellGO.transform, false);
        //Hero 컴포넌트 반환 
        return hero;
    }
 
    private GameObject CreateModel(string path)
    {
        //프리펍로드 
       var profab = Resources.Load(path) as GameObject;
        // 복사본 생성 (게임 오브젝트 생성)
        var model = Instantiate<GameObject>(profab);
        //생성된 복사본 프리팹을 반환 
        return model;
    }
 
    private GameObject CreateShell(string name)
    {
        // 동적으로 게임오브젝트 생성 
        var shellGo = new GameObject();
        //게임오브젝트의 이름을 설정 
        shellGo.name = name;
        return shellGo;
    }
 
 
 
 
    // Update is called once per frame
    void Update()
    {
        
    }
}
 
cs

 

 

Charater > 

더보기
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Character : MonoBehaviour
{
    public float range;
    
    private void Awake()
    {
 
        Debug.Log("Character : Awake");
    }
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("Character : Start");
    }
 
    public void Init(Vector3 position)
    {
        Debug.Log("Character : init");
        // this.gameObject.transform.position()
 
       
        this.transform.position = position;
    }
 
    public virtual void Attack()
    {
 
 
 
    }
 
    public virtual void Move()
    {
 
 
    }
 
    public virtual void Stop()
    {
 
    }
 
    // Update is called once per frame
    void Update()
    {
        
    }
}
 
cs

 

 

Hero>

더보기
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System.Collections;
using System.Collections.Generic;
using TMPro;
using Unity.Collections.LowLevel.Unsafe;
using UnityEditor;
using UnityEditor.Rendering;
using UnityEngine;
 
public class Hero : Character
{
    private Animation anim;
    private GameObject target;
    private Pet pet;
    private bool isMove;
    private bool isAttack;
 
    private float elapsedTimeAttack;
    private AnimationState attackanimState;
 
 
    private void Awake()
    {
        Debug.Log(" Hero : Awake");
    }
    // Start is called before the first frame update
    void Start()
    {
        this.anim = this.transform.GetChild(0).GetComponent<Animation>();
        this.range = 1f;
        this.attackanimState = this.anim["attack_sword_01"];
        Debug.Log(" Hero : Start");
    }
 
    public override void Attack()
    {
        //게임 오브젝트의 태그로 찾는다. 
        //var target = GameObject.FindWithTag("Monster");
        // Debug.LogFormat("target : {0}", target);
 
        //컴포넌트의 타입으로 찾는다.
        //var taget = GameObject.FindObjectOfType<Monster>();
        // Debug.LogFormat("target : {0}", target);
 
        //게임 오브젝트의 이름으로 찾는다,
        target = GameObject.Find("Monster");
        Debug.LogFormat("target : {0}", target);
 
        this.Move(target);
 
 
 
 
    }
 
    public void Move(GameObject target)
    {
        this.target = target;
        this.isMove = true;
        this.anim.Play("run@loop");
    }
 
    public override void Stop()
    {
        this.anim.Play("idle@loop");
    }
 
    // Update is called once per frame
    void Update()
    {
 
       
        //if (isAttack)
        //{
 
        //    this.elapsedTimeAttack += Time.deltaTime;
 
        //    var dis = Vector3.Distance(this.target.transform.position, this.transform.position);
 
        //    if (this.elapsedTimeAttack >= this.attackanimState.length)
        //    {
        //        if (dis <= range)
        //        {
        //            this.isMove = false;
 
        //            this.elapsedTimeAttack = 0;
        //            this.isAttack = false;
        //            this.anim.Play("attack_sword_01");
        //        }
        //    }
        //}
 
        if (isMove)
        {
            
            var speed = 1;
            var dir = Vector3.forward;
            this.transform.Translate(speed * dir * Time.deltaTime);
 
            var dis = Vector3.Distance(this.target.transform.position, this.transform.position);
 
            target = GameObject.Find("Monster");
             
 
            this.elapsedTimeAttack += Time.deltaTime;
 
            if (dis <= range)
            {
                if (this.elapsedTimeAttack >= this.attackanimState.length)
                {
                    this.isMove = false;
                    this.elapsedTimeAttack = 0;
                    this.isAttack = false;
                    this.anim["attack_sword_01"].wrapMode = WrapMode.Loop;
                    this.anim.Play("attack_sword_01");
                   
 
                }
            }
 
      
 
        }
 
 
    }
}
 
cs

 

 

Moster>

더보기
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
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
 
public class Monster : Character
{
    private Animation anim;
    private GameObject target;
    private bool isMove;
    private bool isAttack;
 
    private float elapsedTimeAttack;
    private AnimationState attackanimState;
 
    private void Awake()
    {
        Debug.Log(" Monster : Awake");
    }
    // Start is called before the first frame update
    void Start()
    {
        this.anim = this.transform.GetChild(0).GetComponent<Animation>();
        this.range = 1;
        this.attackanimState = this.anim["attack_sword_02"];
        Debug.Log(" Monster : Start");
    }
 
    public override void Attack()
    {
        this.target = GameObject.FindWithTag("Hero");
        //Debug.LogFormat("target : {0}", target);
 
        this.Move(target);
    }
 
    public void Move(GameObject target)
    {
        this.target = target;
        this.isMove = true;
        this.anim.Play("run@loop");
    }
 
    public override void Stop()
    {
        this.anim.Play("idle@loop");
    }
 
    // Update is called once per frame
    void Update()
    {
        if (isMove)
        {
            var speed = 1;
            var dir = Vector3.forward;
            this.transform.Translate(speed * dir * Time.deltaTime);
 
            var dis = Vector3.Distance(this.target.transform.position, this.transform.position);
 
            this.elapsedTimeAttack += Time.deltaTime;
 
            if (dis <= range)
            {
                if (this.elapsedTimeAttack >= this.attackanimState.length)
                {
                    this.isMove = false;
                    this.elapsedTimeAttack = 0;
                    this.isAttack = false;
                    this.anim["attack_sword_02"].wrapMode = WrapMode.Loop;
                    this.anim.Play("attack_sword_02");
 
                }
            }
 
 
        }
 
    }
}
 
cs