MiniGames/Assets/Scripts/CubeClash/CubeClash_Butterfly.cs

45 lines
1.2 KiB
C#
Raw Normal View History

2025-09-01 05:03:00 +05:00
using UnityEngine;
public class CubeClash_Butterfly : MonoBehaviour
{
[Header("Movement")]
public float flySpeed = 2f;
public float hoverDistance = 2f; // Distance in front of camera
public float hoverHeight = 1.5f; // Height offset
2025-09-01 05:03:00 +05:00
public float lifeTime = 5f;
private Transform cam;
private Vector3 targetOffset;
2025-09-01 05:03:00 +05:00
void Start()
{
// Get main camera
if (Camera.main != null)
{
cam = Camera.main.transform;
}
// Destroy after some time
2025-09-01 05:03:00 +05:00
Destroy(gameObject, lifeTime);
}
void Update()
{
if (cam)
2025-09-01 05:03:00 +05:00
{
// Position in front of camera
targetOffset = cam.position + cam.forward * hoverDistance + Vector3.up * hoverHeight;
2025-09-01 05:03:00 +05:00
// Smoothly move toward the target offset
transform.position = Vector3.Lerp(transform.position, targetOffset, flySpeed * Time.deltaTime);
// Face the camera (optional)
transform.rotation = Quaternion.Slerp(
transform.rotation,
Quaternion.LookRotation(cam.position - transform.position),
Time.deltaTime * flySpeed
);
2025-09-01 05:03:00 +05:00
}
}
}