85 lines
2.9 KiB
C#
85 lines
2.9 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
[RequireComponent(typeof(Rigidbody))]
|
|
public class Torch : MonoBehaviour {
|
|
public Transform FirePoint;
|
|
public ParticleSystem Particles;
|
|
public Light Light;
|
|
public Color NormalColor;
|
|
public Color PanicColor;
|
|
public float Lifetime;
|
|
public float PanicLifetimeThreshold;
|
|
public float PassTorchAnimationDuration;
|
|
public float IgniteDuration;
|
|
|
|
public bool Burning {
|
|
get {
|
|
return Lifetime > 0;
|
|
}
|
|
}
|
|
|
|
private Rigidbody Body;
|
|
|
|
private Quaternion PassRotation = Quaternion.LookRotation(Vector3.forward, (Vector3.right + Vector3.forward).normalized);
|
|
private bool PassingTorch = false;
|
|
private float TorchPassStarted = 0;
|
|
private Transform PassTo;
|
|
private Vector3 PassStartPosition;
|
|
private Quaternion PassStartRotation;
|
|
|
|
private Vector3 BaseScale;
|
|
|
|
private void Awake() {
|
|
Body = GetComponent<Rigidbody>();
|
|
BaseScale = transform.localScale;
|
|
}
|
|
|
|
private void Update() {
|
|
if (PassingTorch) {
|
|
if (PassTo != null) {
|
|
// Move to other, to-be-enflamed torch
|
|
transform.position = Vector3.Lerp(
|
|
PassStartPosition,
|
|
PassTo.position - transform.rotation * FirePoint.localPosition,
|
|
(Time.time - TorchPassStarted) / PassTorchAnimationDuration);
|
|
transform.localRotation = Quaternion.Slerp(
|
|
PassStartRotation,
|
|
PassRotation,
|
|
(Time.time - TorchPassStarted) / PassTorchAnimationDuration * 2f);
|
|
}
|
|
if (Time.time - TorchPassStarted >= IgniteDuration) {
|
|
Lifetime = 0;
|
|
}
|
|
} else if (Burning) {
|
|
// Move to origin
|
|
transform.localPosition = Vector3.Lerp(transform.localPosition, Vector3.zero, 12f * Time.deltaTime);
|
|
transform.localRotation = Quaternion.Slerp(transform.localRotation, Quaternion.LookRotation(Vector3.forward, Vector3.up), 12f * Time.deltaTime);
|
|
}
|
|
|
|
Lifetime -= Time.deltaTime;
|
|
Light.color = Color.Lerp(Light.color, Lifetime <= PanicLifetimeThreshold ? PanicColor : NormalColor, 4f * Time.deltaTime);
|
|
if (Lifetime <= 0) {
|
|
Particles.Stop();
|
|
Light.color = Color.Lerp(Light.color, Color.black, 4f * Time.deltaTime);
|
|
if (Light.color == Color.black) {
|
|
Light.enabled = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void PassTorch(Transform target) {
|
|
PassingTorch = true;
|
|
PassTo = target;
|
|
TorchPassStarted = Time.time;
|
|
PassStartPosition = transform.position;
|
|
PassStartRotation = transform.localRotation;
|
|
}
|
|
|
|
public void Drop() {
|
|
Body.isKinematic = false;
|
|
transform.parent = GameObject.FindGameObjectWithTag("World").transform;
|
|
}
|
|
}
|