Guest User

StickMove.cs

a guest
Jan 7th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.05 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using Valve.VR;
  4.  
  5. public class StickMove : MonoBehaviour
  6. {
  7.     [Tooltip("input action for stick movement, e.g. left joystick")]
  8.     public SteamVR_Action_Vector2 moveAction;
  9.  
  10.     [Tooltip("input action for turning, e.g. right joystick. only the X axis is used.")]
  11.     public SteamVR_Action_Vector2 turnAction;
  12.  
  13.     [Tooltip("headset position, for head-relative stick movement and turning.")]
  14.     public Transform headTransform;
  15.  
  16.     [Tooltip("Transform to affect, e.g. the Player's playspace.")]
  17.     public Transform targetTransform;
  18.  
  19.     [Tooltip("joystick range to ignore, since usually they stick to small positive values.")]
  20.     [Range(0.0f, 1)]
  21.     public float deadZone = 0.1f;
  22.  
  23.     [Tooltip("move speed multiplier")]
  24.     [Range(0.0f, 50f)]
  25.     public float moveSpeed = 10.0f;
  26.  
  27.     [Tooltip("turn speed multiplier, in degrees")]
  28.     [Range(0.0f, 180f)]
  29.     public float turnSpeed = 50.0f;
  30.  
  31.     void Update()
  32.     {
  33.         if (moveAction.axis.magnitude > deadZone)
  34.         {
  35.             targetTransform.Translate(
  36.                 // rotated around the Y axis (yaw) according to head,
  37.                 // ignoring X/Z roll/pitch, so no Y axis movement
  38.                 // there is likely a more efficient way to do this transform
  39.                 Quaternion.Euler(0.0f, headTransform.rotation.eulerAngles.y,0.0f) *
  40.                 // by the joystick axes along forward/right
  41.                 ((Vector3.forward * moveAction.axis.y + Vector3.right * moveAction.axis.x) *
  42.                 // by move speed
  43.                 moveSpeed * Time.deltaTime),
  44.                 // in world space, since headTransform.rotation is in world space
  45.                 Space.World);
  46.         }
  47.  
  48.         if (Math.Abs(turnAction.axis.x) > deadZone)
  49.         {
  50.             // rotate the playspace relative to your head
  51.             targetTransform.RotateAround(
  52.                 headTransform.transform.position,
  53.                 Vector3.up, // around Y axis
  54.                 turnAction.axis.x * turnSpeed * Time.deltaTime);
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment