among-us: 캐릭터만들기: 조이스틱
JoyStick
- Character 자식으로 UI-Image(JoyStick)만들고, 그 자식으로 UI-Image(Stick)를 만듦
- Soucr-Image에 sprite를 넣어줌
- Canvas셋팅 • Scale With Screen Size • 1920, 1080 • Match = 1
- Joystick 셋팅 • PosX = 200, PosY = 200, Width=200, Height= 200 • Pivot위치: left-bottom • 투명도 50%
-
Stick • Witdh = 100, Height= 100
- JoyStick 스크립트 생성 ```c# using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Joystick : MonoBehaviour {
//1. Stick드래그 , 제한 //2. 드래그만큼 캐릭터 이동 // 스틱 누르면 호출 public RectTransform stick; bool isDrag; public void ClickStick() { isDrag = true; } void Start() { } // Update is called once per frame void Update() { if (isDrag) { stick.position = Input.mousePosition; } } } ``` 7. Joystic 스크립트를 Charcter에 넣기 8. JoyStic Image에 eventtrigger 만들고, pintdown선택하기 9. Character를 드래그하고 Joystick의 ClickStick()선택 10. 조이스틱의 드래그 값을 제한하기 ```c# using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Joystick : MonoBehaviour {
//1. Stick드래그 , 제한 //2. 드래그만큼 캐릭터 이동 // 스틱 누르면 호출 public RectTransform stick, background; bool isDrag; float limit; public void ClickStick() { isDrag = true; } void Start() { limit = background.rect.width * 0.5f; } // Update is called once per frame void Update() { //드래그하는동안 if (isDrag) { //마우스의 위치로부터 background중앙까지의 거리 계산 Vector2 vec = Input.mousePosition - background.position; //vector2의 값을 제한(어떤값을, 얼마만큼 제한할지) stick.localPosition = Vector2.ClampMagnitude(vec, limit); //마우스를 뗐을때는 부모의 위치에서 최초위치로 원복 //드래그 끝나면 if (Input.GetMouseButtonUp(0)) { stick.localPosition = new Vector3(0, 0, 0); isDrag = false; } } } } ```
- 드래그한 만큼 캐릭터가 이동하기 -Joystick- ```c# using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Joystick : MonoBehaviour {
//1. Stick드래그 , 제한
//2. 드래그만큼 캐릭터 이동
// 다른 스크립트 불러오기
PlyayerControler plyayerControler_script;
// 스틱 누르면 호출
public RectTransform stick, background;
bool isDrag;
float limit;
public void ClickStick()
{
isDrag = true;
}
void Start()
{
plyayerControler_script = GetComponent<PlyayerControler>();
limit = background.rect.width * 0.5f;
}
void Update()
{
//드래그 하는 동안
if (isDrag)
{
//방향벡터(스틱의위치-현재포지션값 빼기)
Vector3 dir = (stick.position - background.position).normalized;
//Player의 위치는 조이스틱의 방향*스피드*델타타임
transform.position += dir * plyayerControler_script.speed * Time.deltaTime;
//마우스의 위치로부터 background중앙까지의 거리 계산
Vector2 vec = Input.mousePosition - background.position;
//vector2의 값을 제한(어떤값을, 얼마만큼 제한할지)
stick.localPosition = Vector2.ClampMagnitude(vec, limit);
//드래그 끝나면
//마우스를 뗐을때는 부모의 위치에서 최초위치로 원복
if (Input.GetMouseButtonUp(0))
{
stick.localPosition = new Vector3(0, 0, 0);
isDrag = false;
}
}
} } ```
12. 조이스틱과 터치를 구분하기
-PlayerCtrl-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlyayerControler : MonoBehaviour
{
public bool isJoyStick;
public float speed;
public GameObject joystick;
private void Start()
{
//카메라가 Player의 자식으로 들어감
Camera.main.transform.parent = transform;
//카메라가 자식들어갈때 위치지정
Camera.main.transform.localPosition = new Vector3(0, 0, -10);
}
private void Update()
{
Move();
}
void Move()
{
//조이스틱 이동일때
if (isJoyStick)
{
joystick.SetActive(true);
}
//터치 이동일때
else
{
joystick.SetActive(false);
if (Input.GetMouseButton(0))
{
//스크립트 상에서 나누기보다 곱하기가 계산속도가 빠름
//전체화면의 0.5를 곱하면 중앙이 되며, 이를 마우스 지점으로 지정하고,
정규화해주면 방향을 얻는 벡터가 됨
//클릭했는지 판단(마우스, 터치)
Vector3 dir = (Input.mousePosition - new Vector3(Screen.width * 0.5f,
Screen.height * 0.5f)).normalized;
transform.position += dir * speed * Time.deltaTime;
}
}
}
}
댓글남기기