Skip to content

Instantly share code, notes, and snippets.

@runewake2
Created January 16, 2018 05:37
Show Gist options
  • Save runewake2/ff564a39ba539a9bb1a71321c9f2723a to your computer and use it in GitHub Desktop.
Save runewake2/ff564a39ba539a9bb1a71321c9f2723a to your computer and use it in GitHub Desktop.
Demonstrates a projectile reflecting in 3D Space a variable number of times in Unity 3D
using UnityEditor;
using UnityEngine;
/*
* Projectile reflection demonstration in Unity 3D
*
* Demonstrates a projectile reflecting in 3D space a variable number of times.
* Reflections are calculated using Raycast's and Vector3.Reflect
*
* Developed on World of Zero: https://youtu.be/GttdLYKEJAM
*/
public class ProjectileReflectionEmitterUnityNative : MonoBehaviour
{
public int maxReflectionCount = 5;
public float maxStepDistance = 200;
void OnDrawGizmos()
{
Handles.color = Color.red;
Handles.ArrowHandleCap(0, this.transform.position + this.transform.forward * 0.25f, this.transform.rotation, 0.5f, EventType.Repaint);
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(this.transform.position, 0.25f);
DrawPredictedReflectionPattern(this.transform.position + this.transform.forward * 0.75f, this.transform.forward, maxReflectionCount);
}
private void DrawPredictedReflectionPattern(Vector3 position, Vector3 direction, int reflectionsRemaining)
{
if (reflectionsRemaining == 0) {
return;
}
Vector3 startingPosition = position;
Ray ray = new Ray(position, direction);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, maxStepDistance))
{
direction = Vector3.Reflect(direction, hit.normal);
position = hit.point;
}
else
{
position += direction * maxStepDistance;
}
Gizmos.color = Color.yellow;
Gizmos.DrawLine(startingPosition, position);
DrawPredictedReflectionPattern(position, direction, reflectionsRemaining - 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment