2016-09-22 4 views
0

내 화합 프로젝트에 Google vr SDK가 설치되어 있으며 카메라 이동과 완벽하게 작동합니다. 하지만 vr 방향을 사용하여 플레이어를 제어하려면 어떻게해야합니까? 예를 들어 머리를 왼쪽이나 오른쪽으로 돌리면 플레이어가 왼쪽이나 오른쪽으로 이동합니다. 이것은 내가 종종 카메라의 방향으로 플레이어를 이동하는 데 사용하는 스크립트입니다판지 방향으로 플레이어를 움직일 수 있습니까?

float moveHorizontal = Input.GetAxis("Horizontal"); 
float moveVertical = Input.GetAxis("Vertical"); 
Vector3 movement = new Vector3(moveHorizontal, moveVertical + motion_cont.getVertical(), 1f); 
rb.velocity = movement * motion_cont.getSpeed(); 
+0

어쩌면 당신이 뭔가를 감당할 수있는 [이] (https://developers.google.com/vr/unity/reference/class/gvr-head) – MadJlzz

답변

0

: 내 코드에서

은, 난 그냥 키보드로부터 입력을받을 것을 알고있다. 골판지 자석 버튼을 사용하여 걷기를 시작합니다. 원하는대로 변경할 수 있습니다.

// This script moves your player automatically in the direction he is looking at. You can 
// activate the autowalk function by pull the cardboard trigger, by define a threshold angle 
// or combine both by selecting both of these options. 
// The threshold is an value in degree between 0° and 90°. So for example the threshold is 
// 30°, the player will move when he is looking 31° down to the bottom and he will not move 
// when the player is looking 29° down to the bottom. This script can easally be configured 
// in the Unity Inspector.Attach this Script to your CardboardMain-GameObject. If you 
// haven't the Cardboard Unity SDK, download it from https://developers.google.com/cardboard/unity/download 

using UnityEngine; 
using System.Collections; 

public class Autowalk : MonoBehaviour 
{ 
    private const int RIGHT_ANGLE = 90; 

    // This variable determinates if the player will move or not 
    private bool isWalking = false; 

    CardboardHead head = null; 

    //This is the variable for the player speed 
    [Tooltip("With this speed the player will move.")] 
    public float speed; 

    [Tooltip("Activate this checkbox if the player shall move when the Cardboard trigger is pulled.")] 
    public bool walkWhenTriggered; 

    [Tooltip("Activate this checkbox if the player shall move when he looks below the threshold.")] 
    public bool walkWhenLookDown; 

    [Tooltip("This has to be an angle from 0° to 90°")] 
    public double thresholdAngle; 

    [Tooltip("Activate this Checkbox if you want to freeze the y-coordiante for the player. " + 
      "For example in the case of you have no collider attached to your CardboardMain-GameObject" + 
      "and you want to stay in a fixed level.")] 
    public bool freezeYPosition; 

    [Tooltip("This is the fixed y-coordinate.")] 
    public float yOffset; 

    void Start() 
    { 
     head = Camera.main.GetComponent<StereoController>().Head; 
    } 

    void Update() 
    { 
     // Walk when the Cardboard Trigger is used 
     if (walkWhenTriggered && !walkWhenLookDown && !isWalking && Cardboard.SDK.Triggered) 
     { 
      isWalking = true; 
     } 
     else if (walkWhenTriggered && !walkWhenLookDown && isWalking && Cardboard.SDK.Triggered) 
     { 
      isWalking = false; 
     } 

     // Walk when player looks below the threshold angle 
     if (walkWhenLookDown && !walkWhenTriggered && !isWalking && 
      head.transform.eulerAngles.x >= thresholdAngle && 
      head.transform.eulerAngles.x <= RIGHT_ANGLE) 
     { 
      isWalking = true; 
     } 
     else if (walkWhenLookDown && !walkWhenTriggered && isWalking && 
       (head.transform.eulerAngles.x <= thresholdAngle || 
       head.transform.eulerAngles.x >= RIGHT_ANGLE)) 
     { 
      isWalking = false; 
     } 

     // Walk when the Cardboard trigger is used and the player looks down below the threshold angle 
     if (walkWhenLookDown && walkWhenTriggered && !isWalking && 
      head.transform.eulerAngles.x >= thresholdAngle && 
      Cardboard.SDK.Triggered && 
      head.transform.eulerAngles.x <= RIGHT_ANGLE) 
     { 
      isWalking = true; 
     } 
     else if (walkWhenLookDown && walkWhenTriggered && isWalking && 
       head.transform.eulerAngles.x >= thresholdAngle && 
       (Cardboard.SDK.Triggered || 
       head.transform.eulerAngles.x >= RIGHT_ANGLE)) 
     { 
      isWalking = false; 
     } 

     if (isWalking) 
     { 
      Vector3 direction = new Vector3(head.transform.forward.x, 0, head.transform.forward.z).normalized * speed * Time.deltaTime; 
      Quaternion rotation = Quaternion.Euler(new Vector3(0, -transform.rotation.eulerAngles.y, 0)); 
      transform.Translate(rotation * direction); 
     } 

     if(freezeYPosition) 
     { 
      transform.position = new Vector3(transform.position.x, yOffset, transform.position.z); 
     } 
    } 
} 

출처 : JuppOtto/Google-Cardboard

관련 문제