Skip to content

Instantly share code, notes, and snippets.

@MrChrisHammond
Created August 29, 2017 01:31
Show Gist options
  • Save MrChrisHammond/f4f6025b665833771d5cea1ad0c3e375 to your computer and use it in GitHub Desktop.
Save MrChrisHammond/f4f6025b665833771d5cea1ad0c3e375 to your computer and use it in GitHub Desktop.
A simple Android VR Player Controller for Handheld Joystick Input.
/*************************************************************************
* SimpleVRPlayerController created by Chris @ ChrisHammond.ca
* __________________
*
* Usage: Please feel free to use this code for your own personal or commercial
* projects if it helps or is any benefit to you.
*
*
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.VR;
public class SimpleVRPlayerController : Photon.MonoBehaviour
{
public enum InputType { KeyboardAndMouse, OneVRHandheldController }
//set to keyboard and mouse in inspector if you want to test in editor without controller
public InputType inputType = InputType.OneVRHandheldController;
//movement and character controller variables
public float moveSpeed = 5f;
public float lookSpeed = 1f;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
public CharacterController characterController;
//camera
public Transform vrCameraTransform;
//camera parent - used to move camera/change position of cam
public Transform vrCameraTransformParent;
//player body parent for player movement
public Transform playerBodyTransform;
//variable for capsule controller direction
private Vector3 moveDirection = Vector3.zero;
//audio for footsteps
public AudioSource footSteps;
//mouse control variables for testing on PC
private Vector3 rotation;
public float mouseSensitivity = 100.0f;
public float clampAngle = 80.0f;
void Start()
{
rotation = vrCameraTransform.rotation.eulerAngles;
}
void Update()
{
//keyboard and mouse for testing purposes on PC
if (inputType == InputType.KeyboardAndMouse)
{
float mouseX = Input.GetAxis("Mouse X");
float mouseY = -Input.GetAxis("Mouse Y");
rotation.y += mouseX * mouseSensitivity * Time.deltaTime;
rotation.x += mouseY * mouseSensitivity * Time.deltaTime;
rotation.x = Mathf.Clamp(rotation.x, -clampAngle, clampAngle);
Quaternion localRotation = Quaternion.Euler(rotation.x, rotation.y, 0.0f);
vrCameraTransform.rotation = localRotation;
float y = moveDirection.y;
moveDirection = (vrCameraTransform.forward * moveSpeed * Input.GetAxis("Vertical")) +
(vrCameraTransform.right * moveSpeed * Input.GetAxis("Horizontal"));
moveDirection.y = y;
vrCameraTransformParent.localPosition = playerBodyTransform.localPosition;
playerBodyTransform.rotation = vrCameraTransform.rotation;
}
else
{
//get forward and right direction of the VR Camera
Vector3 forward = vrCameraTransform.TransformDirection(Vector3.forward).normalized;
Vector3 right = vrCameraTransform.TransformDirection(Vector3.right).normalized;
//y direction for falling/jumping
float y = moveDirection.y;
//use the forward and right direction of Camera for applying force to body in the correct direction.
moveDirection = (forward * moveSpeed * Input.GetAxis("Vertical")) + //up/down on joystic is vertical
(right * moveSpeed * Input.GetAxis("Horizontal")); //left right on joystick is horizontal
moveDirection.y = y;
//set camera parent to match player body position.
//cannot set camera position directly, seems to cause problems/not apply as reported by multiple Unity users.
//so in that case, we we have childed the camera and we move it by moving it's parent.
vrCameraTransformParent.localPosition = playerBodyTransform.localPosition;
playerBodyTransform.rotation = vrCameraTransform.rotation;
}
//if character is on the ground then they can jump if they desire to.
if (characterController.isGrounded)
{
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
//apply gravity to y axis move direction
moveDirection.y -= gravity * Time.deltaTime;
//if player is moving, play footstep sounds.
if (moveDirection.x > 0.1f || moveDirection.x < -0.1f || moveDirection.z < -0.1f || moveDirection.z > 0.1f)
{
if (!footSteps.isPlaying)
{
footSteps.Play();
}
}
else
{
footSteps.Stop();
}
//apply calculated movement to character controller.
characterController.Move(moveDirection * Time.deltaTime);
}
private void OnApplicationPause(bool pauseStatus)
{
InputTracking.Recenter();
}
}
@bokhQ
Copy link

bokhQ commented Jun 3, 2018

hello, new to the vr, can you share instructions?
we need the google vr sdk and unity's standard character asset?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment