Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

SpotLight

Discussion in 'Scripting' started by taity180, Jan 8, 2013.

  1. taity180

    taity180

    Joined:
    Oct 16, 2012
    Posts:
    23
    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
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Make a trigger in the shape of the spotlight and use OnTriggerEnter.

    --Eric
     
  3. Ereous

    Ereous

    Joined:
    Aug 29, 2012
    Posts:
    163
    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.
     
  4. taity180

    taity180

    Joined:
    Oct 16, 2012
    Posts:
    23
    I did a trigger at first but i might try Ereous' idea that sounds like a good way to do it.
    Thanks :)
     
  5. taity180

    taity180

    Joined:
    Oct 16, 2012
    Posts:
    23
    Im having trouble on how to use SphereCast, are you able to help?
     
  6. Ereous

    Ereous

    Joined:
    Aug 29, 2012
    Posts:
    163
    Put this in a Script..

    Code (csharp):
    1.  
    2. public float rangeToCheck = 18f;    //Distance to sphere cast.
    3. public float angle = 180f;          //Angle we want to sphere cast
    4. private int playerLayer = 1<<8;     //You can get this number by looking the layers 1-32 or something like that.
    5. private Collider[] hit;             //All Objects we hit with the Sphere cast.
    6. public bool CheckIfPlayerInFront()
    7. {
    8.                                 //Position we cast from, Distance to cast, and The layer we only want to check for hits..
    9.     hit = Physics.OverlapSphere(transform.position, rangeToCheck, playerLayer);
    10.     foreach (Collider c in hit) //c is the objects that we are hitting with the cast.
    11.     {
    12.         if (Vector3.Angle(transform.forward, c.transform.position - transform.position) <= angle)
    13.         {
    14.             //Change this script if needed..
    15.             if(c.GetComponent<SomeComponentOnThePlayer>() != null)
    16.             {
    17.                 //Adjust some values of the player.
    18.                 return true;
    19.             }
    20.         }
    21.     }
    22.     return false;
    23. }
    24.  
    Create a folder named Editor-> Create a Script. This will be an editor of the script you put above.

    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using UnityEditor;
    5. using System.Collections;
    6. using System.Collections.Generic;
    7.  
    8. [CustomEditor(typeof(EnterYourClassYouPutTheCodeIn))]
    9. public class OpenableEditor : Editor
    10. {
    11.     void OnSceneGUI ()
    12.     {
    13.         var tar = (EnterYourClassYouPutTheCodeIn)target;
    14.        
    15.         if(tar.transform != null)
    16.         {
    17.             Handles.color = Color.white;
    18.             Handles.DrawSolidArc(tar.transform.position,
    19.                     tar.transform.up,
    20.                     tar.transform.forward,
    21.                     tar.angle/2,
    22.                     tar.rangeToCheck);
    23.            
    24.             Handles.DrawSolidArc(tar.transform.position,
    25.                     tar.transform.up,
    26.                     tar.transform.forward,
    27.                     -tar.angle/2,
    28.                     tar.rangeToCheck);
    29.             Handles.color = Color.white;
    30.             Quaternion arrowPointer =  tar.transform.rotation;
    31.             tar.rangeToCheck = Handles.ScaleValueHandle(
    32.                     tar.rangeToCheck,
    33.                     tar.transform.position + tar.transform.forward*tar.rangeToCheck,
    34.                     arrowPointer, 2, Handles.ConeCap, 1);
    35.              Handles.color = Color.red;
    36.         }
    37.  
    38.       if (GUI.changed)
    39.         EditorUtility.SetDirty (tar);
    40.     }
    41.  
    The Editor will better help you visualize what its really checking. $playerDetection.gif
     
    Last edited: Jan 8, 2013
  7. taity180

    taity180

    Joined:
    Oct 16, 2012
    Posts:
    23
    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?
     
  8. Ereous

    Ereous

    Joined:
    Aug 29, 2012
    Posts:
    163
    Yeah.. the name is exactly what you should do.. Your looking for Some component on your player.. Mine is InputHandler for instance.
     
  9. taity180

    taity180

    Joined:
    Oct 16, 2012
    Posts:
    23
    What should go in here?

    EnterYourClassYouPutTheCodeIn
     
  10. Ereous

    Ereous

    Joined:
    Aug 29, 2012
    Posts:
    163
    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.