- Home /
how do i have a crosshair turn red when over an enemy?
Hi! I was wondering how to make a crosshair turn red when it's over an enemy? What I was thinking was, raycasting perhaps? So it draws an imaginary line then when it's in line with a gameobject tagged enemy it will switch the crosshair 2d Texture with a red one that I'll make. (no need to change the color within unity)
This is my crosshair script:
var crosshairTexture : Texture2D;
var position : Rect;
static var OriginalOn = true;
function Start()
{
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);
}
}
So how? Thanks a bunch
Answer by Kacer · Nov 21, 2011 at 09:51 AM
Heh, you answered your own question in your post.
use a simple raycast to check if you've got your crosshair hovering over an enemy, if that is the case, switch the textures for the crosshair.
or was the question about how you made a raycast and used it to switch the textures?, also, please reread your posts after you've posted them to make sure your code was formatted correctly
edit: Here's some stuff about how to do it.
First of all, a raycast is a ray you can cast in any given direction in the game, here's a link to the documentation: http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html
If you cast that in update you get a response for every frame, the return value of the raycasthit is the gameobject itself, with all attached scripts, rigidbodies, values etc etc. SO, what you do is that you write something along the lines of
if(hit.gameobject.tag == "enemy"){
guicrosshairtex = redcrosshairtex;
}else{
guicrosshairtex = greencrosshairtex;
}
simple simple :P
yes! I was asking how to write that into the code, like you writing it in and then reposting the whole code. Please. lol
I know how code works, but when it comes to writing it I get stuck.
Well, i am not going to write the entire code for you, but i will help you along then :)