Adding spinning treat spawner

This commit is contained in:
hz
2026-01-31 21:57:16 -06:00
parent 86a860126e
commit 36422bbbc6
2 changed files with 71 additions and 0 deletions
+53
View File
@@ -2642,6 +2642,58 @@ PrefabInstance:
m_AddedGameObjects: []
m_AddedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 99cce68fc82caea44b5a140697be00a8, type: 3}
--- !u!1 &2200000000
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 2200000001}
- component: {fileID: 2200000002}
m_Layer: 0
m_Name: Treat Spawner Center
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &2200000001
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2200000000}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &2200000002
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2200000000}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 14b0bf07aa1abc44a8ebd00cecacd6f4, type: 3}
m_Name:
m_EditorClassIdentifier: Assembly-CSharp::Spawner
objectToSpawn: {fileID: 262983251238298495, guid: 80c527a86ad6fc34dbc73ee80c1f2951, type: 3}
spawnInterval: 5
maxAlive: 0
spawnOnStart: 1
repeatSpawns: 1
launchForce: 4
launchRandomAngle: 360
spinSpeed: 180
--- !u!1660057539 &9223372036854775807
SceneRoots:
m_ObjectHideFlags: 0
@@ -2671,3 +2723,4 @@ SceneRoots:
- {fileID: 375061916}
- {fileID: 2100000001}
- {fileID: 2100000101}
- {fileID: 2200000001}
+18
View File
@@ -11,6 +11,9 @@ public class Spawner : MonoBehaviour
[SerializeField] private int maxAlive = 0;
[SerializeField] private bool spawnOnStart = true;
[SerializeField] private bool repeatSpawns = true;
[SerializeField] private float launchForce = 0f;
[SerializeField] private float launchRandomAngle = 0f;
[SerializeField] private float spinSpeed = 0f;
private readonly List<GameObject> spawned = new List<GameObject>();
@@ -31,6 +34,14 @@ public class Spawner : MonoBehaviour
}
}
private void Update()
{
if (spinSpeed != 0f)
{
transform.Rotate(0f, 0f, spinSpeed * Time.deltaTime);
}
}
private IEnumerator SpawnRoutine()
{
// Infinite loop to keep spawning (can be modified)
@@ -66,6 +77,13 @@ public class Spawner : MonoBehaviour
var instance = Instantiate(objectToSpawn, transform.position, Quaternion.identity);
spawned.Add(instance);
if (launchForce > 0f && instance.TryGetComponent<Rigidbody2D>(out var body))
{
var angleOffset = launchRandomAngle > 0f ? Random.Range(-launchRandomAngle, launchRandomAngle) : 0f;
var direction = Quaternion.Euler(0f, 0f, angleOffset) * transform.right;
body.AddForce(direction.normalized * launchForce, ForceMode2D.Impulse);
}
}
private int CountAlive()