/// <summary> /// Calculates grenade position for every frame and stores it inside the buffer. Returns number of positions calculated. /// </summary> public static int Calculate(Vector3 start, Vector3 target, float maxVelocity, GrenadeDescription desc, Vector3[] buffer, float step = 0.05f) { if (buffer.Length == 0) { return(0); } buffer[0] = start; var count = 1; var currentVelocity = GetVelocity(start, target, maxVelocity, desc.Gravity); var position = start; var time = 0f; Profiler.BeginSample("Grenade path"); while (count < buffer.Length && time < desc.Duration && currentVelocity.magnitude > 0.1f) { Step(ref position, ref currentVelocity, step, desc.Gravity, desc.Bounciness); time += step; buffer[count++] = position; } Profiler.EndSample(); return(count); }
/// <summary> /// Calculates grenade position for every frame and stores it inside the buffer. Returns number of positions calculated. /// </summary> public static int Calculate(Vector3 start, float horizontalAngle, float angleInDegrees, float velocity, GrenadeDescription desc, Vector3[] buffer, float step = 0.05f) { if (buffer.Length == 0) { return(0); } buffer[0] = start; var count = 1; var horizontal = Util.HorizontalVector(horizontalAngle); var distance = horizontal.magnitude; var angle = angleInDegrees * Mathf.Deg2Rad; var currentVelocity = (horizontal.normalized * Mathf.Cos(angle) + Vector3.up * Mathf.Sin(angle)) * velocity; var position = start; var time = 0f; Profiler.BeginSample("Grenade path"); while (count < buffer.Length && time < desc.Duration && currentVelocity.magnitude > 0.1f) { Step(ref position, ref currentVelocity, step, desc.Gravity, desc.Bounciness); time += step; buffer[count++] = position; } Profiler.EndSample(); return(count); }
public static Vector3 GetVelocity(Vector3 start, Vector3 target, float maxVelocity, GrenadeDescription desc) { var horizontal = new Vector3(target.x, 0, target.z) - new Vector3(start.x, 0, start.z); var height = target.y - start.y; var distance = horizontal.magnitude; float angle = Mathf.Deg2Rad * 45; float velocity = maxVelocity; if (getAngle(height, distance, velocity, -desc.Gravity, ref angle)) { if (angle > Mathf.Deg2Rad * 45) { angle = Mathf.Deg2Rad * 45; velocity = getVelocity(height, distance, -desc.Gravity, angle); } } return((horizontal.normalized * Mathf.Cos(angle) + Vector3.up * Mathf.Sin(angle)) * velocity); }