88 lines
2.5 KiB
C#
88 lines
2.5 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class ConveiorRotund : MonoBehaviour
|
|
{
|
|
[SerializeField, Tooltip("Coordonate puncte pe banda")]
|
|
private Transform[] puncte;
|
|
|
|
[SerializeField, Tooltip("Raza sfere vizuale")]
|
|
private float raza;
|
|
|
|
[SerializeField, Tooltip("viteza obiectului pe banda")]
|
|
private float speed;
|
|
|
|
[SerializeField, Tooltip("Distanta maxima pentru a trece de la un punct la altul")]
|
|
private float maxDistance;
|
|
|
|
|
|
private Dictionary<GameObject, int> obiectePeConveior = new Dictionary<GameObject, int>();
|
|
|
|
void Update()
|
|
{
|
|
List<GameObject> obiecte = new List<GameObject>(obiectePeConveior.Keys);
|
|
|
|
foreach (GameObject obiect in obiecte)
|
|
{
|
|
int index = obiectePeConveior[obiect];
|
|
|
|
if (index >= puncte.Length)
|
|
continue;
|
|
|
|
|
|
Vector3 newPos = Vector3.MoveTowards(
|
|
obiect.transform.position,
|
|
puncte[index].position,
|
|
speed * Time.deltaTime
|
|
);
|
|
|
|
Vector3 finalPos = new Vector3(newPos.x, obiect.transform.position.y, newPos.z);
|
|
Quaternion newRot = Quaternion.RotateTowards(
|
|
obiect.transform.rotation,
|
|
puncte[index].rotation,
|
|
speed * Time.deltaTime
|
|
);
|
|
|
|
obiect.transform.position = finalPos;
|
|
obiect.transform.rotation = newRot;
|
|
|
|
|
|
float distance = Vector3.Distance(obiect.transform.position, puncte[index].position);
|
|
|
|
if (distance < maxDistance)
|
|
{
|
|
obiectePeConveior[obiect] = index + 1;
|
|
Debug.Log($"{obiect.name} a ajuns la punctul {index + 1}");
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnDrawGizmos()
|
|
{
|
|
if (puncte == null) return;
|
|
foreach (Transform t in puncte)
|
|
{
|
|
if (t != null)
|
|
Gizmos.DrawWireSphere(t.position, raza);
|
|
}
|
|
}
|
|
|
|
private void OnCollisionEnter(Collision collision)
|
|
{
|
|
if (!obiectePeConveior.ContainsKey(collision.gameObject))
|
|
{
|
|
obiectePeConveior.Add(collision.gameObject, 0);
|
|
Debug.Log($"{collision.gameObject.name} a intrat pe bandă");
|
|
}
|
|
}
|
|
|
|
private void OnCollisionExit(Collision collision)
|
|
{
|
|
if (obiectePeConveior.ContainsKey(collision.gameObject))
|
|
{
|
|
obiectePeConveior.Remove(collision.gameObject);
|
|
Debug.Log($"{collision.gameObject.name} a ieșit de pe bandă");
|
|
}
|
|
}
|
|
}
|