I am making a stealth game and i was wondering if it is possible to make and object be detected when it enters a lights spot angle? I have a spot light and I want the light to detected the player if they go into the spot lights spot angle? I put a raycats on the light but it doesn’t work like i want it to, it only works if the play is in the middle of the light?
Thanks
Make a trigger in the shape of the spotlight and use OnTriggerEnter.
–Eric
Eric’s way is the easiest. You could also do a SphereCast and make it only check at a specific angle. Which I think would work since it would be basically like a ConeCast…
This would give you more control over things like checking at what angle they are at in the light. Say your checking 100 degree’s… maybe they are at 50 and the light is more intense… you could add some different modifier based on that.
Just an idea.
I did a trigger at first but i might try Ereous’ idea that sounds like a good way to do it.
Thanks ![]()
Im having trouble on how to use SphereCast, are you able to help?
Put this in a Script…
public float rangeToCheck = 18f; //Distance to sphere cast.
public float angle = 180f; //Angle we want to sphere cast
private int playerLayer = 1<<8; //You can get this number by looking the layers 1-32 or something like that.
private Collider[] hit; //All Objects we hit with the Sphere cast.
public bool CheckIfPlayerInFront()
{
//Position we cast from, Distance to cast, and The layer we only want to check for hits..
hit = Physics.OverlapSphere(transform.position, rangeToCheck, playerLayer);
foreach (Collider c in hit) //c is the objects that we are hitting with the cast.
{
if (Vector3.Angle(transform.forward, c.transform.position - transform.position) <= angle)
{
//Change this script if needed..
if(c.GetComponent<SomeComponentOnThePlayer>() != null)
{
//Adjust some values of the player.
return true;
}
}
}
return false;
}
Create a folder named Editor-> Create a Script. This will be an editor of the script you put above.
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
[CustomEditor(typeof(EnterYourClassYouPutTheCodeIn))]
public class OpenableEditor : Editor
{
void OnSceneGUI ()
{
var tar = (EnterYourClassYouPutTheCodeIn)target;
if(tar.transform != null)
{
Handles.color = Color.white;
Handles.DrawSolidArc(tar.transform.position,
tar.transform.up,
tar.transform.forward,
tar.angle/2,
tar.rangeToCheck);
Handles.DrawSolidArc(tar.transform.position,
tar.transform.up,
tar.transform.forward,
-tar.angle/2,
tar.rangeToCheck);
Handles.color = Color.white;
Quaternion arrowPointer = tar.transform.rotation;
tar.rangeToCheck = Handles.ScaleValueHandle(
tar.rangeToCheck,
tar.transform.position + tar.transform.forward*tar.rangeToCheck,
arrowPointer, 2, Handles.ConeCap, 1);
Handles.color = Color.red;
}
if (GUI.changed)
EditorUtility.SetDirty (tar);
}
The Editor will better help you visualize what its really checking.
Got an error.
Assets/CAster.cs(20,39): error CS0246: The type or namespace name `SomeComponentOnThePlayer’ could not be found. Are you missing a using directive or an assembly reference?
Yeah… the name is exactly what you should do… Your looking for Some component on your player… Mine is InputHandler for instance.
What should go in here?
EnterYourClassYouPutTheCodeIn
Seriously if you can’t figure that out I can’t help anymore. I gave you two great pieces of code that will help you achieve your results. You have to think a bit for yourself.
