Search Unity

Spherical Worlds Character Controller in VR

Discussion in 'Scripting' started by BearSheriff, May 4, 2016.

  1. BearSheriff

    BearSheriff

    Joined:
    May 14, 2015
    Posts:
    20
    Hey Guys! I am trying to adapt this tutorial for the Oculus DK2, but I keep running into a problem with the character controller. The object doesn't rotate with the Oculus currently, really messing up the movement. And with my current code the player starts freaking out! I changed the first few lines of update, so I turned transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * mouseSensitivityX) to InputTracking.GetLocalRotation(VRNode.Head).eulerAngles.x * mouseSensitivityX);

    Tutorial:
    (
    )

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.VR;
    4.  
    5. [RequireComponent(typeof(GravityBody))]
    6. public class FirstPersonController : MonoBehaviour
    7. {
    8.  
    9.     // public vars
    10.     public float mouseSensitivityX = 1;
    11.     public float mouseSensitivityY = 1;
    12.     public float walkSpeed = 6;
    13.     public float jumpForce = 220;
    14.     public LayerMask groundedMask;
    15.  
    16.     // System vars
    17.     bool grounded;
    18.     Vector3 moveAmount;
    19.     Vector3 smoothMoveVelocity;
    20.     float verticalLookRotation;
    21.     Transform cameraTransform;
    22.     Rigidbody rigidbody;
    23.  
    24.  
    25.     void Awake()
    26.     {
    27.         Cursor.lockState = CursorLockMode.Locked;
    28.         Cursor.visible = false;
    29.         cameraTransform = Camera.main.transform;
    30.         rigidbody = GetComponent<Rigidbody>();
    31.     }
    32.  
    33.     void Update()
    34.     {
    35.  
    36.         // Look rotation:
    37.         //  transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * mouseSensitivityX);
    38.         transform.Rotate(Vector3.up * InputTracking.GetLocalRotation(VRNode.Head).eulerAngles.x * mouseSensitivityX);
    39.         //verticalLookRotation += Input.GetAxis("Mouse Y") * mouseSensitivityY;
    40.  
    41.         verticalLookRotation += InputTracking.GetLocalRotation(VRNode.Head).y;
    42.         //verticalLookRotation = Mathf.Clamp(verticalLookRotation, -60, 60);
    43.         cameraTransform.localEulerAngles = Vector3.left * verticalLookRotation;
    44.  
    45.         // Calculate movement:
    46.         //float inputX = Input.GetAxisRaw("Horizontal");
    47.         //float inputY = Input.GetAxisRaw("Vertical");
    48.  
    49.         float inputX = Input.GetAxisRaw("Horizontal");
    50.         float inputY = Input.GetAxisRaw("Vertical");
    51.  
    52.         Vector3 moveDir = new Vector3(inputX, 0, inputY).normalized;
    53.         Vector3 targetMoveAmount = moveDir * walkSpeed;
    54.         moveAmount = Vector3.SmoothDamp(moveAmount, targetMoveAmount, ref smoothMoveVelocity, .15f);
    55.  
    56.         // Jump
    57.         if (Input.GetButtonDown("Jump"))
    58.         {
    59.             if (grounded)
    60.             {
    61.                 rigidbody.AddForce(transform.up * jumpForce);
    62.             }
    63.         }
    64.  
    65.         // Grounded check
    66.         Ray ray = new Ray(transform.position, -transform.up);
    67.         RaycastHit hit;
    68.  
    69.         if (Physics.Raycast(ray, out hit, 1 + .1f, groundedMask))
    70.         {
    71.             grounded = true;
    72.         }
    73.         else {
    74.             grounded = false;
    75.         }
    76.  
    77.     }
    78.  
    79.     void FixedUpdate()
    80.     {
    81.         // Apply movement to rigidbody
    82.         Vector3 localMove = transform.TransformDirection(moveAmount) * Time.fixedDeltaTime;
    83.         rigidbody.MovePosition(rigidbody.position + localMove);
    84.     }
    85. }