[4Idle-Gazzlers] 플레이어 왼손 구현 -2. 방패 부착

https://lj9369.tistory.com/127

 

Gazzler R&D - 왼손 방패 잡기 구현(LeftHandShield)

▷LeftHand - 방패(Shield)잡기 구현 ▼구현할 부분(왼손 방패) R&D할 부분 ▼구현할 것 - 왼 손을 쥐면 (LeftIndexTrigger를 누르면) 방패 생성 - 왼 손을 펴면 방패 사라짐 - LeftIndexTrigger를 누르는 동안 방패

lj9369.tistory.com

-팀원분이 구현하신 방패를 이전 포스팅에서 작성한 아이템 기능과 함께 구현되도록 수정하였다.

 

-HandLeft(Shield) 프리팹을 HandLeftModels의 자식으로 추가한다.

-LeftHandAnchor에 부착된 LeftHandController를 수정하여 방패를 추가하는 코드를 넣어준다.

-추가된 프리팹을 LeftHandController에 할당한다. 이 추가된 모델이 방어 상태일때 나타난다.

- 아이템을 그랩한 상태에서는 방패의 손모델이 생성되면 안되므로 코드를 수정해주었다.

- enum eState를 작성해 플레이어의 현재상태가 무엇인지 확인한다.

- GRAB일때에는 if문이 실행되지 않는다.

-캔버스를 월드로 바꾸고, 자식으로 ShieldGauge 프리팹을 가져와 넣어준다.

-프리팹에 있던 UI스크립트를 복사해 새로운 스크립트인 Shield UI 스크립트를 넣어주었다.

(=> 기존 스크립트가 아니라 LeftHandController에서 이벤트를 받아와야 하므로 변경해주었다.)

-ShieldGauge 프리팹은 손 위에 텍스트 UI를 프리팹화한것이다.

 

그랩할때 방패 생기지않고, 서로 따로 잘 동작하는 것 확인

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace hyw
{
    public class LeftHandController : MonoBehaviour
    {
        [SerializeField] GameObject originalLeftHandGo;
        [SerializeField] GameObject grabLeftHandGo;
        [SerializeField] Transform itemTrans;
        private GameObject grabGo;


        public enum eState
        {// 상태 정의
            IDLE, GRAB,DEFENSE,ATTACK
        }
        public eState state = eState.IDLE;//Player의 현재 상태


       // ------------------------------------------------LJE Shiled-------------------------------------

        [SerializeField] private GameObject leftHandShield;
        public System.Action getDown;
        public System.Action getUp;
        public System.Action<int> onChangeValue;
        private int shieldMaxValue = 100;
        private int shieldValue;
        private float elapsedTime;

        // Start is called before the first frame update
        void Start()
        {
            this.shieldValue = this.shieldMaxValue;
            this.onChangeValue(this.shieldValue);
        }

        // Update is called once per frame
        void Update()
        {
            

            if (this.grabGo != null)
            {
                Animator anim = this.grabGo.GetComponent<Animator>();
                GameObject go = this.grabGo.GetComponentInChildren<ParticleSystem>(true).gameObject;



                if (OVRInput.Get(OVRInput.Button.PrimaryHandTrigger) && this.grabGo != null)
                {
                    this.state = eState.GRAB;
                    this.originalLeftHandGo.SetActive(false);
                    this.grabLeftHandGo.SetActive(true);
                    anim.SetInteger("State", 1);
                    // GameObject go = this.grabGo.GetComponentInChildren<ParticleSystem>(true).gameObject;               
                    go.SetActive(true);

                    if (OVRInput.Get(OVRInput.Button.PrimaryIndexTrigger))
                    {//while grabbing, if pushed left index trigger
                        Debug.Log("left Hand Index Trigger");
                        this.grabGo.SetActive(false);
                        if (this.grabGo.CompareTag("Item"))
                        {
                            this.grabGo.transform.position = this.itemTrans.position;
                        }
                    }
                }
                else if (OVRInput.GetUp(OVRInput.Button.PrimaryHandTrigger))
                {
                    this.state = eState.IDLE;
                    this.originalLeftHandGo.SetActive(true);
                    this.grabLeftHandGo.SetActive(false);
                    anim.SetInteger("State", 0);

                    go.SetActive(false);
                    this.grabGo = null;
                }
            }

            // ------------------------------------------------LJE Shiled-------------------------------------
            if (OVRInput.Get(OVRInput.Button.PrimaryIndexTrigger) && this.state != eState.GRAB)
            {
                this.state = eState.DEFENSE;
                this.originalLeftHandGo.SetActive(false);
                this.leftHandShield.SetActive(true);
                this.getDown();
                this.elapsedTime += Time.deltaTime;
                if (this.elapsedTime >= 1)
                {
                    this.shieldValue--;
                    this.onChangeValue(this.shieldValue);
                    this.elapsedTime = 0f;
                }
            }
            else if (OVRInput.GetUp(OVRInput.Button.PrimaryIndexTrigger))
            {
                this.state = eState.IDLE;
                this.originalLeftHandGo.SetActive(true);
                this.leftHandShield.SetActive(false);
                this.elapsedTime = 0;

                this.onChangeValue(this.shieldValue);
                this.getUp();
            }

        }

        private void OnTriggerEnter(Collider other)
        {
            this.grabGo = other.gameObject;

        }

    

    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using System.Runtime.CompilerServices;

namespace hyw 
{
    public class ShieldUI : MonoBehaviour
    {
        [SerializeField] private TMP_Text shieldGauge;
        [SerializeField] private LeftHandController leftShield;


        void Start()
        {
            this.gameObject.SetActive(false);

            this.leftShield.getDown = () =>
            {
                this.gameObject.SetActive(true);
            };

            this.leftShield.getUp = () =>
            {
                this.gameObject.SetActive(false);
            };

            this.leftShield.onChangeValue = (value) =>
            {
                this.shieldGauge.text = string.Format("{0}%", value);
            };
        }

        // Update is called once per frame
        void Update()
        {
            this.transform.position = this.leftShield.transform.position + Vector3.right * 0.1f;
           
        }
    }
}
myoskin