-
Notifications
You must be signed in to change notification settings - Fork 0
/
DebugProjectileArc.cs
56 lines (37 loc) · 1.72 KB
/
DebugProjectileArc.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
50
51
52
53
54
55
56
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
public class DebugProjectileArc : MonoBehaviour {
public float angle;
public Transform target;
private float speed;
private float distance;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnDrawGizmos()
{ // Positions of this object and the target on the same plane
Vector3 planarTarget = new Vector3(target.position.x, 0, target.position.z);
Vector3 planarPostion = new Vector3(transform.position.x, 0, transform.position.z);
// Planar distance between objects
distance = Vector3.Distance(planarTarget, planarPostion);
// Distance along the y axis between objects
float yOffset = transform.position.y - target.position.y;
speed = ProjectileUtils.LaunchSpeed(distance, yOffset, 9.8f, angle * Mathf.Deg2Rad);
Vector3 velocity = new Vector3(0, speed * Mathf.Sin(angle * Mathf.Deg2Rad), speed * Mathf.Cos(angle * Mathf.Deg2Rad));
float angleBetweenObjects = Vector3.SignedAngle(Vector3.forward, planarTarget - planarPostion, Vector3.up);
Vector3 finalVelocity = Quaternion.AngleAxis(angleBetweenObjects, Vector3.up) * velocity;
Gizmos.DrawLine(transform.position, transform.position + finalVelocity);
Vector3 lastPosition = transform.position;
for (float t = 0.0f; t < 10.0f; t += 0.05f)
{
Vector3 nextPosition = transform.position + finalVelocity * t + Vector3.down * (9.8f / 2.0f) * t * t;
Gizmos.DrawLine(lastPosition, nextPosition);
lastPosition = nextPosition;
}
}
}