Hello there everyone!
So, I am building a FPS and want to make the hitmaker show up once you hit something but I cannot get the hitmaker in the correct location I want it.
Here is my code (Nothing is wrong with it)
http://pastebin.com/W1EpUz8b
And yes, showHitmaker is always set to true…
So, I have an object called “HitmakerHolder” and of course, is on a vector3 axis. I put that object right infront of my gun’s tip so what I want is to show the hitmaker right there.
I’m just not sure how to get the 2D x and y axis from that object?
Please help me get the correct x and y, thanks.
First, for future posts please include your code with your question. Paste the code into the question, select your code, and use the 101/010 button to format it. Second, the function name is ‘OnGUI()’ with an upper case ‘O’. Third, GUI.DrawTexture() uses GUI coordinates, not world coordinates. Fourth, avoid multiple Transform.Find() calls. Fifth, if you can do so, cache the result from Transform.Find() in Start() if your code allows. Here is a possible (untested) rewrite:
void OnGUI() {
if (showHitmarker) {
Rect rect = new Rect(0,0,crosshairImage.width, crosshairImageHeight);
Vector3 pos = transform.Find("BulletSpawn").position;
pos = Camera.main.WorldToScreenPoint(pos);
pos.y = Screen.height - pos.y;
rect.center = pos;
GUI.DrawTexture(rect, crosshairImage);
}
}
}