본문 바로가기
[ CODING STUDY ]/》유니티 실습

유니티 crosshair

by MRG 2024. 1. 18.
728x90
반응형

 

▣ 먼저 UI에 Panel를 생성합니다. 

Panel Component에 Image는 삭제하고 Anchors로  Midlle center로 정렬합니다.

그리고 크기 Width, Height는 100으로 지정합니다.

 

 

 

▣ UI Image를 Panel안에 상속하여 생성하고 각각 위치에 맞게 정렬한 다음에 크기를 지정합니다. 

 

 

▣ 그리고 Panel 오브젝트에 스크립트를 추가합니다. 

 

▣ RectTransfrom crossHair; 를 통해서 Panel에 크기를 조절하기 위함입니다.

 

▣ WeaponManager코드는 총을 쏘는 스크립트를 가져오기 위함입니다. 

 

▣ 총을 쏘면 Mathf.Lerp 함수를 이용해서 서서히 크기를 조절합니다.

 

https://docs.unity3d.com/kr/530/ScriptReference/Mathf.Lerp.html

 

Mathf-Lerp - Unity 스크립팅 API

Linearly interpolates between a and b by t.

docs.unity3d.com

 

▣ sizeDelta는

앵커 사이의 거리를 기준으로 한 이 RectTransform의 크기입니다.

앵커가 함께 있으면 sizeDelta는 크기와 동일합니다. 앵커가 부모의 네 모서리 각각에 있는 경우 sizeDelta는 부모와 비교하여 사각형이 얼마나 크거나 작은 지를 나타냅니다.

 

https://docs.unity3d.com/ScriptReference/RectTransform-sizeDelta.html

 

Unity - Scripting API: RectTransform.sizeDelta

If the anchors are together, sizeDelta is the same as size. If the anchors are in each of the four corners of the parent, the sizeDelta is how much bigger or smaller the rectangle is compared to its parent.

docs.unity3d.com

 

 

▣ 총을 쏘는 스크립트에서는 총을 쏘는 상태 변수, 최대크기에 대한 변수를 생성하여 사용하고

총을 쏘게 되면 크로스헤어 최대크기를 늘려가는 방식입니다. 

 

 

728x90
반응형

댓글