미디어 콘텐츠 스터디

Part13. 점프(Jump) 본문

가상현실(Virtual Reality)/가상현실 기초 다루기

Part13. 점프(Jump)

danmujicat 2022. 11. 10. 00:20

오른쪽 컨트롤러의 secondaryButton 버튼 누르면 점프, 왼쪽 컨트롤러의 touch pad 버튼으로 이동/회전

 

1. 하이러키창에 GameObject ->3D Object-> Plane 메뉴를 선택하여 Plane을 추가하고 이름을 Ground으로 변경하고 머티리얼 설정한다.

2. 하이러키창에  GameObject ->3D Object->  Cube 메뉴를 선택하여 Cube 추가하고, Position(0, 0.5, 3), Rotate(0, 45, 0), 머티리얼 설정한다.

3. 하이러키창에  GameObject ->XR->  Device based->Locomotion System 메뉴를 선택하여 Locomotion System 추가하고 XR Origin 속성에 하이러키 창의 XR Origin을 설정한다.

 -  인스펙터 창에서 Teleportation Porvider와 Snap Turen Provider를 비활성화/삭제한다.

-  인스펙터 창에서 [Add Component]버튼을 클릭하여 Continuous Move Provider(Device based) 컴포넌트 추가하고, System 속성에 하이러키창의 Locomotion System을 설정, Controller 속성에 Elemenet 0에 XR Origin하위의 LeftHand Controller를 설정한다.

 인스펙터 창에서 [Add Component]버튼을 클릭하여 Continuous Turn Provider(Device based) 컴포넌트 추가하고, System 속성에 하이러키창의 Locomotion System을 설정, Controller 속성에 Elemenet 0에 XR Origin하위의 LeftHand Controller를 설정한다.

4.하이러키 창에서 XR Orign를 선택한 후 인스펙터창에  [Add Component]버튼을 클릭하여 Capsule Collider 추가한다.

- 인스펙터 창에서 Capsule Collider 컴포넌트의 Center 속성은 (0,1,0), Radius 속성은 0.5, Height 속성은 2, Direction 속성은 Y-Axis로 설정한다.

5. 하이러키 창에서 XR Orign를 선택한 후 인스펙터창에  [Add Component]버튼을 클릭하여  Rigidbody추가한다.

-인스펙터 창에서 Rigidbody 컴포넌트의 Constrains속성의 Freeze Rotation x,y, z 체크 박스 모두 선택한다.

6. 하이러키창에서 XR Orign선택한 후 인스펙터 창에 [Add Component]버튼을 클릭하여 JumController 스크립트 추가한다:

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

public class JumpController : MonoBehaviour
{
    
    private float jumpForce = 10.0f;

    private Rigidbody _body;
     public XRNode inputSource;

    // Start is called before the first frame update
    void Start()
    {
        _body = GetComponent<Rigidbody>();
       

    }

    void Update()
    {

        InputDevice device = InputDevices.GetDeviceAtXRNode(inputSource);

        if (device.TryGetFeatureValue(CommonUsages.secondaryButton, out bool primaryButtonValue2) && primaryButtonValue2)
        {
            if (!Physics.Raycast(
            new Vector2(transform.position.magnitude, transform.position.y + 3.0f),
            Vector3.down,
            10.0f
             )) return;
            _body.AddForce(Vector3.up * jumpForce);
        }              
       
    }
}

- 인스펙터 창에서  JumpController 스크립트의Jump Force 속성은 10, Input Source 속성은 Right Hand를 설정한다.

 

실행하기

Comments