JoeJeffController.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using UnityEngine;
  2. using System.Collections;
  3. using Valve.VR;
  4. using Valve.VR.InteractionSystem;
  5. namespace Valve.VR.InteractionSystem.Sample
  6. {
  7. public class JoeJeffController : MonoBehaviour
  8. {
  9. public Transform Joystick;
  10. public float joyMove = 0.1f;
  11. public SteamVR_Action_Vector2 moveAction = SteamVR_Input.GetAction<SteamVR_Action_Vector2>("platformer", "Move");
  12. public SteamVR_Action_Boolean jumpAction = SteamVR_Input.GetAction<SteamVR_Action_Boolean>("platformer", "Jump");
  13. public JoeJeff character;
  14. public Renderer jumpHighlight;
  15. private Vector3 movement;
  16. private bool jump;
  17. private float glow;
  18. private SteamVR_Input_Sources hand;
  19. private Interactable interactable;
  20. private void Start()
  21. {
  22. interactable = GetComponent<Interactable>();
  23. }
  24. private void Update()
  25. {
  26. if (interactable.attachedToHand)
  27. {
  28. hand = interactable.attachedToHand.handType;
  29. Vector2 m = moveAction[hand].axis;
  30. movement = new Vector3(m.x, 0, m.y);
  31. jump = jumpAction[hand].stateDown;
  32. glow = Mathf.Lerp(glow, jumpAction[hand].state ? 1.5f : 1.0f, Time.deltaTime * 20);
  33. }
  34. else
  35. {
  36. movement = Vector2.zero;
  37. jump = false;
  38. glow = 0;
  39. }
  40. Joystick.localPosition = movement * joyMove;
  41. float rot = transform.eulerAngles.y;
  42. movement = Quaternion.AngleAxis(rot, Vector3.up) * movement;
  43. jumpHighlight.sharedMaterial.SetColor("_EmissionColor", Color.white * glow);
  44. character.Move(movement * 2, jump);
  45. }
  46. }
  47. }