campfire/Assets/Scripts/StickSpawner.cs

58 lines
1.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StickSpawner : MonoBehaviour {
public GameObject StickPrefab;
public GameObject CasettePrefab;
public float StickSpawnChance = 0.5f;
public float StickSpawnMinDistance = 5f;
[Tooltip("Stick spawning minimum distance when spawning a stick behind the player. These can be noticeable in VR because of erratic head movements.")]
public float StickSpawnMinDistanceBehind = 0f;
public float StickSpawnAngle = 70f;
public float StickSpawnTimer = 2f;
public float CasetteChance = 0.2f;
private GameObject Player;
private float LastSpawn;
private bool CanSpawnNew = true;
private PickupStatus SpawnedPickup;
void Awake() {
Player = GameObject.FindGameObjectWithTag("MainCamera");
LastSpawn = Time.time + StickSpawnTimer * Random.value;
}
void Update() {
if (!CanSpawnNew) {
if (SpawnedPickup == null || SpawnedPickup.Grabbed) {
CanSpawnNew = true;
} else {
return;
}
}
if (Time.time > LastSpawn + StickSpawnTimer) {
LastSpawn = Time.time;
} else {
return;
}
var dir = transform.position - Player.transform.position;
if ((Vector3.Angle(Player.transform.forward, dir) > StickSpawnAngle && dir.magnitude > StickSpawnMinDistanceBehind) ||
dir.magnitude > StickSpawnMinDistance && Random.value <= StickSpawnChance) {
var SpawnedThing = StickPrefab;
if (Random.value <= CasetteChance) {
SpawnedThing = CasettePrefab;
}
CanSpawnNew = false;
var Spawned = GameObject.Instantiate(SpawnedThing, transform.position + Vector3.up * 2, Random.rotation);
SpawnedPickup = Spawned.GetComponent<PickupStatus>();
}
}
}