There is now a new updated tutorial based on this one, so still read this one, but make sure to check out the new one for bug fixes, jumping code, and general improvement. Also, the latest version of the code and an example scene are included in the VR instincts plugin on GitHub.

One of the most basic game mechanics you want in your game is the ability to walk. I know that people claim that walking mechanics in VR games cause nausea, but they are also the most immersive form of movement. Could you imagine playing a game like Halo or Call of D uty where you have to teleport every step? No, it would be a terrible experience. But that’s my titanium stomach talking.

So let’s get to it! To start off, as always we are going to set up our steamVR bindings. You are going to want to create one action to store a Vector2 named MovementAxis, this is the one we use to get input from our touchpad.

Now we set up our scene, add a Rigidbody and capsule collider to your [cameraRig] component, make sure that you constrain all rotation on the rigidbody so you don’t find yourself falling over. For the capsule collider all you need to set is the radius since all the other values are controlled by our code, I set mine to 0.1.

Now we can start to code. Create a new script called movement and add it to your [CameraRig] component. Here Is the code I used:

using Valve.VR;

public class Movement : MonoBehaviour

{

private Vector2 trackpad;

private float Direction;

private Vector3 moveDirection;





public SteamVR_Input_Sources Hand;//Set Hand To Get Input From

public float speed;

public GameObject Head;

public CapsuleCollider Collider;

public GameObject AxisHand;//Hand Controller GameObject

public float Deadzone;//the Deadzone of the trackpad. used to prevent unwanted walking.

// Start is called before the first frame update



void Update()

{

//Set size and position of the capsule collider so it maches our head.

Collider.height = Head.transform.localPosition.y;

Collider.center = new Vector3(Head.transform.localPosition.x, Head.transform.localPosition.y / 2, Head.transform.localPosition.z);



moveDirection = Quaternion.AngleAxis(Angle(trackpad) + AxisHand.transform.localRotation.eulerAngles.y, Vector3.up) * Vector3.forward;//get the angle of the touch and correct it for the rotation of the controller

updateInput();

if (GetComponent<Rigidbody>().velocity.magnitude < speed && trackpad.magnitude >Deadzone){//make sure the touch isn't in the deadzone and we aren't going to fast.

GetComponent<Rigidbody().AddForce(moveDirection * 30);

}

}

public static float Angle(Vector2 p_vector2)

{

if (p_vector2.x < 0)

{

return 360 - (Mathf.Atan2(p_vector2.x, p_vector2.y) * Mathf.Rad2Deg * -1);

}

else

{

return Mathf.Atan2(p_vector2.x, p_vector2.y) * Mathf.Rad2Deg;

}

}

private void updateInput()

{

trackpad = SteamVR_Actions._default.MovementAxis.GetAxis(Hand);

}

}

Drag and drop/enter your values into the component and you should be ready to go! Check out the second part to add jumping and general code improvements.