- Home /
How to make a raycast that kills my player
In my game, I have lights that move back and forth and I want to make it so that when my player walks into the light, he dies and respawns. I was considering using a raycast to do this. How would i make this raycast so that it moves along with the lights and kills my player when he walks into it. I am also open to any other suggestions on how to kill my player when he walks into the light. Thanks.
A raycast is an action. It has no physical form and therefore cannot move. It's like saying I move the slashing of my knife. For your purpose I suggest a lineast, since it will make it easier when it comes to from/to calculation
you would have to send message from raycasting lazer script to the player script telling it to destroy.
It IS possible to do this with Raycast, and it's quite easy. But this would be a straight line, and would not work if you're wanting to use something like a spotlight with a wide angle, unless you do many raycasts within the light in a cone shape. But if you're talking something like a laser beam, this would work like so. Attach an empty gameObject as a child to your "light".
RaycastHit hit;
void Update()
{
if(Physics.Raycast(transform.position, transform.forward, out hit, $$anonymous$$athf.Infinity)
{
if(hit.collider.tag == "Player")
{
//Destroy and Respawn Code
}
}
}
This is a very basic C# example.
If you want to do something like a spotlight, the EASIEST thing I can think of would be to use a 3D model of a cone or cylinder with a collider attached that "Is Trigger". Then you could look into OnTriggerEnter on the Unity Script Reference, it would work something like:
void OnTriggerEnter(Collider collider)
{
if(collider.gameObject.tag == "Player")
//Do something
}
Question is a bit vague, so if this doesn't help at all, I would need some more detailed information.
Or you could just test wether the player is within the "cone" of the light, eg within the radius, then use the raycast only to deter$$anonymous$$e the shadow. Like @robertbu XD
Please either accept an answer if one provides a solution, or close the question if you have found another solution. If not, please let us know this has not been resolved, so that we can work with you to figure it out. When you ask a question, we all put some real effort into answering it properly for you. Please have the courtesy to show some type of appreciation for the help you have received.
Answer by robertbu · Jul 30, 2013 at 03:09 AM
Here is a method. Attach this script to a light:
#pragma strict
private var player : Transform;
function Start() {
player = GameObject.Find("Player").transform;
}
function Update() {
var v3 = player.position - transform.position;
var angle = Vector3.Angle(transform.forward, v3);
if (angle <= light.spotAngle / 2.0 && v3.magnitude < light.range) {
if (!Physics.Linecast(player.position, transform.position)) {
player.position = Random.insideUnitSphere * 5.0;
}
}
}
This assumes that your light is a spot light. You can take the angle check out for a point light. It works by checking to make sure the pivot point of the character is in the cone of light and that there are no objects between the light and the character's pivot point. This is basic functionality. A more through check would be to check all the points on the bounding box of the character. Note this script just moves the player. If you kill and then Instantiate a new player, you will need to reinitialize the 'player' variable.
Your answer
![](https://koobas.hobune.stream/wayback/20220613111149im_/https://answers.unity.com/themes/thub/images/avi.jpg)