유니티 캐릭터생성 및 위치지정
Unity/과제2019. 4. 15. 02:44
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 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class Character : MonoBehaviour { private Character target; Yangjaejoon.CharacterInfo info; Animation anim; public GameObject model { get; private set; } private bool isMoveStart = false; private bool isMoveComplete = false; private bool isAttack = false; private bool isDie = false; private int speed = 1; private Vector3 targetPosition; private float currentTime; //현재시간을 구하기위함 private int totalFrame = 22; private int AttackFrame = 8; private float attackLength = 0.733f; private void Awake() { this.anim = this.gameObject.GetComponent<Animation>(); } public void SetModel(GameObject model) { this.model = model; this.model.transform.SetParent(this.transform); //캐릭터프리팹 안에 모델프리팹 인스턴스를 넣음 } public void Init(Yangjaejoon.CharacterInfo info, Vector3 initPosition) { this.info = info; this.transform.position = initPosition; } public void Move(Character target) { var distance = Vector3.Distance(this.gameObject.transform.position, targetPosition); this.target = target; this.isMoveStart = true; var data = App.Instance.dicCharacterData[this.info.Id]; this.transform.LookAt(this.target.transform); this.anim.Play(data.run_anim_name); } public void Attack(Character target) { this.target = target; this.Move(target); this.isAttack = true; } private void Update() { this.currentTime += Time.deltaTime; //현재시간(누적) var distance = Vector3.Distance(this.gameObject.transform.position, targetPosition); if (this.isMoveStart == true) { var data = App.Instance.dicCharacterData[this.info.Id]; var dir = (this.target.transform.position - this.transform.position).normalized; this.transform.position += dir * this.speed * Time.deltaTime; if (data.attack_range > distance) { this.isAttack = true; this.anim.Play(data.attack_anim_name); this.transform.LookAt(target.transform); var fps = this.anim.GetClip(data.attack_anim_name).frameRate; var length = this.anim[data.attack_anim_name].length; var currentFrame = this.currentTime * totalFrame / length; if (this.currentTime >= 8) { Debug.Log("타격"); } if(this.currentTime >= this.anim[data.attack_anim_name].length) { this.currentTime = 0; this.isAttack = false; anim.Play(data.idle_anim_name); } } } if (distance <= 0.1f) { var data = App.Instance.dicCharacterData[this.info.Id]; Debug.Log("이동완료"); this.isMoveStart = false; isMoveComplete = true; this.anim.Play(data.idle_anim_name); } } } | 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 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class App : MonoBehaviour { public Dictionary<int, CharacterData> dicCharacterData; public static App Instance; Animation anim; public void Awake() { App.Instance = this; } // Start is called before the first frame update void Start() { this.dicCharacterData = new Dictionary<int, CharacterData>(); this.anim = gameObject.GetComponent<Animation>(); var heroData = new CharacterData(0, "양재준", "ch_01_01", 10, 1, 0.7f, "run@loop", "idle@loop", "attack_sword_01", "damage", "die"); var monsterData = new CharacterData(1, "박민지", "StoneMonster", 10, 2, 1.2f, "Anim_Run", "Anim_Idel", "Anim_Attack", "Anim_Damage", "Anim_Death"); this.dicCharacterData.Add(heroData.id, heroData); this.dicCharacterData.Add(monsterData.id, monsterData); //캐릭터생성 var hero = this.CreateCharacter(0); hero.Init(new Yangjaejoon.CharacterInfo(0), new Vector3(1, 0, 1)); var monster = this.CreateCharacter(1); monster.Init(new Yangjaejoon.CharacterInfo(1), new Vector3(5, 0, 1)); hero.Attack(monster); } //캐릭터 생성 메서드 private Character CreateCharacter(int id) { var characterData = dicCharacterData[id]; //매개변수 id를 받아서 값 복사 //캐릭터프리팹 로드 var characterPath = string.Format("Prefabs/character"); //(string.Format : 텍스트를 일련의 유니코드 문자로 나타냄) var characterPrefab = Resources.Load<GameObject>(characterPath); //Resources폴더에 있는 모든 에셋,Scene을 불러온다 (미리 Resources폴더를 만들어놓자) //캐릭터프리팹의 인스턴스화 var characterGo = GameObject.Instantiate<GameObject>(characterPrefab); //모델프리팹 로드 var modelPath = string.Format("Prefabs/{0}", characterData.prefab_name); var modelPrefab = Resources.Load<GameObject>(modelPath); //모델프리팹 인스턴스화 var modelGo = GameObject.Instantiate<GameObject>(modelPrefab); var character = characterGo.AddComponent<Character>(); character.SetModel(modelGo); characterGo.name = characterData.name; return character; } // Update is called once per frame void Update() { } } | 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 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class CharacterData { public int id; public string name; public string prefab_name; public int hp; public int damage; public float attack_range; public string run_anim_name; public string idle_anim_name; public string attack_anim_name; public string hit_anim_name; public string die_anim_name; public CharacterData(int id, string name, string prefab_name, int hp, int damage, float attack_range, string run_anim_name, string idle_anim_name, string attack_anim_name, string hit_anim_name, string die_anim_name) { this.id = id; this.name = name; this.prefab_name = prefab_name; this.hp = hp; this.damage = damage; this.attack_range = attack_range; this.run_anim_name = run_anim_name; this.idle_anim_name = idle_anim_name; this.attack_anim_name = attack_anim_name; this.hit_anim_name = hit_anim_name; this.die_anim_name = die_anim_name; } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | using System.Collections; using System.Collections.Generic; using UnityEngine; namespace Yangjaejoon { public class CharacterInfo { public int Id { get; private set; } public CharacterInfo(int id) { this.Id = id; } } } | cs |
'Unity > 과제' 카테고리의 다른 글
unity 지정된 몬스터 공격 및 대상 삭제 (0) | 2019.04.24 |
---|---|
타이틀씬까지 로드 (0) | 2019.04.18 |
FadIn,FadOut 코드 (0) | 2019.04.17 |
[Unity]코루틴 사용하여 캐릭터 이동 및 공격모션 (0) | 2019.04.17 |
유니티 게임시작화면관리하기 (0) | 2019.04.16 |