mirror of
https://projects.caleb-brown.dev/UDRI-XRT/UDRIGEEKCup2024.git
synced 2025-01-22 15:18:25 -05:00
92 lines
3.1 KiB
C#
92 lines
3.1 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
|
|
public class MoveAndReturn : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
private float distance = 5f; // Distance to move along the selected axis
|
|
|
|
[SerializeField]
|
|
private Axis axisOfMovement = Axis.Right; // Axis of movement (default is X axis)
|
|
|
|
[SerializeField]
|
|
private float speed = 2f; // Speed of movement
|
|
|
|
[SerializeField]
|
|
private int numberOfCycles = 1; // Number of cycles of movement
|
|
|
|
[SerializeField]
|
|
private float pauseDuration = 1f; // Duration of pause after completing the specified number of cycles
|
|
|
|
private Vector3 startingPosition;
|
|
private Vector3 targetPosition;
|
|
private bool movingForward = true;
|
|
private int cyclesCompleted = 0;
|
|
|
|
// Enumeration for the axis of movement
|
|
public enum Axis
|
|
{
|
|
Right,
|
|
Up,
|
|
Forward
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
// Save the starting position of the object
|
|
startingPosition = transform.position;
|
|
|
|
// Calculate the target position based on the selected axis and distance
|
|
switch (axisOfMovement)
|
|
{
|
|
case Axis.Right:
|
|
targetPosition = startingPosition + (transform.right * distance); // new Vector3(distance, 0, 0);
|
|
break;
|
|
case Axis.Up:
|
|
targetPosition = startingPosition + (transform.up * distance); // new Vector3(0, distance, 0);
|
|
break;
|
|
case Axis.Forward:
|
|
targetPosition = startingPosition + (transform.forward * distance); // new Vector3(0, 0, distance);
|
|
break;
|
|
}
|
|
|
|
// Start the movement coroutine
|
|
StartCoroutine(MoveObject());
|
|
}
|
|
private IEnumerator MoveObject()
|
|
{
|
|
while (true)
|
|
{
|
|
// Move the object towards the target position
|
|
if (movingForward)
|
|
{
|
|
transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
|
|
|
|
// If the object reaches the target position, start moving back
|
|
if (transform.position == targetPosition)
|
|
movingForward = false;
|
|
}
|
|
// Move the object back to the starting position
|
|
else
|
|
{
|
|
transform.position = Vector3.MoveTowards(transform.position, startingPosition, speed * Time.deltaTime);
|
|
|
|
// If the object reaches the starting position, start moving forward again
|
|
if (transform.position == startingPosition)
|
|
{
|
|
movingForward = true;
|
|
cyclesCompleted++;
|
|
|
|
// Check if the specified number of cycles are completed
|
|
if (cyclesCompleted == numberOfCycles)
|
|
{
|
|
// Pause for the specified duration after completing the cycles
|
|
yield return new WaitForSeconds(pauseDuration);
|
|
cyclesCompleted = 0; // Reset the cycle count
|
|
}
|
|
}
|
|
}
|
|
yield return null; // Wait for the next frame
|
|
}
|
|
}
|
|
} |