Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.SceneManagement;
- public class SimulatedScene : MonoBehaviour
- {
- private Scene _simulatedScene;
- private PhysicsScene _physicsScene;
- // A reference to a collection that holds the items we plan to simulate
- [SerializeField] private Transform _targetParent;
- // Start is called before the first frame update
- void Start()
- {
- CreateSimulatedPhysicsScene();
- }
- private void CreateSimulatedPhysicsScene()
- {
- _simulatedScene = SceneManager.CreateScene("Physics Simulation Scene", new CreateSceneParameters(LocalPhysicsMode.Physics3D));
- _physicsScene = _simulatedScene.GetPhysicsScene();
- foreach (Transform obstacle in _targetParent)
- {
- Debug.Log($"Considering {obstacle.name} with tag {obstacle.tag}");
- if (obstacle.CompareTag("Obstacle"))
- {
- GameObject simulatedObject = Instantiate(obstacle.gameObject, obstacle.position, obstacle.rotation);
- Renderer renderer = obstacle.GetComponent<Renderer>();
- if (renderer != null )
- {
- renderer.enabled = false;
- }
- SceneManager.MoveGameObjectToScene(simulatedObject, _simulatedScene);
- }
- }
- }
- [SerializeField] LineRenderer _lineRenderer;
- [SerializeField] private int _maxPhysicsInteractions = 100;
- public void SimulatedTrajectory(AirmailPackage package, Vector3 pos, Vector3 velocity)
- {
- AirmailPackage instance = Instantiate(package, pos, Quaternion.identity);
- if (instance.TryGetComponent<Renderer>(out Renderer renderer))
- {
- renderer.enabled = false;
- }
- SceneManager.MoveGameObjectToScene(instance.gameObject, _simulatedScene);
- instance.ApplyImpulse(velocity);
- // Set the points for the line renderer for the simulated projectiles
- _lineRenderer.positionCount = _maxPhysicsInteractions;
- for(int i=0; i < _maxPhysicsInteractions; i++)
- {
- _lineRenderer.SetPosition(i, instance.transform.position);
- _physicsScene.Simulate(Time.fixedDeltaTime * 3);
- }
- Destroy(instance.gameObject);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement