- Home /
Clicking in game view with cross-hair doesn't work
Do I need extra code for the clicking to work when I have a cross-hair? I have to constantly build and run to test something because when I click an object (in game view)it doesn't click the object.
When I build and run it and set up my native resolution it does click the object. For testing purposes how can I continue making my game and have a crosshair? The reason why I need a cross-hair is because the mouse is all messed up. I also had to lock the mouse on every frame to make the mouse go away... This is nuts.
Posting some source might help us see what you are trying to do and where you are having the problem. $$anonymous$$y guess is that you are not doing the Raycast correctly.
var crosshairTexture : Texture2D;
var position : Rect;
static var OriginalOn = true;
function Update() // Start will only get the screen size once. it will not refresh it. the turn around is to use function Update() ins$$anonymous$$d.
{
position = Rect((Screen.width - crosshairTexture.width) / 2, (Screen.height -
crosshairTexture.height) /2, crosshairTexture.width, crosshairTexture.height);
}
function OnGUI()
{
if(OriginalOn == true)
{
GUI.DrawTexture(position, crosshairTexture);
}
}
Answer by robertbu · Jan 16, 2013 at 05:24 PM
You need to Raycast the position into the scene to see what you hit. Your crosshairs are always locked at the center of the screen, so here is a snippet of untested code:
Vector2 v2ScreenCenter = new Vector2(Screen.width / 2, Screen.width / 2);
Ray ray = Camera.mainCamera.ScreenPointToRay (v2ScreenCenter);
RaycastHit hit;
if (Physics.Raycast (ray, out hit))
{
Destroy(hit.collider.gameObject);
}
This code destroys the game object. Replace with whatever you want to do on a hit. v2ScreenCenter only needs to be initialized once.
Why can't you use the regular mouse functions in unity correctly? Why do they not center nor work correctly? Thanks for the help on ray-casting though I wish it wasn't required.. I hope the unity $$anonymous$$m plans on removing the need for code more.. eventually.
Raycasting is needed because Unity is a 3D platform. You view things in perspective. Imagine you are looking out the window in the real world. You see more of the scene at a distance than near to the window. If I placed my finger on the window and closed one eye, I could create a line from my eye through my finger into the scene. The X,Y coordinates of something that my finger was overlaying would be differnet for objects far away than for near object. (relative to the center of the window) That is what you are doing by raycasting.
Note if you were using a mouse, you could use something like On$$anonymous$$ouseDown() {} and avoid raycasts. Alternatively you can change your camera's projection from "Perspective" to "Orthographic." Then there is only a simple bit of math to translate screen position to world position (no Raycast needed). But by making this change you lose the perspective view of objects in the scene.
Your answer
Follow this Question
Related Questions
2D platformer cursor 1 Answer
Texture-based mouseclick detection 1 Answer
Crosshair when holding RMB 2 Answers
Gun object following mouse. 2 Answers
Use Gamepad right analog stick instead of mouse to control crosshair movement 3 Answers