HeavenStudio/Assets/Scripts/Games/AirRally/Cloud.cs
minenice55 76702f110a
Air Rally Fixes (#252)
* fix clouds

* add hit effects

fix jank with shuttle movement

* make near miss effect snap to player hit position

* add easing to forthington movement

* try different shuttle rotation method

* Revert "try different shuttle rotation method"

This reverts commit 96c0d56fd6.

* try other rotation method
2023-02-01 01:02:59 -05:00

71 lines
2.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace HeavenStudio.Games.Scripts_AirRally
{
public class Cloud : MonoBehaviour
{
[SerializeField] Sprite[] sprites;
[SerializeField] Vector3 spawnRange;
[SerializeField] float baseSpeed = 1f;
[SerializeField] float fadeDist = 10f;
[SerializeField] float lifeTime = 6f;
[SerializeField] float fadeInTime = 0.25f;
Camera cam;
SpriteRenderer spriteRenderer;
float time = 0f;
public bool isWorking = false;
public void Init()
{
cam = GameCamera.GetCamera();
spriteRenderer = GetComponent<SpriteRenderer>();
spriteRenderer.color = new Color(1, 1, 1, 0);
}
// Update is called once per frame
void Update()
{
time += Time.deltaTime;
transform.position += Vector3.forward * -baseSpeed * Time.deltaTime;
// get distance to camera
float dist = Vector3.Distance(cam.transform.position, transform.position);
if (dist <= fadeDist)
{
spriteRenderer.color = new Color(1, 1, 1, Mathf.Clamp01(dist / fadeDist));
}
else if (time < fadeInTime)
{
spriteRenderer.color = new Color(1, 1, 1, Mathf.Clamp01(time/fadeInTime));
}
if (time > lifeTime)
{
isWorking = false;
gameObject.SetActive(false);
spriteRenderer.color = new Color(1, 1, 1, 0);
}
}
public void StartCloud(Vector3 origin, bool prebake)
{
isWorking = true;
time = 0f;
gameObject.SetActive(true);
spriteRenderer.sprite = sprites[Random.Range(0, sprites.Length)];
transform.position = origin;
transform.position += new Vector3(Random.Range(-spawnRange.x, spawnRange.x), Random.Range(-spawnRange.y, spawnRange.y), Random.Range(-spawnRange.z, spawnRange.z));
if (prebake)
{
time = Random.Range(0, lifeTime);
transform.position += Vector3.forward * -baseSpeed * time;
float dist = Vector3.Distance(cam.transform.position, transform.position);
spriteRenderer.color = new Color(1, 1, 1, Mathf.Clamp01(dist / fadeDist));
}
}
}
}