193 lines
4.3 KiB
C#
193 lines
4.3 KiB
C#
using UnityEngine;
|
|
|
|
public class MovingSystem : MonoBehaviour
|
|
{
|
|
public enum MoveType
|
|
{
|
|
None = 0,
|
|
Random = 1,
|
|
Loop = 2,
|
|
OneWay = 3,
|
|
InSphere = 4
|
|
}
|
|
|
|
public MoveType moveType;
|
|
|
|
public Color connectLineGizmos = Color.red;
|
|
|
|
public bool useRotation = true;
|
|
|
|
public float rotationSpeed = 4f;
|
|
|
|
public Transform[] movePoints;
|
|
|
|
public float speed = 4f;
|
|
|
|
public float inSphereRange = 5f;
|
|
|
|
private int nextMovePointIndex;
|
|
|
|
private bool isMoveForward = true;
|
|
|
|
private Vector3 spherePosition;
|
|
|
|
private Vector3 sphereNewPointPosition;
|
|
|
|
private void Start()
|
|
{
|
|
nextMovePointIndex = 0;
|
|
if (moveType != MoveType.InSphere && movePoints.Length != 0)
|
|
{
|
|
base.transform.position = movePoints[nextMovePointIndex].position;
|
|
return;
|
|
}
|
|
spherePosition = base.transform.position;
|
|
GetNextPoint();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
Quaternion b = Quaternion.identity;
|
|
if (moveType != MoveType.InSphere && movePoints.Length != 0)
|
|
{
|
|
if (base.transform.position == movePoints[nextMovePointIndex].position)
|
|
{
|
|
GetNextPoint();
|
|
}
|
|
base.transform.position = Vector3.MoveTowards(base.transform.position, movePoints[nextMovePointIndex].position, Time.deltaTime * speed);
|
|
if (useRotation)
|
|
{
|
|
Vector3 vector = movePoints[nextMovePointIndex].position - base.transform.position;
|
|
if (vector != Vector3.zero)
|
|
{
|
|
b = Quaternion.LookRotation(vector);
|
|
}
|
|
}
|
|
}
|
|
else if (useRotation)
|
|
{
|
|
if (Vector3.Distance(base.transform.position, sphereNewPointPosition) < 1f)
|
|
{
|
|
GetNextPoint();
|
|
}
|
|
base.transform.Translate(Vector3.forward * Time.deltaTime * speed);
|
|
Vector3 vector = sphereNewPointPosition - base.transform.position;
|
|
if (vector != Vector3.zero)
|
|
{
|
|
b = Quaternion.LookRotation(vector);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (base.transform.position == sphereNewPointPosition)
|
|
{
|
|
GetNextPoint();
|
|
}
|
|
base.transform.position = Vector3.MoveTowards(base.transform.position, sphereNewPointPosition, Time.deltaTime * speed);
|
|
}
|
|
if (useRotation)
|
|
{
|
|
base.transform.rotation = Quaternion.Slerp(base.transform.rotation, b, rotationSpeed * Time.deltaTime);
|
|
}
|
|
}
|
|
|
|
private void GetNextPoint()
|
|
{
|
|
switch (moveType)
|
|
{
|
|
case MoveType.None:
|
|
if (isMoveForward)
|
|
{
|
|
nextMovePointIndex++;
|
|
if (nextMovePointIndex == movePoints.Length)
|
|
{
|
|
nextMovePointIndex = movePoints.Length - 1;
|
|
isMoveForward = false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
nextMovePointIndex--;
|
|
if (nextMovePointIndex < 0)
|
|
{
|
|
nextMovePointIndex = 1;
|
|
isMoveForward = true;
|
|
}
|
|
}
|
|
break;
|
|
case MoveType.Loop:
|
|
nextMovePointIndex++;
|
|
if (nextMovePointIndex == movePoints.Length)
|
|
{
|
|
nextMovePointIndex = 0;
|
|
}
|
|
break;
|
|
case MoveType.Random:
|
|
nextMovePointIndex = Random.Range(0, movePoints.Length);
|
|
break;
|
|
case MoveType.OneWay:
|
|
nextMovePointIndex++;
|
|
if (nextMovePointIndex == movePoints.Length)
|
|
{
|
|
nextMovePointIndex = 0;
|
|
base.transform.position = movePoints[nextMovePointIndex].position;
|
|
}
|
|
break;
|
|
case MoveType.InSphere:
|
|
sphereNewPointPosition = spherePosition + Random.onUnitSphere * inSphereRange;
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void OnDrawGizmos()
|
|
{
|
|
switch (moveType)
|
|
{
|
|
case MoveType.None:
|
|
{
|
|
for (int j = 0; j < movePoints.Length - 1; j++)
|
|
{
|
|
Gizmos.color = connectLineGizmos;
|
|
Gizmos.DrawLine(movePoints[j].transform.position, movePoints[j + 1].transform.position);
|
|
}
|
|
break;
|
|
}
|
|
case MoveType.Loop:
|
|
{
|
|
for (int k = 0; k < movePoints.Length - 1; k++)
|
|
{
|
|
Gizmos.color = connectLineGizmos;
|
|
Gizmos.DrawLine(movePoints[k].transform.position, movePoints[k + 1].transform.position);
|
|
}
|
|
Gizmos.DrawLine(movePoints[movePoints.Length - 1].transform.position, movePoints[0].transform.position);
|
|
break;
|
|
}
|
|
case MoveType.Random:
|
|
{
|
|
for (int l = 0; l < movePoints.Length; l++)
|
|
{
|
|
for (int m = 0; m < movePoints.Length; m++)
|
|
{
|
|
Gizmos.color = connectLineGizmos;
|
|
Gizmos.DrawLine(movePoints[l].transform.position, movePoints[m].transform.position);
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
case MoveType.OneWay:
|
|
{
|
|
for (int i = 0; i < movePoints.Length - 1; i++)
|
|
{
|
|
Gizmos.color = connectLineGizmos;
|
|
Gizmos.DrawLine(movePoints[i].transform.position, movePoints[i + 1].transform.position);
|
|
}
|
|
Gizmos.color = Color.black;
|
|
Gizmos.DrawSphere(movePoints[movePoints.Length - 1].transform.position, 0.3f);
|
|
break;
|
|
}
|
|
case MoveType.InSphere:
|
|
break;
|
|
}
|
|
}
|
|
}
|