Simple Unity Android VR controller setup for Xbox 360 Controllers

It is no longer required to download the Android VR SDK to build a mobile VR game or application for Unity. Instead, you can set your game up in the Unity build settings and make use of UnityEngine.VR.

Creating a simple player controller may be a bit tricky and so I hope this post will save some developers a head ache when it comes to moving a player around with a gamepad in Android based VR projects.

In this setup, I’m using the following:
1 x Android 6.0 Phone
1 x XBOX 360 Wired USB Controller (but you can use any Android VR controller)
1 x Generic Android VR Headset

To begin, it’s important to understand that you have to create a parent and child arrangement of GameObject’s to control a VR player. Whereas generally a simple FPS controller might be arranged with a Camera as a child to a Capsule, with Unity VR you’ll have to child the Camera to a Camera parent Transform and that Camera parent will be a child to your main player. Even more, the Body of the player will have to be a separate child Transform of the parenting Main Player GameObject if your player has a body. Why? This is done to control the movement of the Camera since moving the Camera Transform directly in VR seems to result in odd behaviour/non-functioning movement at the time of this post.

Here is how your basic Main Player hierarchy should look:

Step 2:
Attach a camera to the Camera GameObject.

Step 3:
In your input settings, add two new axes. For the two different Android phones I tested, the Xbox 360 controller I had seemed to map to these axis:

Step 4:

Create a new script and call it SimpleVRController.cs Use the code below then assign the proper transforms in the inspector.

/*************************************************************************
* 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();
}
}

Unity 2D Target Scanner

Target scanners are essential for any game where you have an enemy mob that actively looks for prey. One way to scan for targets is to cast a circular physics ray based on the radius area you would like to scan.

Below is a quick and helpful intro I’ve put together for anyone who needs a quick and easy target scanner to notify another script via event subscription. It searches for targets based on the tags set through the Unity Inspector.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Simple2DTargetScanner : MonoBehaviour
{
public float scanFrequency; //in seconds
public float scanRadius = 5; //in world units
private float scanFrequencyCounter = 0; //counter for time between scans
public string[] targetTagArray; //array of tags of GameObjects to include
//delegate event to notify subscribed scripts
public delegate void TargetHit(Transform target);
public event TargetHit OnTargetHit;
public Color editorCircleColor = Color.green; //circle colour for editor
public bool scannerEnabled = true; //enable or disable scanner (or you can just enable/disable script if you prefer)
// Update is called once per frame
void FixedUpdate()
{
//if not enabled, return to stop code below from running.
if (!scannerEnabled)
return;
//counter to track time between scans
scanFrequencyCounter += Time.deltaTime;
//once counter reaches the total time we set between scans, we scan for target and reset counter to zero.
if (scanFrequencyCounter >= scanFrequency)
{
scanFrequencyCounter = 0;
//using Physics2D we will draw a circle and use a 2d raycast to see what objects are within it.
//Targets must have collider for raycast to return or it won't be detected with this method.
RaycastHit2D[] raycastHit2D = Physics2D.CircleCastAll(this.transform.position, scanRadius, Vector2.right, scanRadius * 2f);
//if we hit any targets, notify listeners that need this info
//make sure to check if OnTargetHit is null or not in case no subscribers (but if no subscribers, why using this at all? 😉 )
if (raycastHit2D.Length > 0 && OnTargetHit != null)
{
for(int i = 0; i < raycastHit2D.Length; i++)
{
//Important note: if you have a lot of target scanners running at once, for performance it might be better to just scan based on
//layers rather than compare gameobject tags.
for (int t = 0; t < targetTagArray.Length; t++)
{
if (raycastHit2D[i].transform.CompareTag(targetTagArray[t]))
{
//delgate event
OnTargetHit(raycastHit2D[i].transform);
//break out of both loops after first hit
i = raycastHit2D.Length + 1;
break;
}
}
}
}
}
}
//Unity editor code. Make sure to use hashtag UNITY_EDITOR so this code doesn't compile to non-editor build.
#if UNITY_EDITOR
//Draw gizmo wire disc (circle) when GameObject is selected.
//If you want it to always draw, then you can use void OnDrawGizmos() instead
void OnDrawGizmosSelected()
{
//set gizmo colour.
UnityEditor.Handles.color = editorCircleColor;
//draw wire circle (disc in unity lingo) based on radius variable you set.
UnityEditor.Handles.DrawWireDisc(this.transform.position, this.transform.forward, scanRadius);
}
#endif
}

How to use from another script:

//Method activated when Simple2DTargetScanner OnTargetHit event is called.
void OnTargetHit(Transform target)
{
//do something with target hit
}
void OnEnable()
{
//subscribe to Simple2DTargetScanner OnTargetHit event notification if GameObject is enabled.
this.transform.GetComponent<Simple2DTargetScanner>().OnTargetHit += OnTargetHit;
}
void OnDisable()
{
//unsubscribe to Simple2DTargetScanner OnTargetHit event notification if GameObject is disabled
this.transform.GetComponent<Simple2DTargetScanner>().OnTargetHit -= OnTargetHit;
}
view raw EnemyMob.cs hosted with ❤ by GitHub

 

Final notes:

This is a simple solution if you only require a few dozen scanners running at a time or less. If you have many more running concurrently then using tags may be slower and I would recommend scanning based on Unity Layers. You can set which layer to RayCast2D on or alternatively, you can set in Project Settings -> Physics 2D and set which physics layers interact with one another.