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; } }
private Vector3 getVelocityForTarget(Vector3 position, Vector3 target, float initialAngle) { float gravity = GameSettings.gravity; // Selected angle in radians float angle = initialAngle * Mathf.Deg2Rad; // Positions of this object and the target on the same plane Vector3 planarTarget = new Vector3(target.x, 0, target.z); Vector3 planarPostion = new Vector3(position.x, 0, position.z); // Planar distance between objects float distance = Vector3.Distance(planarTarget, planarPostion); // Distance along the y axis between objects float yOffset = position.y - target.y; float speed = ProjectileUtils.LaunchSpeed(distance, yOffset, gravity, angle); Vector3 velocity = new Vector3(0, speed * Mathf.Sin(angle), speed * Mathf.Cos(angle)); float angleBetweenObjects = Vector3.SignedAngle(Vector3.forward, planarTarget - planarPostion, Vector3.up); timeOfFlight = ProjectileUtils.TimeOfFlight(speed, angle, yOffset, gravity); return(Quaternion.AngleAxis(angleBetweenObjects, Vector3.up) * velocity); }