87 lines
2.1 KiB
C#
87 lines
2.1 KiB
C#
using UnityEngine;
|
|
|
|
namespace Oculus.Platform.Samples.VrHoops
|
|
{
|
|
public class GoalMover : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
private float MAX_OFFSET = 2f;
|
|
|
|
[SerializeField]
|
|
private float m_speed = 0.005f;
|
|
|
|
private const float MOVE_TOLERANCE = 0.1f;
|
|
|
|
private Vector3 m_expectedPosition;
|
|
|
|
private Vector3 m_moveDirection;
|
|
|
|
private Vector3 m_nextMoveDirection;
|
|
|
|
public Vector3 ExpectedPosition
|
|
{
|
|
get
|
|
{
|
|
return m_expectedPosition;
|
|
}
|
|
set
|
|
{
|
|
m_expectedPosition = value;
|
|
}
|
|
}
|
|
|
|
public Vector3 MoveDirection
|
|
{
|
|
get
|
|
{
|
|
return m_moveDirection;
|
|
}
|
|
set
|
|
{
|
|
m_moveDirection = value;
|
|
}
|
|
}
|
|
|
|
public Vector3 NextMoveDirection
|
|
{
|
|
get
|
|
{
|
|
return m_nextMoveDirection;
|
|
}
|
|
set
|
|
{
|
|
m_nextMoveDirection = value;
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
ExpectedPosition = base.transform.localPosition;
|
|
m_moveDirection.x = Random.Range(-1f, 1f);
|
|
m_moveDirection.y = Random.Range(-1f, 1f);
|
|
m_moveDirection = Vector3.ClampMagnitude(m_moveDirection, m_speed);
|
|
m_nextMoveDirection.x = (0f - Mathf.Sign(m_moveDirection.x)) * Random.Range(0f, 1f);
|
|
m_nextMoveDirection.y = (0f - Mathf.Sign(m_moveDirection.y)) * Random.Range(0f, 1f);
|
|
m_nextMoveDirection = Vector3.ClampMagnitude(m_nextMoveDirection, m_speed);
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
base.transform.localPosition += MoveDirection;
|
|
ExpectedPosition += MoveDirection;
|
|
Vector3 vector = ExpectedPosition - base.transform.localPosition;
|
|
vector = Vector3.ClampMagnitude(vector, 0.1f);
|
|
base.transform.localPosition += vector;
|
|
if (base.transform.localPosition.sqrMagnitude > MAX_OFFSET * MAX_OFFSET)
|
|
{
|
|
base.transform.localPosition = Vector3.ClampMagnitude(base.transform.localPosition, MAX_OFFSET);
|
|
ExpectedPosition = base.transform.localPosition;
|
|
MoveDirection = NextMoveDirection;
|
|
m_nextMoveDirection.x = (0f - Mathf.Sign(m_moveDirection.x)) * Random.Range(0f, 1f);
|
|
m_nextMoveDirection.y = (0f - Mathf.Sign(m_moveDirection.y)) * Random.Range(0f, 1f);
|
|
m_nextMoveDirection = Vector3.ClampMagnitude(m_nextMoveDirection, m_speed);
|
|
}
|
|
}
|
|
}
|
|
}
|