campfire/Assets/Scripts/StickSpawner.cs

58 lines
1.9 KiB
C#
Raw Normal View History

2020-04-20 20:46:52 +02:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StickSpawner : MonoBehaviour {
public GameObject StickPrefab;
2020-04-21 02:11:11 +02:00
public GameObject CasettePrefab;
2020-04-20 20:46:52 +02:00
public float StickSpawnChance = 0.5f;
public float StickSpawnMinDistance = 5f;
2020-05-02 18:28:42 +02:00
[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;
2020-04-20 20:46:52 +02:00
public float StickSpawnAngle = 70f;
public float StickSpawnTimer = 2f;
2020-04-21 02:11:11 +02:00
public float CasetteChance = 0.2f;
2020-04-20 20:46:52 +02:00
private GameObject Player;
private float LastSpawn;
private bool CanSpawnNew = true;
2020-05-01 04:37:20 +02:00
private PickupStatus SpawnedPickup;
2020-04-20 20:46:52 +02:00
void Awake() {
2020-05-02 18:28:42 +02:00
Player = GameObject.FindGameObjectWithTag("MainCamera");
2020-04-20 20:46:52 +02:00
LastSpawn = Time.time + StickSpawnTimer * Random.value;
}
void Update() {
if (!CanSpawnNew) {
2020-05-01 04:37:20 +02:00
if (SpawnedPickup == null || SpawnedPickup.Grabbed) {
2020-04-20 20:46:52 +02:00
CanSpawnNew = true;
} else {
return;
}
}
if (Time.time > LastSpawn + StickSpawnTimer) {
LastSpawn = Time.time;
} else {
return;
}
var dir = transform.position - Player.transform.position;
2020-05-02 18:28:42 +02:00
if ((Vector3.Angle(Player.transform.forward, dir) > StickSpawnAngle && dir.magnitude > StickSpawnMinDistanceBehind) ||
dir.magnitude > StickSpawnMinDistance && Random.value <= StickSpawnChance) {
2020-04-21 02:11:11 +02:00
var SpawnedThing = StickPrefab;
if (Random.value <= CasetteChance) {
SpawnedThing = CasettePrefab;
}
2020-04-20 20:46:52 +02:00
CanSpawnNew = false;
2020-04-23 13:30:09 +02:00
var Spawned = GameObject.Instantiate(SpawnedThing, transform.position + Vector3.up * 2, Random.rotation);
2020-05-01 04:37:20 +02:00
SpawnedPickup = Spawned.GetComponent<PickupStatus>();
2020-04-20 20:46:52 +02:00
}
}
}