- Home /
Object detect my light RayCast and activating Lerp
I am trying to get make a script that will detect when my light that has a set range of 7 it's raycast hits a few different objects. Once the object detects the ray it will start my lerp(colors are set in inspector from alpha(0)-alpha(1)). I have set up my lerp and checked if it runs in the update function(it does). My goal is now to only activate the lerp now once the ray hits the object the lerp is on.
I have tried looking through the documentation (ray, raycast, raycasthit, etc), I saw about having to change layers as well which my next course of action was to have all other objects on layer IgnoreRayCast just not sure if thats the best or most efficient way, also I tried somethings from (https://www.reddit.com/r/Unity3D/comments/6ryx76/raycast_for_activating_objects/). I am very new to unity and have only been programming for a few months, I am having a hard time even understanding how to read the documentation properly to execute what is being relayed. Any help would be really appreciated, not to sound too ignorant but layman's terms if possible?
(script for object going through lerp)
public class enumLight : MonoBehaviour
{
public float speed = 1;
public Color startColor;
public Color endColor;
float startTime;
// Start is called before the first frame update
void Start()
{
startTime = Time.time;
}
// Update is called once per frame
void Update()
{
RaycastHit hit;
float t = (Time.time - startTime) * speed;
GetComponent<Renderer>().material.color = Color.Lerp(startColor, endColor, t);
//Debug.Log(Color.Lerp(startColor, endColor, t));
//Debug.Log(hit.transform.name);
//hitByray();
}
void hitByray()
{
Debug.Log("I was hit");
}
}
(script from my light that the raycast is on)
//hold the camera
public Camera lightRay;
//camera range for raycast
public float lightRange = 7f;
//reference the puzzle
public GameObject LightBox;
LightPuzzle lightPuzzle;
// Start is called before the first frame update
void Start()
{
lightPuzzle = LightBox.GetComponent<LightPuzzle>();
}
// Update is called once per frame
void Update()
{
shoot();
}
void shoot()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, -Vector3.up, out hit))
{
hit.transform.SendMessage("hitByRay");
}
if (Physics.Raycast(lightRay.transform.position, lightRay.transform.forward, out hit, lightRange))
{
Debug.Log(hit.transform.name);
if (hit.distance < lightRange)
{
}
}
}
in summary I want my object that has the lerp to detect my light that has the raycast on it to activate the lerp that I have in place.
Your answer
![](https://koobas.hobune.stream/wayback/20220612190108im_/https://answers.unity.com/themes/thub/images/avi.jpg)