- Home /
Cursors and crosshairs
How would I make a crosshair or cursor, and lock the mouse cursor into the crosshair so its like the cross hair is the mouse? i want to make the crosshair interact with things, like activate objects when i click on them. Would i have to use raycasts?
Answer by playbass06 · Jul 16, 2011 at 05:02 PM
To lock the mouse just use screen.lockCursor = true;
For a crosshair, I just used a GUI texture (Create > GUI Texture) and centered it. Just use whatever image you want for that.
As for activating objects, there's probably a better way but I just use (with javascript, anyway) the function OnMouseOver and then an if statement checking whether there's input on the left mouse button.
Hope that helps! EDIT: to answer your comment (since I can't put code in a comment):
function OnMouseOver () {
if(Input.GetMouseDown(0)){
//interaction
}
}
by input on the left mouse button, you mean checking whether the object is interactable?
Edited in the question (can't put code in a comment), hope that explains. Like I said there's probably a better way to do it, that's just my solution.
cool thanks, but i decided to learn raycasting, and it worked out so far. thanks for the help though.
the On$$anonymous$$ouseOver code works same with C#, just change function to void and also the //interaction code you choose has the C# syntax in C# scripts, but whatever you prefer/ know
Your answer
Follow this Question
Related Questions
Problem with raycast aim, need help ignoring layers/tags and pointing at the end of the ray. 2 Answers
Unity3D - RTS Game - Instantiate object on buttonclick and make it follow the cursors position 2 Answers
Aiming to the crosshair 1 Answer
How to make an Interactive Door (Raycasting) 1 Answer
Bullets, raycasts, and obstacles 1 Answer