Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

unity cone hit test spotlight lits object light on object test

public static class SpotLightCastExtension
{
	public static bool InLineOfSight(this UnityEngine.Light light, Vector3 targetWorldPosition, System.Predicate<RaycastHit> collisionTest = null)
	{
		Transform lightTransform = light.transform;
		return ConeCastTest(lightTransform.position, lightTransform.forward, light.spotAngle, light.range, targetWorldPosition, collisionTest);
	}

	public static bool ConeCastTest(Vector3 sourcePosition, Vector3 forward, float openingAngle, float range, Vector3 testPosition, System.Predicate<RaycastHit> validHit = null)
	{
		Vector3 dirDetector = (testPosition - sourcePosition).normalized;
		bool visible = Vector3.Distance(testPosition, sourcePosition) <= range;

		Vector3 start = sourcePosition + forward * range;
		Vector3 projectedDetectedPositionEnd = sourcePosition + dirDetector * range;
		float detectorConeDistance = Vector3.Distance(start, projectedDetectedPositionEnd);
		// if projected object position towards center of light both at end
		// is further distanced than the openAngle max distance
		if (detectorConeDistance > 4 * openingAngle * Mathf.Deg2Rad)
		{
			visible = false;
		}
		// Cast light towards detector, is it blocked by a collider?
		else if (visible 
		    	&& Physics.Linecast(sourcePosition, projectedDetectedPositionEnd, out RaycastHit hit) 
			    && validHit is not null 
			    && !validHit.Invoke(hit))
		{
			visible = false;
		}
		
		return visible;
	}
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: ASP.NET Core set update clear cache from IMemoryCache (set by Set method of CacheExtensions class) 
Csharp :: CullingGroup 
Csharp :: c# use list as a paramter 
Csharp :: how to colapse all methods visual studio 
Csharp :: c# changimg to one decimal place 
Csharp :: downloading a large file asp boilerplate (abp) 
Csharp :: C# sprint key 
Csharp :: entity framework dynamic search 
Csharp :: ip validation .net core 
Csharp :: linq cross join 
Csharp :: c# check if object can be cast to type 
Csharp :: unity control physics of multiple simulation 
Csharp :: ? in c# 
Csharp :: secret 
Csharp :: how to authorize token when consuming api in c# 
Csharp :: linq pick random element 
Csharp :: Acrylic UWP Title bar C# 
Csharp :: unity screentoworldpoint 
Csharp :: polymorphism in c# 
Csharp :: c# windows service .net core 
Csharp :: swagger skip endpoint .net core 
Csharp :: how to find min of an array in c# 
Csharp :: C# IEnumerable access element at index 
Csharp :: how to backgrund c# red 
Csharp :: flsa itextsharp 
Html :: html 5 default code 
Html :: copyright footer html code 
Html :: ion icon size small 
Html :: jquery $ is not defined 
Html :: no cache html 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =