-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloatingObject3D.cs
More file actions
93 lines (75 loc) · 2.72 KB
/
Copy pathFloatingObject3D.cs
File metadata and controls
93 lines (75 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// FloatingObject3D.cs
using UnityEngine;
using UnityEngine.Events;
[DisallowMultipleComponent]
public class FloatingObject3D : MonoBehaviour
{
[Header("Floating Settings")]
[SerializeField] private float amplitude = 0.5f; // Height of float
[SerializeField] private float frequency = 1f; // Speed of float
[SerializeField] private Vector3 floatAxis = Vector3.up;
[Header("Optional Rotation")]
[SerializeField] private bool enableRotation = false;
[SerializeField] private Vector3 rotationSpeed = new Vector3(0f, 45f, 0f);
[Header("Pickup Integration")]
[SerializeField] private bool enablePickup = false;
[Tooltip("The tag of the object that can trigger this pickup (e.g., 'Player').")]
[SerializeField] private string collectorTag = "Player";
[Tooltip("Fires when the collector enters the trigger collider.")]
public UnityEvent onPickup;
[SerializeField] private bool destroyOnPickup = true;
private Vector3 startPosition;
private float randomOffset;
private void Awake()
{
startPosition = transform.position;
randomOffset = Random.Range(0f, Mathf.PI * 2f); // Prevent synchronized motion
}
private void Update()
{
FloatMotion();
Rotate();
}
private void FloatMotion()
{
float offset = Mathf.Sin((Time.time + randomOffset) * frequency) * amplitude;
transform.position = startPosition + floatAxis.normalized * offset;
}
private void Rotate()
{
if (!enableRotation) return;
transform.Rotate(rotationSpeed * Time.deltaTime);
}
public void ResetStartPosition()
{
startPosition = transform.position;
}
// --- Pickup Logic ---
private void OnTriggerEnter(Collider other)
{
if (!enablePickup) return;
if (other.CompareTag(collectorTag))
{
onPickup?.Invoke();
if (destroyOnPickup)
{
Destroy(gameObject);
}
}
}
// --- Gizmos Visualization ---
private void OnDrawGizmos()
{
Vector3 basePos = Application.isPlaying ? startPosition : transform.position;
Vector3 direction = floatAxis.normalized;
Vector3 upBound = basePos + (direction * amplitude);
Vector3 downBound = basePos - (direction * amplitude);
Gizmos.color = Color.cyan;
Gizmos.DrawLine(upBound, downBound);
Gizmos.color = Color.magenta;
Gizmos.DrawWireSphere(upBound, 0.1f);
Gizmos.DrawWireSphere(downBound, 0.1f);
Gizmos.color = Color.white;
Gizmos.DrawCube(basePos, new Vector3(0.05f, 0.05f, 0.05f));
}
}