├── README.md ├── .gitignore └── TrajectoryController /README.md: -------------------------------------------------------------------------------- 1 | # Unity-3D-Trajectory-Prediction 2 | 3 | Free code, use it as you want!!! 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # This .gitignore file should be placed at the root of your Unity project directory 2 | # 3 | # Get latest from https://github.com/github/gitignore/blob/master/Unity.gitignore 4 | # 5 | /[Ll]ibrary/ 6 | /[Tt]emp/ 7 | /[Oo]bj/ 8 | /[Bb]uild/ 9 | /[Bb]uilds/ 10 | /[Ll]ogs/ 11 | /[Mm]emoryCaptures/ 12 | 13 | # Asset meta data should only be ignored when the corresponding asset is also ignored 14 | !/[Aa]ssets/**/*.meta 15 | 16 | # Uncomment this line if you wish to ignore the asset store tools plugin 17 | # /[Aa]ssets/AssetStoreTools* 18 | 19 | # Autogenerated Jetbrains Rider plugin 20 | [Aa]ssets/Plugins/Editor/JetBrains* 21 | 22 | # Visual Studio cache directory 23 | .vs/ 24 | 25 | # Gradle cache directory 26 | .gradle/ 27 | 28 | # Autogenerated VS/MD/Consulo solution and project files 29 | ExportedObj/ 30 | .consulo/ 31 | *.csproj 32 | *.unityproj 33 | *.sln 34 | *.suo 35 | *.tmp 36 | *.user 37 | *.userprefs 38 | *.pidb 39 | *.booproj 40 | *.svd 41 | *.pdb 42 | *.mdb 43 | *.opendb 44 | *.VC.db 45 | 46 | # Unity3D generated meta files 47 | *.pidb.meta 48 | *.pdb.meta 49 | *.mdb.meta 50 | 51 | # Unity3D generated file on crash reports 52 | sysinfo.txt 53 | 54 | # Builds 55 | *.apk 56 | *.unitypackage 57 | 58 | # Crashlytics generated file 59 | crashlytics-build.properties 60 | 61 | -------------------------------------------------------------------------------- /TrajectoryController: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class TrajectoryController : MonoBehaviour 6 | { 7 | [Header("Line renderer veriables")] 8 | public LineRenderer line; 9 | [Range(2, 30)] 10 | public int resolution; 11 | 12 | [Header("Formula variables")] 13 | public Vector3 velocity; 14 | public float yLimit; 15 | private float g; 16 | 17 | [Header("Linecast variables")] 18 | [Range(2, 30)] 19 | public int linecastResolution; 20 | public LayerMask canHit; 21 | 22 | private void Start() 23 | { 24 | g = Mathf.Abs(Physics.gravity.y); 25 | } 26 | 27 | private void Update() 28 | { 29 | RenderArc(); 30 | } 31 | 32 | private void RenderArc() 33 | { 34 | line.positionCount = resolution + 1; 35 | line.SetPositions(CalculateLineArray()); 36 | } 37 | 38 | private Vector3[] CalculateLineArray() 39 | { 40 | Vector3[] lineArray = new Vector3[resolution + 1]; 41 | 42 | var lowestTimeValueX = MaxTimeX() / resolution; 43 | var lowestTimeValueZ = MaxTimeZ() / resolution; 44 | var lowestTimeValue = lowestTimeValueX > lowestTimeValueZ ? lowestTimeValueZ : lowestTimeValueX; 45 | 46 | for (int i = 0; i < lineArray.Length; i++) 47 | { 48 | var t = lowestTimeValue * i; 49 | lineArray[i] = CalculateLinePoint(t); 50 | } 51 | 52 | return lineArray; 53 | } 54 | 55 | private Vector3 HitPosition() 56 | { 57 | var lowestTimeValue = MaxTimeY() / linecastResolution; 58 | 59 | for (int i = 0; i < linecastResolution + 1; i++) 60 | { 61 | RaycastHit rayHit; 62 | 63 | var t = lowestTimeValue * i; 64 | var tt = lowestTimeValue * (i + 1); 65 | 66 | if (Physics.Linecast(CalculateLinePoint(t), CalculateLinePoint(tt), out rayHit, canHit)) 67 | return rayHit.point; 68 | } 69 | 70 | return CalculateLinePoint(MaxTimeY()); 71 | } 72 | 73 | private Vector3 CalculateLinePoint(float t) 74 | { 75 | float x = velocity.x * t; 76 | float z = velocity.z * t; 77 | float y = (velocity.y * t) - (g * Mathf.Pow(t, 2) / 2); 78 | return new Vector3(x + transform.position.x, y + transform.position.y, z + transform.position.z); 79 | } 80 | 81 | private float MaxTimeY() 82 | { 83 | var v = velocity.y; 84 | var vv = v * v; 85 | 86 | var t = (v + Mathf.Sqrt(vv + 2 * g * (transform.position.y - yLimit))) / g; 87 | return t; 88 | } 89 | 90 | private float MaxTimeX() 91 | { 92 | if (IsValueAlmostZero(velocity.x)) 93 | SetValueToAlmostZero(ref velocity.x); 94 | 95 | var x = velocity.x; 96 | 97 | var t = (HitPosition().x - transform.position.x) / x; 98 | return t; 99 | } 100 | 101 | private float MaxTimeZ() 102 | { 103 | if (IsValueAlmostZero(velocity.z)) 104 | SetValueToAlmostZero(ref velocity.z); 105 | 106 | var z = velocity.z; 107 | 108 | var t = (HitPosition().z - transform.position.z) / z; 109 | return t; 110 | } 111 | 112 | private bool IsValueAlmostZero(float value) 113 | { 114 | return value < 0.0001f && value > -0.0001f; 115 | } 116 | 117 | private void SetValueToAlmostZero(ref float value) 118 | { 119 | value = 0.0001f; 120 | } 121 | 122 | public void SetVelocity(Vector3 velocity) 123 | { 124 | this.velocity = velocity; 125 | } 126 | } 127 | --------------------------------------------------------------------------------