Created
February 27, 2018 05:38
-
-
Save MartinHarding/c5218a6ed2d8894aae8763d873a8d582 to your computer and use it in GitHub Desktop.
This file contains 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
// Launch Arc Renderer for Unity 3D from this Tutorial (with minor fixes): | |
// https://www.youtube.com/watch?v=iLlWirdxass | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
[RequireComponent (typeof(LineRenderer))] | |
public class LaunchArcRenderer : MonoBehaviour { | |
LineRenderer lr; | |
public float velocity = 10f; | |
public float angle = 45f; | |
[Range(1, 100)] public int resolution = 10; | |
float gravity; | |
float radianAngle; | |
void Awake() { | |
lr = GetComponent<LineRenderer>(); | |
gravity = Mathf.Abs(Physics2D.gravity.y); | |
} | |
void OnValidate() { | |
if (lr != null && Application.isPlaying) { | |
RenderArc(); | |
} | |
} | |
void Start() { | |
RenderArc(); | |
} | |
void RenderArc() { | |
lr.positionCount = resolution + 1; | |
lr.SetPositions(CalcArcArray()); | |
} | |
Vector3[] CalcArcArray() { | |
Vector3[] arcArray = new Vector3[resolution + 1]; | |
radianAngle = Mathf.Deg2Rad * angle; | |
float maxDistance = (velocity * velocity * Mathf.Sin(2 * radianAngle)) / gravity; | |
for (int i = 0; i <= resolution; i++) { | |
float t = (float)i / (float)resolution; | |
arcArray[i] = CalculateArcPoint(t, maxDistance); | |
} | |
return arcArray; | |
} | |
Vector3 CalculateArcPoint(float progressedDist, float maxDistance) { | |
float x = progressedDist * maxDistance; | |
float y = x * Mathf.Tan(radianAngle) - ((gravity * x * x) / (2 * velocity * velocity * Mathf.Cos(radianAngle) * Mathf.Cos(radianAngle))); | |
return new Vector3(x, y, 0f); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment