ControlTransform.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace SC.XR.Unity
  5. {
  6. public class ControlTransform : MonoBehaviour
  7. {
  8. [Range(0.1f, 10f)]
  9. public float speed = 0.4f;
  10. Vector3 positionReset, eulerAnglesReset;
  11. void Awake()
  12. {
  13. positionReset = transform.position;
  14. eulerAnglesReset = transform.eulerAngles;
  15. }
  16. // Update is called once per frame
  17. void Update()
  18. {
  19. if (Application.platform == RuntimePlatform.Android)
  20. return;
  21. if (Input.GetKey(KeyCode.W))
  22. {
  23. transform.Translate(Vector3.forward * Time.deltaTime * speed);
  24. }
  25. else if (Input.GetKey(KeyCode.S))
  26. {
  27. transform.Translate(Vector3.back * Time.deltaTime * speed);
  28. }
  29. if (Input.GetKey(KeyCode.A))
  30. {
  31. transform.Translate(Vector3.left * Time.deltaTime * speed);
  32. }
  33. else if (Input.GetKey(KeyCode.D))
  34. {
  35. transform.Translate(Vector3.right * Time.deltaTime * speed);
  36. }
  37. if (Input.GetMouseButton(0) || Input.GetMouseButton(1))
  38. {
  39. transform.Rotate(Vector3.up, Input.GetAxis("Mouse X") * speed * 10);
  40. transform.Rotate(Vector3.left, Input.GetAxis("Mouse Y") * speed * 10);
  41. transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, 0);
  42. }
  43. if (Input.GetKey(KeyCode.Escape))
  44. {
  45. transform.position = positionReset;
  46. transform.eulerAngles = eulerAnglesReset;
  47. }
  48. }
  49. }
  50. }