Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEngine;
- using Valve.VR;
- public class StickMove : MonoBehaviour
- {
- [Tooltip("input action for stick movement, e.g. left joystick")]
- public SteamVR_Action_Vector2 moveAction;
- [Tooltip("input action for turning, e.g. right joystick. only the X axis is used.")]
- public SteamVR_Action_Vector2 turnAction;
- [Tooltip("headset position, for head-relative stick movement and turning.")]
- public Transform headTransform;
- [Tooltip("Transform to affect, e.g. the Player's playspace.")]
- public Transform targetTransform;
- [Tooltip("joystick range to ignore, since usually they stick to small positive values.")]
- [Range(0.0f, 1)]
- public float deadZone = 0.1f;
- [Tooltip("move speed multiplier")]
- [Range(0.0f, 50f)]
- public float moveSpeed = 10.0f;
- [Tooltip("turn speed multiplier, in degrees")]
- [Range(0.0f, 180f)]
- public float turnSpeed = 50.0f;
- void Update()
- {
- if (moveAction.axis.magnitude > deadZone)
- {
- targetTransform.Translate(
- // rotated around the Y axis (yaw) according to head,
- // ignoring X/Z roll/pitch, so no Y axis movement
- // there is likely a more efficient way to do this transform
- Quaternion.Euler(0.0f, headTransform.rotation.eulerAngles.y,0.0f) *
- // by the joystick axes along forward/right
- ((Vector3.forward * moveAction.axis.y + Vector3.right * moveAction.axis.x) *
- // by move speed
- moveSpeed * Time.deltaTime),
- // in world space, since headTransform.rotation is in world space
- Space.World);
- }
- if (Math.Abs(turnAction.axis.x) > deadZone)
- {
- // rotate the playspace relative to your head
- targetTransform.RotateAround(
- headTransform.transform.position,
- Vector3.up, // around Y axis
- turnAction.axis.x * turnSpeed * Time.deltaTime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment