initial commit
This commit is contained in:
145
Assets/Scripts/MesosphereEffects.cs
Normal file
145
Assets/Scripts/MesosphereEffects.cs
Normal file
@@ -0,0 +1,145 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class MesosphereEffects : MonoBehaviour
|
||||
{
|
||||
[Header("Àñòåðî¿äè")]
|
||||
public GameObject[] asteroidPrefabs;
|
||||
public GameObject explosionPrefab;
|
||||
public GameObject firePrefab;
|
||||
public Material craterMaterial;
|
||||
|
||||
[Header("Íàëàøòóâàííÿ")]
|
||||
public float planetRadius = 6.371f;
|
||||
public float spawnRadius = 60f;
|
||||
public float mesosphereRadius = 10f;
|
||||
[Range(0f, 1f)]
|
||||
public float mesosphereLevel = 1f;
|
||||
|
||||
[Header("×àñòîòà")]
|
||||
public float spawnInterval = 3f;
|
||||
public Transform earthTransform;
|
||||
|
||||
private float spawnTimer = 0f;
|
||||
private List<GameObject> craters = new List<GameObject>();
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (!Application.isPlaying) return;
|
||||
|
||||
spawnTimer += Time.deltaTime;
|
||||
float adjustedInterval = Mathf.Lerp(0.5f, spawnInterval, mesosphereLevel);
|
||||
|
||||
if (spawnTimer >= adjustedInterval)
|
||||
{
|
||||
spawnTimer = 0f;
|
||||
SpawnAsteroid();
|
||||
}
|
||||
}
|
||||
|
||||
void Awake()
|
||||
{
|
||||
enabled = false;
|
||||
}
|
||||
|
||||
void SpawnAsteroid()
|
||||
{
|
||||
if (asteroidPrefabs == null || asteroidPrefabs.Length == 0) return;
|
||||
|
||||
Vector3 direction = Random.onUnitSphere;
|
||||
Vector3 spawnPos = transform.position + direction * spawnRadius;
|
||||
Vector3 flyDir = -direction;
|
||||
|
||||
GameObject prefab = asteroidPrefabs[Random.Range(0, asteroidPrefabs.Length)];
|
||||
GameObject asteroid = Instantiate(prefab, spawnPos, Random.rotation);
|
||||
asteroid.transform.localScale = Vector3.one * Random.Range(0.3f, 0.8f);
|
||||
|
||||
StartCoroutine(MoveAsteroid(asteroid, spawnPos, flyDir));
|
||||
}
|
||||
|
||||
IEnumerator MoveAsteroid(GameObject asteroid, Vector3 from, Vector3 flyDir)
|
||||
{
|
||||
float speed = Random.Range(3f, 6f);
|
||||
bool passedMesosphere = false;
|
||||
|
||||
while (asteroid != null)
|
||||
{
|
||||
asteroid.transform.position += flyDir * speed * Time.deltaTime;
|
||||
asteroid.transform.Rotate(Vector3.one * 50f * Time.deltaTime);
|
||||
|
||||
float distFromCenter = Vector3.Distance(asteroid.transform.position, transform.position);
|
||||
|
||||
if (!passedMesosphere && distFromCenter <= mesosphereRadius)
|
||||
{
|
||||
float burnChance = Mathf.Clamp01((mesosphereLevel - 0.3f) / 0.7f);
|
||||
if (Random.value < burnChance)
|
||||
{
|
||||
if (firePrefab != null)
|
||||
{
|
||||
GameObject burn = Instantiate(firePrefab, asteroid.transform.position, Quaternion.identity);
|
||||
burn.transform.localScale = Vector3.one * 0.2f;
|
||||
Destroy(burn, 3f);
|
||||
}
|
||||
Destroy(asteroid);
|
||||
yield break;
|
||||
}
|
||||
passedMesosphere = true;
|
||||
}
|
||||
|
||||
if (distFromCenter <= planetRadius + 0.3f)
|
||||
{
|
||||
Vector3 hitPos = transform.position + (-flyDir) * planetRadius;
|
||||
|
||||
if (explosionPrefab != null)
|
||||
{
|
||||
GameObject exp = Instantiate(explosionPrefab, hitPos, Quaternion.identity);
|
||||
exp.transform.localScale = Vector3.one * Random.Range(0.3f, 0.6f);
|
||||
Destroy(exp, 4f);
|
||||
}
|
||||
|
||||
SpawnCrater(hitPos, -flyDir);
|
||||
Destroy(asteroid);
|
||||
yield break;
|
||||
}
|
||||
|
||||
if (distFromCenter > spawnRadius * 2f)
|
||||
{
|
||||
Destroy(asteroid);
|
||||
yield break;
|
||||
}
|
||||
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
|
||||
void SpawnCrater(Vector3 pos, Vector3 normal)
|
||||
{
|
||||
if (craterMaterial == null) return;
|
||||
|
||||
Vector3 center = earthTransform != null ? earthTransform.position : transform.position;
|
||||
Vector3 dirFromCenter = (pos - center).normalized;
|
||||
Vector3 surfacePos = center + dirFromCenter * planetRadius;
|
||||
|
||||
GameObject crater = GameObject.CreatePrimitive(PrimitiveType.Sphere);
|
||||
crater.name = "Crater";
|
||||
crater.transform.position = surfacePos;
|
||||
crater.transform.rotation = Quaternion.FromToRotation(Vector3.up, dirFromCenter);
|
||||
|
||||
float size = Random.Range(0.2f, 0.6f);
|
||||
crater.transform.localScale = new Vector3(size, 0.03f, size);
|
||||
|
||||
Destroy(crater.GetComponent<Collider>());
|
||||
|
||||
Renderer r = crater.GetComponent<Renderer>();
|
||||
r.material = craterMaterial;
|
||||
r.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
|
||||
|
||||
craters.Add(crater);
|
||||
}
|
||||
|
||||
public void SetMesosphereLevel(float level)
|
||||
{
|
||||
mesosphereLevel = level;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user