- Home /
ScreenPointToRay - Missing something simple here?
I have used ScreenPointToRay without issues before, but for some reason its not working. I must be doing something simple wrong but I cant figure it out. Here is my C# code:
Ray ray;
RaycastHit hit;
void Start()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition)
}
void Update ()
{
if(Input.GetMouseButton(1))
{
if(Physics.Raycast(ray, out hit))
{
itemScript = itemManager.GetComponent<ItemManager>();
Debug.Log("you hit " + hit.transform.name);
}
}
}
I am trying to right click on a box (with a collider, and it has no layer so its not in a layer that doesnt see raycasts) and yet all I get is "You hit terrain" , I cant ever hit the box. Argg
Answer by madmike6537 · Dec 15, 2012 at 08:20 PM
Im retarded - its because I was setting the ray at the start function instead of the update function. So of course it never updated! :)
lol I voted this up to give you some karma points just for the fun of it. Happy Coding =)
Your answer
Follow this Question
Related Questions
Editor mousePosition changing when key is pressed 0 Answers
Get 2D Collider with 3D Ray 2 Answers
Raycast ScreenPointToRay Problem 1 Answer
Why won't my Ray initialization work?? 2 Answers
Cinemachine: ScreenPointToRay raycast 2 Answers