- Home /
2d Sidescroller Crosshair!
Hi!
I'm trying to create a sidescrolling shooter game mechanic, but I don't know how to create a GUI crosshair and don't know how to make my character point it's gun at the crosshair at all times! Please help and thanks in advance :)
Answer by Zib Redlektab · Mar 07, 2011 at 02:02 PM
To make the crosshair (assuming that you want to make it follow the cursor), you would probably want to use GUI.DrawTexture:
This goes outside your functions, load it with the cursor image in the Inspector:
var cursor : Texture2D;
Then in OnGUI:
GUI.DrawTexture(Rect(Input.mousePosition.x, Input.mousePosition.y, cursor.width, cursor.height), cursor);
And in Update:
GunObject.transform.LookAt(Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
I'm not 100% sure on that 0, but it would be something along those lines, I think.
Well I tried that. And the look at thingie worked quite well, but the crosshair! It's something odd! It follows the mouse (kinda) , but it's somehow inverted and not on top of the mouse! :O So further help would still be appriciated. :)
Ah yes, ins$$anonymous$$d of Input.mousePosition.y, use (Screen.height - Input.mousePosition.y). For the not-on-top-of-the-mouse, you just need to adjust the location of the image:
Input.mousePosition.x - 15, (Screen.height - Input.mousePosition.y) +20 for example.
Your answer
Follow this Question
Related Questions
Restrict a player's movement to a path for a 2.5D game that has curved paths 6 Answers
Camera follow in c# 1 Answer
Dealing with multiple crosshairs 0 Answers
Simple GUI In game 1 Answer
Shooting spell at center of screen 0 Answers