- Home /
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
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.
@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?
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.
@Eno-Khaon, I changed to compare tag and added the rigidbodies. 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?
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)
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);
}
}
@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...
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);
}
}
}
}
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?
@rayith, @bubzy, @Eno-Khaon , do any of you guys understand where my problem lies?