- Home /
The question is answered, right answer was accepted
How to use a raycast in a complete script? C#
I am trying to figure out how to use a raycast in C#, but the scripting reference doesn't really help me understand how to use it. All I know is its a way of marking an object's 3D position on a 2D screen, or something like that.
I would be very appreciative if someone could make an example script with a raycast and give an explanation of what is going on. (Maybe they could have an object move to a mouseclick)
If don't have time to do that maybe you could just explain how to use a raycast and where it would go in a script.
I'm completely lost on this so any help would be nice. =)
Answer by Hullu · Jul 28, 2014 at 05:49 PM
I think the scripting reference explains it perfectly http://docs.unity3d.com/ScriptReference/Physics.Raycast.html
For example this casts ray from your camera to mouse position when you click left mouse button:
void Update() {
if (Input.GetMouseButtonDown(0)){
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, 100))
print("Hit something");
}
}
Follow this Question
Related Questions
Finding Distance between Angles and Points 2 Answers
Getting the Furthest Point on a Ray 1 Answer
C# Raycast 2D GameObject follow Mouse 1 Answer
Can't get a laser working properly. 2 Answers
C# Raycast isn't working? 1 Answer