ShipPlayerController.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using UnityEngine;
  2. using System.Collections;
  3. public class ShipPlayerController : MonoBehaviour
  4. {
  5. public float m_speed = 1f;
  6. public float m_rotationSpeed = 1f;
  7. public Collider2D m_moveArea;
  8. void Update()
  9. {
  10. if (Input.GetKey(KeyCode.LeftArrow))
  11. {
  12. gameObject.transform.Rotate(new Vector3(0f, 0f, m_rotationSpeed * Time.deltaTime));
  13. }
  14. if (Input.GetKey(KeyCode.RightArrow))
  15. {
  16. gameObject.transform.Rotate(new Vector3(0f, 0f, -m_rotationSpeed * Time.deltaTime));
  17. }
  18. Vector3 dir = gameObject.transform.rotation * Vector3.right;
  19. gameObject.transform.position += dir * m_speed * Time.deltaTime;
  20. if (!m_moveArea.bounds.Contains(transform.position))
  21. {
  22. Vector3 new_pos = transform.position;
  23. if (transform.position.x > m_moveArea.bounds.max.x)
  24. {
  25. new_pos.x = m_moveArea.bounds.min.x;
  26. }
  27. else if (transform.position.x < m_moveArea.bounds.min.x)
  28. {
  29. new_pos.x = m_moveArea.bounds.max.x;
  30. }
  31. if (transform.position.y > m_moveArea.bounds.max.y)
  32. {
  33. new_pos.y = m_moveArea.bounds.min.y;
  34. }
  35. else if (transform.position.y < m_moveArea.bounds.min.y)
  36. {
  37. new_pos.y = m_moveArea.bounds.max.y;
  38. }
  39. transform.position = new_pos;
  40. }
  41. }
  42. }