45 lines
1.2 KiB
C#
45 lines
1.2 KiB
C#
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
|
|
public float lifeTime = 5f;
|
|
|
|
private Transform cam;
|
|
private Vector3 targetOffset;
|
|
|
|
void Start()
|
|
{
|
|
// Get main camera
|
|
if (Camera.main != null)
|
|
{
|
|
cam = Camera.main.transform;
|
|
}
|
|
|
|
// Destroy after some time
|
|
Destroy(gameObject, lifeTime);
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (cam)
|
|
{
|
|
// Position in front of camera
|
|
targetOffset = cam.position + cam.forward * hoverDistance + Vector3.up * hoverHeight;
|
|
|
|
// 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
|
|
);
|
|
}
|
|
}
|
|
}
|