Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.7 KiB

  1. namespace Oculus.Platform.Samples.VrHoops
  2. {
  3. using UnityEngine;
  4. // This script moves to goal around in a random direction to add a bit more difficulty
  5. // to the game.
  6. public class GoalMover : MonoBehaviour {
  7. // how far to from the center before changing direction
  8. [SerializeField] private float MAX_OFFSET = 2.0f;
  9. // how fast the backboard will move
  10. [SerializeField] private float m_speed = 0.005f;
  11. // maximum interpolation distance allow to correct per update
  12. private const float MOVE_TOLERANCE = 0.1f;
  13. // the position the goal should be in - only differs if network updates come in
  14. private Vector3 m_expectedPosition;
  15. // the current move vector * m_speed;
  16. private Vector3 m_moveDirection;
  17. // the direction to move when we run into the boundary
  18. private Vector3 m_nextMoveDirection;
  19. public Vector3 ExpectedPosition
  20. {
  21. get { return m_expectedPosition; }
  22. set { m_expectedPosition = value; }
  23. }
  24. public Vector3 MoveDirection
  25. {
  26. get { return m_moveDirection; }
  27. set { m_moveDirection = value; }
  28. }
  29. public Vector3 NextMoveDirection
  30. {
  31. get { return m_nextMoveDirection; }
  32. set { m_nextMoveDirection = value; }
  33. }
  34. void Start ()
  35. {
  36. ExpectedPosition = transform.localPosition;
  37. m_moveDirection.x = Random.Range(-1.0f, 1.0f);
  38. m_moveDirection.y = Random.Range(-1.0f, 1.0f);
  39. m_moveDirection = Vector3.ClampMagnitude(m_moveDirection, m_speed);
  40. m_nextMoveDirection.x = -Mathf.Sign(m_moveDirection.x) * Random.Range(0f, 1.0f);
  41. m_nextMoveDirection.y = -Mathf.Sign(m_moveDirection.y) * Random.Range(0f, 1.0f);
  42. m_nextMoveDirection = Vector3.ClampMagnitude(m_nextMoveDirection, m_speed);
  43. }
  44. void FixedUpdate ()
  45. {
  46. // move a bit along our random direction
  47. transform.localPosition += MoveDirection;
  48. ExpectedPosition += MoveDirection;
  49. // make a slight correction to the position if we're not where we should be
  50. Vector3 correction = ExpectedPosition - transform.localPosition;
  51. correction = Vector3.ClampMagnitude(correction, MOVE_TOLERANCE);
  52. transform.localPosition += correction;
  53. // if we've gone too far from the center point, correct and change direction
  54. if (transform.localPosition.sqrMagnitude > (MAX_OFFSET*MAX_OFFSET))
  55. {
  56. transform.localPosition = Vector3.ClampMagnitude(transform.localPosition, MAX_OFFSET);
  57. ExpectedPosition = transform.localPosition;
  58. MoveDirection = NextMoveDirection;
  59. // select a the next randomish direction to move in
  60. m_nextMoveDirection.x = -Mathf.Sign(m_moveDirection.x) * Random.Range(0f, 1.0f);
  61. m_nextMoveDirection.y = -Mathf.Sign(m_moveDirection.y) * Random.Range(0f, 1.0f);
  62. m_nextMoveDirection = Vector3.ClampMagnitude(m_nextMoveDirection, m_speed);
  63. }
  64. }
  65. }
  66. }