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