- Home /
Question by
idiot333 · Mar 08, 2015 at 12:18 PM ·
mouseraycasthitclickmouseclickray cast
Raycast hit not detected on cube
my script is attached to a cube and i am ray casting to check whether the cursor is clicked on the cube.
this is inside my update method-
if(Input.GetMouseButtonUp(0)|Input.GetMouseButtonUp(1)){
RaycastHit hit;
Vector3 pos=Input.mousePosition;
Ray ray=Camera.main.ScreenPointToRay(pos);
Debug.DrawRay (ray.origin, ray.direction * 10, Color.yellow);
if(Physics.Raycast(ray, out hit)){
if(hit.collider!=null&&hit.collider.gameObject.tag=="nextship"){
ShipMenuController.nextShipSwitch();
}
}
}
THE EFFECT OF THIS code is- When he cube is clicked, nothing happens and when i click on any other place except the cube, the click is detected. The click is not detected on the cube but on every other places.
What is the problem? Any help?
Comment
Answer by Ilgiz · Apr 13, 2015 at 05:47 PM
your code is good. Сheck the following questions: 1. did you set tag "nextship" to your box? 2. did your box have a boxcollider?
if (Input.GetMouseButtonUp(0) | Input.GetMouseButtonUp(1))
{
RaycastHit hit;
Vector3 pos = Input.mousePosition;
Ray ray = Camera.main.ScreenPointToRay(pos);
Debug.DrawRay(ray.origin, ray.direction * 10, Color.yellow);
if (Physics.Raycast(ray, out hit))
{
if (hit.collider != null && hit.collider.gameObject.tag == "nextship")
{
Debug.Log("Clicked");
}
}
}