본문 바로가기
Unity

[유니티] 조이패드

by Skull Crusher 2021. 5. 18.
728x90

1. 추적 카메라 넣기

C# Script

Main Camera -> Target에 Player 연결

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

public class CameraFollow : MonoBehaviour
{
    // 추적 타겟
    public Transform target;

    // 부드러움 수치 (보간수치)
    public float smoothing = 5f;

    // 추적 간격(거리) 오프셋
    Vector3 offset; 

    // Start is called before the first frame update
    void Start()
    {
        offset = transform.position - target.position;
    }

    // LateUpdate(): 모든 Update가 호출된 다음에 실행되는 이벤트 메소드
    // * 캐릭터의 이동처리가 완료된(Update의 처리 후가 보장된) 후 실행되는 이벤트 Update메소드
    void LateUpdate()
    {
        //갱신된 추적 대상(캐릭터)의 위치에 오프셋값을 더함
        Vector3 targetCamPos = target.position + offset;

        //카메라의 위치를 변경함
        transform.position = Vector3.Lerp(transform.position, targetCamPos, smoothing + Time.deltaTime);
    }
}

 

2. 조이스틱

Asset Store -> Joystick Pack 다운받기

UI -> Canvas 추가 -> Fixed Joystick 하위에 추가

C# Script

Player에 조이스틱 연결

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

public class JpypadMovement : MonoBehaviour
{
    //애니메이터 참조
    public Animator animator;

    //캐릭터 컨트롤러 참조
    public CharacterController cc;

    //이동속도
    public float speed;

    //FixedJoyStick 컴포넌트 참조
    public FixedJoystick joystick;

    // Update is called once per frame
    void Update()
    {
        //키보드 또는 조이패드의 방향값을 구함
        //float h = Input.GetAxis("Horizontal"); //수평
        //float v = Input.GetAxis("Vertical"); //수직

        float h = joystick.Horizontal;// 조이스틱 수평
        float v = joystick.Vertical;// 조이스틱 수직

        //캐릭터 이동 방향 벡터
        Vector3 direction = new Vector3(h, 0f, v).normalized;

        //이동할 경우 이동 애니메이션 수행
        animator.SetFloat("speed", direction.magnitude);

        if (direction != Vector3.zero)
        {
            //이동 방향으로 캐릭터의 방향을 설정
            transform.forward = direction;

            //이동할 방향에 속도를 적용함
            direction *= speed;
        }

        //캐릭터 컨트롤러를 이용해 지정한 키방향으로 캐릭터를 이동시킴
        cc.SimpleMove(direction);
    }
}

 

3. 경사로/계단 만들기

3D -> Cube

 

4.

 

5. 

 

 

 

 

'Unity' 카테고리의 다른 글

[유니티]UI (드롭박스)  (0) 2021.05.20
[유니티] UI/UX Samples  (0) 2021.05.18
[유니티] 조명설정하기  (0) 2021.05.14
[유니티] 회전하기  (0) 2021.05.14
[유니티] 3D 캐릭터 이동하기  (0) 2021.05.13

댓글