Need help with Descending Orbit Game Object (Plane Crashing)
I have a lot of the code done however I do not now how to get this to work properly. What I am doing is spawning in a cargo plane that I want to crash after so many seconds minimum and max seconds to randomly either begin the plane crash and when it does it goes from flying in a straight path to changing it to a angled descending orbit till it hits the ground with it to randomly decided clockwise or counterclockwise and adjust the game object to be angled at 20 or -20 degrees.
So basically it starts to crash leans to the side in a circular pattern descending to the ground until it hits the ground and dies basically.
I am also trying to display a message saying plane has started to crash which I have working but seems to be stuck in a loop when it happens.
Here is some of the code I already have done - currently it starts to descend and pitch towards the ground however I cant figure out from here how to make it orbit while descending and only queue the message once.
private void FixedUpdate()
{
if (entity.transform.position.y <= -5)
{
if (!isDying)
Die();
return;
}
if (status == FlightStatus.Crashing)
{
crashTimeTaken = crashTimeTaken + UnityEngine.Time.deltaTime;
float single = Mathf.InverseLerp(0, crashTimeToTake, crashTimeTaken);
if (single < 1)
{
currentSpeed = speed - Mathf.Lerp(0, 10, single);
entity.transform.transform.eulerAngles = new Vector3(Mathf.Lerp(0, 15f, single), entity.transform.transform.eulerAngles.y, entity.transform.transform.eulerAngles.z);
}
entity.transform.position = Vector3.MoveTowards(entity.transform.position, entity.transform.position + (entity.transform.forward * 10), currentSpeed * UnityEngine.Time.deltaTime);
Rotate();
}
else
{
entity.transform.position = Vector3.MoveTowards(entity.transform.position, endPos, currentSpeed * UnityEngine.Time.deltaTime);
if (isDropPlane)
{
timeTaken = timeTaken + UnityEngine.Time.deltaTime;
entity.secondsTaken = timeTaken;
float single = Mathf.InverseLerp(0, timeToTake, timeTaken);
if (!hasDropped && single >= 0.5f)
{
hasDropped = true;
if (!Instance.FancyDrop || !isFancyDrop)
{
BaseEntity drop = GameManager.server.CreateEntity(supply, entity.transform.position);
drop.globalBroadcast = true;
drop.Spawn();
}
}
}
}
entity.transform.hasChanged = true;
}
private void RandomCrash()
{
if (countdownTimer == 0 && status == FlightStatus.Crashing)
SendChatMessage("FailingMessage1", FlightNumber);
BeginCrash();
}
private void Rotate()
{
xSpeed = 35;
zSpread = 500;
yOffset = 15;
if(rotateClockwise)
{
float x = -Mathf.Cos(timer) * xSpeed;
float z = Mathf.Sin(timer) * zSpread;
Vector3 pos = new Vector3(x, yOffset, z);
entity.transform.position = pos + centerPoint.position;
}
else
{
float x = Mathf.Cos(timer) * xSpeed;
float z = Mathf.Sin(timer) * zSpread;
Vector3 pos = new Vector3(x, yOffset, z);
entity.transform.position = pos + centerPoint.position;
}
}
public void BeginCrash()
{
endPos = new Vector3(endPos.x, 0, endPos.z);
status = FlightStatus.Crashing;
SmallExplosion();
AddFire();
}
private void BigExplosion()
{
RunEffect(heliExplosion, null, entity.transform.position);
RunEffect(debris, null, entity.transform.position);
}
private void SmallExplosion()
{
RunEffect(c4Explosion, null, entity.transform.position);
RunEffect(debris, null, entity.transform.position);
}
private void AddFire()
{
if (engineFires == null)
{
engineFires = new FireBall[]
{
SpawnFireball(entity, new Vector3(-10, 6, 10), 600),
SpawnFireball(entity, new Vector3(10, 6, 10), 600)
};
}
}
private void Die()
{
if (isDying)
return;
isDying = true;
BigExplosion();
InvokeHandler.Invoke(this, SmallExplosion, 0.25f);
InvokeHandler.Invoke(this, SmallExplosion, 0.5f);
InvokeHandler.Invoke(this, BigExplosion, 1.25f);
InvokeHandler.Invoke(this, SmallExplosion, 1.75f);
InvokeHandler.Invoke(this, BigExplosion, 2.25f);
Destroy(this, 2.5f);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612213255im_/https://answers.unity.com/themes/thub/images/avi.jpg)