ControlTransform.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. {
  21. transform.Rotate(Vector3.up, Input.GetAxis("Mouse X") * speed * 2);
  22. transform.Rotate(Vector3.left, Input.GetAxis("Mouse Y") * speed * 2);
  23. transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, 0);
  24. return;
  25. }
  26. if (Input.GetKey(KeyCode.W))
  27. {
  28. transform.Translate(Vector3.forward * Time.deltaTime * speed);
  29. }
  30. else if (Input.GetKey(KeyCode.S))
  31. {
  32. transform.Translate(Vector3.back * Time.deltaTime * speed);
  33. }
  34. if (Input.GetKey(KeyCode.A))
  35. {
  36. transform.Translate(Vector3.left * Time.deltaTime * speed);
  37. }
  38. else if (Input.GetKey(KeyCode.D))
  39. {
  40. transform.Translate(Vector3.right * Time.deltaTime * speed);
  41. }
  42. if (Input.GetMouseButton(0) || Input.GetMouseButton(1))
  43. {
  44. transform.Rotate(Vector3.up, Input.GetAxis("Mouse X") * speed * 10);
  45. transform.Rotate(Vector3.left, Input.GetAxis("Mouse Y") * speed * 10);
  46. transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, 0);
  47. }
  48. if (Input.GetKey(KeyCode.Escape))
  49. {
  50. transform.position = positionReset;
  51. transform.eulerAngles = eulerAnglesReset;
  52. }
  53. }
  54. }
  55. }