8/4 ~ 8/10 : Unity Development 2_Control Input





1. Control 6DOF (6 Degrees of Freedom)



- Unist Setup&Hello, Cube
- Frane Definition

- Setup the Scene
: As the Cube will be very long but very thin, we use an Empty Gameobject serving as the parent node of our Cube. In this way we will be able to handle our Cube more easily, using its edge as its position instead of its center.   

    => create empty Game object name Beam-> Beam 우클릭-> (3D object->Cube)

    => project창에서 Assets 우클릭-> Create-> Create Material name Selected-> 초록색으로 바꾸기-> Cube GameObject에 Selected를 Drag&Drop

- Control C# Script
    
    => Select Beam GameObject-> Add Component-> New Script name Control6DOF 
    => 다음 코드 작성하기

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


public class Control6DOF : MonoBehaviour
{
    #region Private Variables
    private MLInput.Controller _controller;   // 컨트롤러의 입력을 수신하는 변수
    #endregion


    #region Unity Methods
    // Start is called before the first frame update
    void Start()   // 수신 시작(컨트롤러를 잡았다)
    {
        //Start receiving input by the Control
        MLInput.Start();
        _controller = MLInput.GetController(MLInput.Hand.Left);
    }

    void OnDestroy()   // 컨트롤러의 입력 중단
    {
        //Stop receiving input by the Control
        MLInput.Stop();
    }

    // Update is called once per frame
    void Update()    // Beam Game Object를 컨트롤러에 붙인다.
    {
        //Attach the Beam GameObject to the Control
        transform.position = _controller.Position;
        transform.rotation = _controller.Orientation;
    }

    #endregion
}


그리고 ZI 시뮬레이터를 돌려보면 다음과 같이 리모컨 앞에 초록색 빔이 나가고 있음을 확인할 수 있다. 




2. Control Raycast


: depth 카메라는 ray를 쏴서 돌아오는 ray를 감지할 수 있음. 이 기능은 Raycast 명령을 통해 구현가능하며, 이번 강좌에서는 ray가 닿는 곳에 큐브를 생성하는 연습을 할 것.

=> Unity Setup
=> Scene Setup
    - Assets-> Create-> Raycast라는 이름의 C# Script
    - GameObject-> Raycast라는 이름의 Empty-> C# Script 넣기
    - GameObject-> 3D Object-> Cube-> (0,0,0) (0,0,0) (0.05,0.05,0.05)-> Assets로 드래그 후 삭제

=> Raycast Script Setup
    - Raycast 파일에 다음 코드 넣기

using System.Collections;
using UnityEngine;
using UnityEngine.XR.MagicLeap;

public class Raycast : MonoBehaviour {

    public Transform  ctransform; // Camera's transform
    public GameObject prefab;    // Cube prefab

    // Use this for initialization
    void Start() {
        MLRaycast.Start();
    }

    // Update is called once per frame
    void Update() {
        // Create a raycast parameters variable
        MLRaycast.QueryParams _raycastParams = new MLRaycast.QueryParams {
            // Update the parameters with our Camera's transform
            Position = ctransform.position,
            Direction = ctransform.forward,
            UpVector = ctransform.up,
            // Provide a size of our raycasting array (1x1)
            Width = 1,
            Height = 1
        };
        // Feed our modified raycast parameters and handler to our raycast request
        MLRaycast.Raycast(_raycastParams, HandleOnReceiveRaycast);
    }
    private void OnDestroy() {
        MLRaycast.Stop();
    }
    // Instantiate the prefab at the given point.
    // Rotate the prefab to match given normal.
    // Wait 2 seconds then destroy the prefab.
    private IEnumerator NormalMarker(Vector3 point, Vector3 normal) {
        Quaternion rotation = Quaternion.FromToRotation(Vector3.up, normal);
        GameObject go = Instantiate(prefab, point, rotation);
        yield return new WaitForSeconds(2);
        Destroy(go);
    }

    // Use a callback to know when to run the NormalMaker() coroutine.
    void HandleOnReceiveRaycast(MLRaycast.ResultState state, UnityEngine.Vector3 point, Vector3 normal, float confidence) {
        if (state == MLRaycast.ResultState.HitObserved) {
            StartCoroutine(NormalMarker(point, normal));
        }
    }

    // When the prefab is destroyed, stop MLWorldRays API from running.
}
    



=> Raycast의 Ctransform에 Main Camera(Transform)을, Prefab에 Cube를 집어넣고 저장한다. 

=> ZI 시뮬레이터로 실행시키면 원하는 결과를 확인할 수 있다. 



3. Control Buttons


: 컨트롤러의 버튼 조작하기


Trigger : Rotate(x)
Bumper: Rotate(y)
Home: Return to Origin state



4. Touchpad Gestures


: 컨트롤러의 터치패드 조작하기








댓글

이 블로그의 인기 게시물

0817 : Hand Tracking

0701_Using server in Unity