C#/과제

2020.05.07. 과제 - Unity 무기 장착 (수정)

dev_sr 2020. 5. 7. 23:17

 

만드려는 캐릭터 prefab파일에 HeroModel컴포넌트를 추가한다

 

1. 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
36
37
38
39
40
41
42
using System.Collections;
using System.Collections.Generic;
using Unity.Collections;
using UnityEngine;
using UnityEngine.UI;
 
public class App : MonoBehaviour
{
 
    public Button btn;
    public GameObject heroPrefab;
    public GameObject weaponPrefab;
 
    private void Start()
    {
 
        this.btn.onClick.AddListener(() =>
        {
            var heroModelGo = this.CreateModel();
            var weaponGo = this.CreateWeapon();
            var dummyRHand = heroModelGo.GetComponent<HeroModel>().dummyRHand;
            //true - 부모요소와 상대적으로 위치를 정함
            //false - 독립적으로 위치를 정함
            weaponGo.transform.SetParent(dummyRHand, false);
        });
    }
 
    private GameObject CreateModel()
    {
        return Instantiate(this.heroPrefab);
    }
 
    private GameObject CreateWeapon()
    {
        return Instantiate(this.weaponPrefab);
 
    }
 
 
}
 
 

 

2. HeroModel

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class HeroModel : MonoBehaviour
{
    public Transform dummyRHand;
 
    // Start is called before the first frame update
    void Start()
    {
 
    }
 
    // Update is called once per frame
    void Update()
    {
 
    }
}
cs

 

 

 

-----------------------------------------------------------------------------------------------------------------

캐릭터 모델 객체 구성인 Bip01의 하위 요소인 DummyRHand 에 

무기를 드래그하여 밑에 놓으면 무기를 가진 캐릭터처럼 행동하는 것은 알 수 있었지만

코드적으로 Bip01 이나 DummyRHand 에 접근하는 방법을 찾지못해서  

Hero 클래스에서 무기를 만들고 실체화 하는 것 까지 진행했습니다.

 

1. 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
36
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class App : MonoBehaviour
{
    public Hero hero;
    public GameObject[] arrHero;
    public string heroName;
    
   
    void Start()
    {
        GameObject newHero = this.CreateNewHero(this.heroName);
        hero.Init(newHero);
        
    }
 
    GameObject CreateNewHero(string name)
    {
        GameObject foundObject = null;
 
        foreach(GameObject data in this.arrHero)
        {
            if(data.name==name)
            {
                foundObject = data.gameObject;        
            }
        }
 
        return Instantiate(foundObject);
    }
 
 
}
 
 

 

2. 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
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEditor.SceneManagement;
using UnityEngine;
 
public class Hero : MonoBehaviour
{
    public GameObject model;
    public GameObject weapon;
 
 
    public void Init(GameObject model)
    {
        this.model = model;
        this.model.transform.SetParent(this.gameObject.transform);
        this.EquipWeapon(this.weapon);
    }
    
    public void EquipWeapon(GameObject weapon)
    {
        
        var newWeapon = Instantiate(weapon);
        newWeapon.transform.position = new Vector3(0.218f, 0.04f, -0.014f);
        newWeapon.transform.SetParent(this.model.transform);
        
    }
 
}
 
 

 

 

3.