ProjectileLauncher.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | using UnityEngine; public class ProjectileLauncher : MonoBehaviour { public Transform launchPoint; public GameObject projectile; public float launchSpeed = 10f; [Header( "****Trajectory Display****" )] public LineRenderer lineRenderer; public int linePoints = 175; public float timeIntervalInPoints = 0.01f; void Update() { if (lineRenderer != null ) { if (Input.GetMouseButton(1)) { DrawTrajectory(); lineRenderer.enabled = true ; } else lineRenderer.enabled = false ; } if (Input.GetMouseButtonDown(0)) { var _projectile = Instantiate(projectile, launchPoint.position, launchPoint.rotation); _projectile.GetComponent<Rigidbody>().velocity = launchSpeed * launchPoint.up; } } void DrawTrajectory() { Vector3 origin = launchPoint.position; Vector3 startVelocity = launchSpeed * launchPoint.up; lineRenderer.positionCount = linePoints; float time = 0; for ( int i = 0; i < linePoints; i++) { // s = u*t + 1/2*g*t*t var x = (startVelocity.x * time) + (Physics.gravity.x / 2 * time * time); var y = (startVelocity.y * time) + (Physics.gravity.y / 2 * time * time); Vector3 point = new Vector3(x, y, 0); lineRenderer.SetPosition(i, origin + point); time += timeIntervalInPoints; } } } |