Skip to content

Instantly share code, notes, and snippets.

@kurtdekker
Created July 1, 2021 22:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurtdekker/2435ff2acc5756f8c186c60700d88f2e to your computer and use it in GitHub Desktop.
Save kurtdekker/2435ff2acc5756f8c186c60700d88f2e to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// @kurtdekker random spawn utilities from my Jetpack Kurt game (And other games)
public static class SpawnUtilities
{
public static bool ProgressiveLiftAndCastDown( ref Vector3 position, out Vector3 normal)
{
Vector3 pos = position;
normal = Vector3.up;
bool hitGround = false;
float castDistance = 0.0f;
for (int tries = 0; tries < 250; tries++)
{
pos += Vector3.up * 1.0f;
castDistance += 2.0f;
Ray ray = new Ray( pos, Vector3.down);
RaycastHit rch;
if (Physics.Raycast( ray, out rch, castDistance))
{
hitGround = true;
position = rch.point;
normal = rch.normal;
var s = "SpawnUtilities.ProgressiveLiftAndCastDown:hit '" + rch.collider.name + "'";
s += System.String.Format( ": pos:{0} norm:{1}", position, normal);
Debug.Log( s);
break;
}
}
return hitGround;
}
public static bool ProgressiveLiftAndCastDown( ref Vector3 position)
{
Vector3 normalDummy = Vector3.zero;
bool hitGround = ProgressiveLiftAndCastDown( ref position, out normalDummy);
return hitGround;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment