Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AhmadArshad · Jul 19, 2020 at 01:16 PM · gameobjectlighting3dcollision detectionlight

How to detect if a GameObject is hit by a Spotlight

Hi to everyone in this community. So I am working on a game. I am using SpotLight in this Unity scene (Image attached), I have a GameObject standing in front.

The question is how can I make this GameObject detect if the spot light is hitting or on him or not?

I want to make changes in GameObject transform.position if Spot Light is on him.

Any help will be much appreciated.

[1]: /storage/temp/163666-capture.png

capture.png (351.4 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by davidcox70 · Jul 19, 2020 at 03:39 PM

A raycast would work if you are only interested in whether your target GameObject passes through the center of the spot light beam. if you wanted to know if the outer area of the spotlight beam affected your gameObject, maybe the simplest solution would be to add a collider (set to trigger) attached to your spotlight that covers the area your light is illuminating. You could then use the OnTrigger calls to see when your gameObject enters or exits the lit zone.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PolymathicIndustries · Feb 06 at 08:34 PM 0
Share

@davidcox70 , I have tried this several times and nothing has worked:

 public GameObject footsteps;
     
     // Start is called before the first frame update
     void Start()
     {
         footsteps.SetActive(false);
     }
 
 
     private void OnTriggerEnter(Collider other)
     {
         //footsteps.SetActive(true);
 
         if (other.gameObject.tag.Equals("footprint"))
         {
             footsteps.SetActive(true);
         }
     }

Why wouldn't this work if attached to the spotlight?

avatar image Eno-Khaon PolymathicIndustries · Feb 09 at 05:36 AM 0
Share

Does either of your spotlight shadow and/or "footsteps" include a Rigidbody? That's a condition for OnTriggerEnter().

As a side note, you would also see better performance using other.gameObject.CompareTag("footprint") in this situation.

avatar image PolymathicIndustries Eno-Khaon · Feb 19 at 06:00 PM 0
Share

@Eno-Khaon, I changed to compare tag and added the rigidbodies.alt text and added the tags that I had missed, but still nothing. I added a screenshot to show how it looks in play mode and my spotlight is intersecting where my "footprint" should be. I also used both a cube and a world space image of a footprint, both tagged, and neither of them worked for this, even with rigidbodies, set without gravity and set kinematic. What am I doing wrong?

blacklight-attempt.png (166.5 kB)
avatar image
0

Answer by bubzy · Feb 19 at 10:21 PM

make a sphere with a collider at the point of the spotlight and if you collide with that you are in it? its going to overlap the cone a little higher up but is that going to ruin the immersion too much? if it is, make a cone and add a mesh collider to that and check for collisions with the cone. (obviously turn the renderer off)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by rayith · Feb 20 at 04:52 PM

have you tried using ray cast

 RaycastHit hit;
         if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
         {
             Debug.Log(hit.transform.name);
 
             Target target = hit.transform.GetComponent<Target>();
             if (target != null)
             {
                 target.TakeDamage(damage);
             }
         }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PolymathicIndustries · Feb 20 at 05:52 PM 0
Share

@bubzy, I have a cube that was turned to inactive when I took that screenshot. I have tried to detect colliding with this cube and found no registering of such. Does it matter that you suggest a sphere and I used a cube?

@rayith, I have not tried raycasting because I don't yet have any experience with it so its a lot to absorb at once. I don't understand your code because I don't understand what all the undefined variables are, like range, fpsCam (gameObject?), Target, target, etc. Do you mine redefining your code along with variables so that I can understand what you did. I am not even sure what TakeDamage is or how it can access target...

avatar image rayith PolymathicIndustries · Feb 20 at 08:32 PM 0
Share

so target is just a script that set damage when raycast hits

so here is the full script for my rifle

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class rifle : MonoBehaviour
 {
     public bool isFiring = false;
     public GameObject muzzleFlash;
     public AudioSource gunshot;
     public GameObject m1;
     public float damage = 10f;
     public float range = 100f;
     float firerate = 0.1f;
     float waitfire;
 
     public Camera fpsCam;
 
     void Update()
     {
         if (Input.GetMouseButton(0))
         {
             if (isFiring == false)
             {
                 StartCoroutine(FirethePistol());
             }
         }
     }
 
     IEnumerator FirethePistol()
     {
         isFiring = true;
         m1.GetComponent<Animator>().Play("shotgun");
         gunshot.Play();
         muzzleFlash.SetActive(true);
         yield return new WaitForSeconds(0.05f);
         muzzleFlash.SetActive(false);
         yield return new WaitForSeconds(0.2f);
         m1.GetComponent<Animator>().Play("none");
         isFiring = false;
 
         RaycastHit hit;
         if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
         {
             Debug.Log(hit.transform.name);
 
             Target target = hit.transform.GetComponent<Target>();
             if (target != null)
             {
                 target.TakeDamage(damage);
             }
         }
 
     }
 }



here is a mod version for you that may work

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class rifle : MonoBehaviour
 {   
    public GameObject flashlight;
    public float range = 100f;
 
 void Update()
     {
         if (Input.GetMouseButton(0))
         {
       RaycastHit hit;
         if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
         {
             Debug.Log(hit.transform.name);
 
             Target target = hit.transform.GetComponent<Target>();
             if (target != null)
             {
                 transform.position = new Vector3(0.0f, 0.0f, 0.0f);
             }
         }
    }
 
 }
        


avatar image
0

Answer by PolymathicIndustries · Mar 19 at 05:26 PM

@rayith, I am not sure how a gun script is appropriate to a spotlight script trying to expose an item.. can you explain?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PolymathicIndustries · Apr 02 at 09:00 PM 0
Share

@rayith, @bubzy, @Eno-Khaon , do any of you guys understand where my problem lies?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

274 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make a GameObject not hittable by shadows ? 2 Answers

I need help with directional light 1 Answer

Is there a way to make a scene full bright? 0 Answers

Use normal of Transform for Shader? (for Mesh without normals) 1 Answer

Ammount of Light received by a GameObject 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges