- Home /
Raycast Destroys player.
Hi,i want my player destroys when he touches raycast,i dont know anything about raycast,so may i ask script example? Please?
how it works,like: destroy (gameobject)-its player then action is happening with player,please can you explain me, i dont know anything.
Think of a Raycast as simply a Line with an Origin(starting point), Distance, and Direction. It's basically a hit test.
So when you test if the Ray(or line) collides with the target object and if it does, you can do whatever such as destroy the game object.
The documentation posted above explains with examples.
Well its not posted what i need,well just destroy player and thats all,with that Destroy(gameobject)
Answer by MibZ · Oct 16, 2012 at 07:10 PM
Ray rayToCast = Camera.main.ScreenPointToRay(screenPosition);
RaycastHit hit;
//When out is placed in front of a function parameter the variable passed in is also returned, so the RaycastHit "hit" is given data from the Physics.Raycast function, and after the function has finished running you can check "hit" for information related to what the ray collided with.
Physics.Raycast(ray, out hit);
if (hit.collider != null)
{
//If hit.collider isn't null, that means the ray collided with a GameObject that has a Collider (box collider, sphere collider, mesh collider, etc)
//You can reference the GameObject the collider is attached to by using "hit.collider.gameObject"
//In case you don't know, uppercase 'G' GameObject is the type, while lowercase 'g' gameObject allows you to reference the GameObject a component is attached to.
if (hit.collider.gameObject.tag == "Player")
Destroy(hit.collider.gameObject);
}
i am geting this error : ArgumentException: You are not allowed to call INTERNAL_CALL_ScreenPointToRay when declaring a variable.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How To Invoke RayCastAll? 1 Answer
Line Renderer Collision Detection. 2 Answers
Why this script is not working? 1 Answer
hide object start script 4 Answers