1
0
mirror of https://projects.caleb-brown.dev/UDRI-XRT/UDRIGEEKCup2024.git synced 2025-01-22 07:08:51 -05:00
UDRIGEEKCup2024/Assets/Scripts/RandomImageMover.cs
2024-04-24 14:52:55 -04:00

36 lines
1.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MAVRIC.GEEKCup
{
public class RandomImageMover : MonoBehaviour
{
public float moveSpeed = 200f;
private RectTransform rectTransform;
private Vector3 targetPosition;
void OnEnable()
{
rectTransform = GetComponent<RectTransform>();
MoveImageToNewPosition();
}
void Update()
{
rectTransform.localPosition = Vector3.MoveTowards(rectTransform.localPosition, targetPosition, moveSpeed * Time.deltaTime);
// If the image reaches the target position, choose a new random position
if (Vector3.Distance(rectTransform.localPosition, targetPosition) < 1f)
{
MoveImageToNewPosition();
}
}
void MoveImageToNewPosition()
{
RectTransform canvasRect = rectTransform.GetComponentInParent<Canvas>().GetComponent<RectTransform>();
float x = Random.Range(-canvasRect.sizeDelta.x / 2, canvasRect.sizeDelta.x / 2);
float y = Random.Range(-canvasRect.sizeDelta.y / 2, canvasRect.sizeDelta.y / 2);
targetPosition = new Vector3(x, y, 0);
}
}
}