Advertisement
DugganSC

Untitled

Jun 5th, 2024
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3.  
  4. public class SimulatedScene : MonoBehaviour
  5. {
  6. private Scene _simulatedScene;
  7. private PhysicsScene _physicsScene;
  8.  
  9. // A reference to a collection that holds the items we plan to simulate
  10. [SerializeField] private Transform _targetParent;
  11.  
  12. // Start is called before the first frame update
  13. void Start()
  14. {
  15. CreateSimulatedPhysicsScene();
  16. }
  17.  
  18. private void CreateSimulatedPhysicsScene()
  19. {
  20. _simulatedScene = SceneManager.CreateScene("Physics Simulation Scene", new CreateSceneParameters(LocalPhysicsMode.Physics3D));
  21. _physicsScene = _simulatedScene.GetPhysicsScene();
  22.  
  23. foreach (Transform obstacle in _targetParent)
  24. {
  25. Debug.Log($"Considering {obstacle.name} with tag {obstacle.tag}");
  26. if (obstacle.CompareTag("Obstacle"))
  27. {
  28. GameObject simulatedObject = Instantiate(obstacle.gameObject, obstacle.position, obstacle.rotation);
  29. Renderer renderer = obstacle.GetComponent<Renderer>();
  30. if (renderer != null )
  31. {
  32. renderer.enabled = false;
  33. }
  34.  
  35. SceneManager.MoveGameObjectToScene(simulatedObject, _simulatedScene);
  36. }
  37. }
  38. }
  39.  
  40. [SerializeField] LineRenderer _lineRenderer;
  41. [SerializeField] private int _maxPhysicsInteractions = 100;
  42.  
  43. public void SimulatedTrajectory(AirmailPackage package, Vector3 pos, Vector3 velocity)
  44. {
  45. AirmailPackage instance = Instantiate(package, pos, Quaternion.identity);
  46.  
  47. if (instance.TryGetComponent<Renderer>(out Renderer renderer))
  48. {
  49. renderer.enabled = false;
  50. }
  51.  
  52. SceneManager.MoveGameObjectToScene(instance.gameObject, _simulatedScene);
  53.  
  54. instance.ApplyImpulse(velocity);
  55.  
  56. // Set the points for the line renderer for the simulated projectiles
  57. _lineRenderer.positionCount = _maxPhysicsInteractions;
  58.  
  59. for(int i=0; i < _maxPhysicsInteractions; i++)
  60. {
  61. _lineRenderer.SetPosition(i, instance.transform.position);
  62. _physicsScene.Simulate(Time.fixedDeltaTime * 3);
  63. }
  64.  
  65. Destroy(instance.gameObject);
  66. }
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement