I’m trying to play an explosion animation on destroy, but the object just gets placed lower than it should., here’s what I have:
private void OnDestroy()
{
Instantiate(enemyExplosion, gameObject.transform.position, new Quaternion(0, 0, 0, 0));
}
The code for the moving object where the explosion should be gets moved withthe following code:
using UnityEngine;
public class BackAndForth : MonoBehaviour
{
// Transforms to act as start and end markers for the journey.
public Transform startMarker;
public Transform endMarker;
// Movement speed in units/sec.
public float speed = 1.0F;
// Time when the movement started.
private float startTime;
// Total distance between the markers.
private float journeyLength;
void Start()
{
// Keep a note of the time the movement started.
startTime = Time.time;
// Calculate the journey length.
journeyLength = Vector3.Distance(startMarker.position, endMarker.position);
}
// Follows the target position like with a spring
void Update()
{
Vector3 x = Vector3.zero;
// Distance moved = time * speed.
float distCovered = (Time.time - startTime) * speed;
// Fraction of journey completed = current distance divided by total distance.
float fracJourney = distCovered / journeyLength;
// Set our position as a fraction of the distance between the markers.
transform.position = Vector3.Lerp(startMarker.position, endMarker.position, fracJourney);
if(transform.position == endMarker.position)
{
x = endMarker.position;
endMarker.position = startMarker.position;
startMarker.position = x;
Start();
transform.Rotate(new Vector3(0, 180, 0));
}
}
}