- Home /
Trouble with checking Raycast
Working on a prototype level editor, and I'm having trouble detecting if a block is under the mouse pointer to be used for a delete function.
Basically, the ray would check under the mouse's position in the world for an object it would collide with, and would delete it if it came back true (which is being replaced with a Debug.Log), but I can't seem to get the ray to hit anything or detect anything to hit
void Update() {
Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10f);
Vector3 worldPos = Camera.main.ScreenToWorldPoint(mousePos);
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(worldPos);
if (Input.GetMouseButtonDown(0))
{
switch (blockType)
{
case (1):
if (Physics.Raycast(ray, out hit, 20))
{
Debug.Log("Working");
}
break;
}
}
}
Any help/explanation would greatly be appreciated :)
Answer by kaleF01 · Dec 23, 2016 at 07:38 AM
Ray ray = Camera.main.ScreenPointToRay(worldPos);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Your answer
Follow this Question
Related Questions
Raycast problem 1 Answer
What is wrong about this RaycastHit Event??? 1 Answer
Having a RaycastHit event create a component? 1 Answer
C# change an object tag wit raycasthit. 2 Answers
Vector3.Movetoward. Why does this work? 0 Answers