반응형
SMALL
using UnityEngine;
using UnityEngine.EventSystems;
public class SpcialWeapon : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler
{
public GameObject gun;
public RectTransform lever;
private RectTransform rectTransform;
[SerializeField, Range(10f, 150f)]
private float leverRange;
private Vector2 inputVector;
public void Start()
{
this.rectTransform = GetComponent<RectTransform>();
}
void Update()
{
if (Input.GetMouseButton(0))
{
this.GunRotate(inputVector);
}
if (this.inputVector.x < 0)
{
this.gun.GetComponent<SpriteRenderer>().flipY = true;
}
else this.gun.GetComponent<SpriteRenderer>().flipY = false;
}
public void OnBeginDrag(PointerEventData eventData)
{
this.ControlJoystickLever(eventData);
}
public void OnDrag(PointerEventData eventData)
{
this.ControlJoystickLever(eventData);
}
public void OnEndDrag(PointerEventData eventData)
{
this.lever.anchoredPosition = Vector2.zero;
}
public void ControlJoystickLever(PointerEventData eventData)
{
Vector2 inputDir = eventData.position - rectTransform.anchoredPosition;
Vector2 clampedDir = inputDir.magnitude < leverRange ? inputDir
: inputDir.normalized * leverRange;
this.lever.anchoredPosition = clampedDir;
this.inputVector = clampedDir;
}
public void GunRotate(Vector2 dir)
{
this.gun.transform.right = dir;
}
}
- 아날로그 패드의 방향 벡터값을 정규화된 transform.right에 할당하는 방식으로 구현했다.
- 아날로그 패드의 방향 벡터 x값의 범위를 이용해 총 스프라이트의 x축을 기준으로 플립 시키니 좀 자연스러운 그림이 나왔다.
반응형
LIST
'프로젝트 > 건즈앤 레이첼스' 카테고리의 다른 글
| [유니티] 게임 진입 구조잡기 - as연산자, 비동기식 데이터 로드 (0) | 2023.02.27 |
|---|---|
| [유니티] 대쉬 (회피) 기능 고민 (1) | 2023.02.26 |
| [유니티] 2d 스프라이트 회전 (스크린 터치 회전 시키기) (0) | 2023.02.26 |
| [유니티] 캐릭터 카메라 제어 : Update() 와 FixedUpdate() 그리고 LateUpdate() (0) | 2023.02.26 |
| [유니티] 2D 아날로그 터치패드 구현 & 애니메이션 (0) | 2023.02.26 |