using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class EarthquakeSimulator : MonoBehaviour
{
public float duration = 5.0f; // Duration of the earthquake
public float magnitude = 0.1f; // Magnitude of the shaking
public float delayBeforeStart = 2.0f; // Delay before the earthquake starts
// Environmental interactions
public List<Rigidbody> objectsToFall = new List<Rigidbody>(); // List of objects to fall during the earthquake
public float forceMagnitude = 10f; // Magnitude of the force applied to objects
public float torqueMagnitude = 5f; // Magnitude of the torque applied to objects
public float minFallDelay = 0.5f; // Minimum time before an object starts falling
public float maxFallDelay = 2.0f; // Maximum time before an object starts falling
private bool isShaking = false;
private Vector3 originalPosition;
void Start()
{
originalPosition = transform.position; // Store the original position
// Initialize objects to be unaffected by gravity and forces
foreach (Rigidbody rb in objectsToFall)
{
rb.useGravity = false; // Disable gravity
rb.velocity = Vector3.zero; // Ensure no movement
rb.angularVelocity = Vector3.zero; // Ensure no rotation
rb.Sleep(); // Put the rigidbody to sleep
// Freeze position and rotation to prevent any movement
rb.constraints = RigidbodyConstraints.FreezePosition | RigidbodyConstraints.FreezeRotation;
}
// Start the earthquake after a delay
StartCoroutine(StartEarthquakeAfterDelay());
}
private IEnumerator StartEarthquakeAfterDelay()
{
// Wait for the specified delay
yield return new WaitForSeconds(delayBeforeStart);
// Start the earthquake shaking and objects falling
StartCoroutine(Shake());
StartCoroutine(FallObjectsOverTime());
}
private IEnumerator Shake()
{
if (isShaking)
yield break;
isShaking = true;
float elapsed = 0.0f;
Vector3 originalPosition = transform.position; // Cache the original position
while (elapsed < duration)
{
float x = Random.Range(-magnitude, magnitude);
float y = Random.Range(-magnitude, magnitude);
float z = Random.Range(-magnitude, magnitude);
transform.position = originalPosition + new Vector3(x, y, z);
elapsed += Time.deltaTime;
yield return null;
}
// Reset the position of the parent object after shaking
transform.position = originalPosition;
isShaking = false;
}
private IEnumerator FallObjectsOverTime()
{
foreach (Rigidbody rb in objectsToFall)
{
// Random delay before this object starts falling
yield return new WaitForSeconds(Random.Range(minFallDelay, maxFallDelay));
// Unfreeze the object to allow it to fall and move
rb.constraints = RigidbodyConstraints.None;
// Wake up the rigidbody to make it responsive to gravity and forces
rb.WakeUp();
rb.useGravity = true; // Enable gravity
// Apply a random force to simulate the shaking of the earthquake
Vector3 force = new Vector3(
Random.Range(-forceMagnitude, forceMagnitude),
Random.Range(-forceMagnitude, forceMagnitude),
Random.Range(-forceMagnitude, forceMagnitude)
);
rb.AddForce(force, ForceMode.Impulse);
// Apply random torque to simulate objects tipping over
Vector3 torque = new Vector3(
Random.Range(-torqueMagnitude, torqueMagnitude),
Random.Range(-torqueMagnitude, torqueMagnitude),
Random.Range(-torqueMagnitude, torqueMagnitude)
);
rb.AddTorque(torque, ForceMode.Impulse);
// Optional: wait a short time before allowing the next object to fall
yield return new WaitForSeconds(0.1f);
}
}
}
what i have here is a earthquake effect shaking script that is inserted in the main camera of my scene it works as intended in the playmode but it is not working when built in mobile
do i need specific build settings for my scripts? what should i do?