-
2020-05-12 [unity]카테고리 없음 2020. 5. 12. 18:44
각각 객체 생성, 서로 접근. 근접하면 공격까지 진행
뒤에 펫 생겼으나 같이 이동하지 않고 있음.
// 오늘 캐릭터를 중심으로 오브젝트(구체)를 회전 시키는 것도 했음
오늘 생각한거는 해당 구체를 회전 시킨다는 생각을 하고있었으나,
GameObject를 돌리면 해당 구체가 돌아감.
이동할때는 캐릭터포지션 = 구체오브젝트포지션 을 하면 같이 이동하는 듯이 됨.
구조? 좀 더 생각해야 할듯 싶음.
APP>>
더보기1234567891011121314151617181920212223242526272829303132333435using 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 updatevoid 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 framevoid Update(){}}cs InGame >
더보기123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143using 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 updatevoid Start(){Debug.Log("InGame : Start");}//InGame 씬이 로드되어 있을 경우 호출//AwaKe -Init - Startpublic 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(2, 0, 6));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 framevoid Update(){}}cs Charater >
더보기1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253using 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 updatevoid 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 framevoid Update(){}}cs Hero>
더보기123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127using 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 updatevoid 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 framevoid 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>
더보기1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980using 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 updatevoid 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 framevoid 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