Created
November 24, 2021 11:49
-
-
Save RoeiRubach/0f0437e404d8b9fac0b592464bd68562 to your computer and use it in GitHub Desktop.
[Line projection] *not mine. credit -> https://github.com/Matthew-J-Spencer/Trajectory-Line-Unity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections.Generic; | |
| using UnityEngine; | |
| using UnityEngine.SceneManagement; | |
| [RequireComponent(typeof(LineRenderer))] | |
| public class LineProjection : MonoBehaviour | |
| { | |
| [SerializeField] private Transform _obstaclesParent; | |
| [SerializeField] private int _maxPhysicsFrameIterations = 100; | |
| private LineRenderer _line; | |
| private Scene _simulationScene; | |
| private PhysicsScene _physicsScene; | |
| private readonly Dictionary<Transform, Transform> _spawnedObjects = new Dictionary<Transform, Transform>(); | |
| private void Start() | |
| { | |
| _line = GetComponent<LineRenderer>(); | |
| CreatePhysicsScene(); | |
| } | |
| private void CreatePhysicsScene() | |
| { | |
| _simulationScene = SceneManager.CreateScene("Simulation", new CreateSceneParameters(LocalPhysicsMode.Physics3D)); | |
| _physicsScene = _simulationScene.GetPhysicsScene(); | |
| foreach (Transform obstacle in _obstaclesParent) | |
| { | |
| var ghostObject = Instantiate(obstacle.gameObject, obstacle.position, obstacle.rotation); | |
| ghostObject.GetComponent<Renderer>().enabled = false; | |
| SceneManager.MoveGameObjectToScene(ghostObject, _simulationScene); | |
| if (!ghostObject.isStatic) _spawnedObjects.Add(obstacle, ghostObject.transform); | |
| } | |
| } | |
| private void Update() | |
| { | |
| foreach (var item in _spawnedObjects) | |
| { | |
| item.Value.position = item.Key.position; | |
| item.Value.rotation = item.Key.rotation; | |
| } | |
| } | |
| public void SimulateTrajectory(SomeObjectWithPhysics someObjectWithPhysics, Vector3 position, Vector3 velocity) | |
| { | |
| var ghostObject = Instantiate(someObjectWithPhysics, position, Quaternion.identity); | |
| SceneManager.MoveGameObjectToScene(ghostObject.gameObject, _simulationScene); | |
| ghostObject.Initialize(velocity, true); | |
| _line.positionCount = _maxPhysicsFrameIterations; | |
| for (var i = 0; i < _maxPhysicsFrameIterations; i++) | |
| { | |
| _physicsScene.Simulate(Time.fixedDeltaTime); | |
| _line.SetPosition(i, ghostObject.transform.position); | |
| } | |
| Destroy(ghostObject.gameObject); | |
| } | |
| } | |
| public class SomeObjectWithPhysics : MonoBehaviour | |
| { | |
| [SerializeField] private Rigidbody _rigidbody; | |
| private bool _isGhost; | |
| public void Initialize(Vector3 velocity, bool isGhost) | |
| { | |
| _isGhost = isGhost; | |
| _rigidbody.AddForce(velocity, ForceMode.Impulse); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment