107 lines
3.1 KiB
C#
107 lines
3.1 KiB
C#
using UnityEngine;
|
|
|
|
namespace Artngame.TEM
|
|
{
|
|
public class Debris : MonoBehaviour
|
|
{
|
|
private Rigidbody debrisRB;
|
|
|
|
private Transform[] skeletonPiecesArray;
|
|
|
|
private Tornado tornadoScript;
|
|
|
|
private void Start()
|
|
{
|
|
debrisRB = GetComponent<Rigidbody>();
|
|
tornadoScript = GameObject.FindGameObjectWithTag("Tornado").GetComponent<Tornado>();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (skeletonPiecesArray == null)
|
|
{
|
|
skeletonPiecesArray = tornadoScript.skeletonPiecesArray;
|
|
}
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
if (skeletonPiecesArray == null)
|
|
{
|
|
return;
|
|
}
|
|
float mass = debrisRB.mass;
|
|
Vector3 position = base.transform.position;
|
|
int num = FindIndex(position);
|
|
Vector3 position2 = skeletonPiecesArray[num].position;
|
|
Vector3 vector = position2;
|
|
if (num != skeletonPiecesArray.Length - 1)
|
|
{
|
|
Vector3 position3 = skeletonPiecesArray[num + 1].position;
|
|
vector = TornadoMath.CalculateProgress(position2, position3, position) * (position3 - position2) + position2;
|
|
}
|
|
float magnitude = (position - vector).magnitude;
|
|
float num2 = 100f;
|
|
float num3 = TornadoData.current.forceCurve.Evaluate(magnitude / num2);
|
|
float num4 = TornadoData.current.forceLiftCurve.Evaluate(position.y / 100f);
|
|
float velocity = 0f;
|
|
if (magnitude < num2)
|
|
{
|
|
velocity = num3 * tornadoScript.tornadoSpinSpeed;
|
|
}
|
|
float num5 = CalculateCentrifulgalForce(velocity, mass, vector, position);
|
|
Vector3 normalized = (position - vector).normalized;
|
|
debrisRB.AddForce(num5 * normalized * num4);
|
|
if (magnitude > 1f && magnitude < num2)
|
|
{
|
|
debrisRB.AddForce(TornadoData.current.suctionForce * -normalized * num3);
|
|
}
|
|
if (magnitude < num2 && position.y < 100f)
|
|
{
|
|
if (num == skeletonPiecesArray.Length - 1)
|
|
{
|
|
debrisRB.AddForce(Vector3.up * TornadoData.current.liftForce * num3);
|
|
}
|
|
else
|
|
{
|
|
Vector3 normalized2 = (skeletonPiecesArray[num + 1].position - skeletonPiecesArray[num].position).normalized;
|
|
debrisRB.AddForce(normalized2 * TornadoData.current.liftForce * num3);
|
|
}
|
|
}
|
|
if (magnitude < num2)
|
|
{
|
|
Vector3 vector2 = vector - position;
|
|
Vector3 vector3 = new Vector3(0f - vector2.z, 0f, vector2.x) / Mathf.Sqrt(vector2.x * vector2.x + vector2.z * vector2.z);
|
|
debrisRB.AddForce(vector3 * TornadoData.current.rotationForce * -1f * num3);
|
|
}
|
|
}
|
|
|
|
private float CalculateCentrifulgalForce(float velocity, float mass, Vector3 centerPos, Vector3 pos)
|
|
{
|
|
float num = (pos.x - centerPos.x) * (pos.x - centerPos.x);
|
|
float num2 = (pos.z - centerPos.z) * (pos.z - centerPos.z);
|
|
return velocity * velocity / Mathf.Sqrt(num + num2);
|
|
}
|
|
|
|
public int FindIndex(Vector3 particlePos)
|
|
{
|
|
int num = 0;
|
|
float num2 = float.PositiveInfinity;
|
|
for (int i = 0; i < skeletonPiecesArray.Length; i++)
|
|
{
|
|
float num3 = Mathf.Abs(skeletonPiecesArray[i].position.y - particlePos.y);
|
|
if (num3 < num2)
|
|
{
|
|
num2 = num3;
|
|
num = i;
|
|
}
|
|
}
|
|
if (particlePos.y < skeletonPiecesArray[num].position.y)
|
|
{
|
|
num--;
|
|
}
|
|
return Mathf.Clamp(num, 0, skeletonPiecesArray.Length - 1);
|
|
}
|
|
}
|
|
}
|