- Home /
How to make crosshair turn red when it targets a enemy
Im having problems in making the Crosshair turn red when it on a enemy? Can anybody give me some tips or a script I can use
var crosshair01 : GUITexture;
var FpsPlayer;
function Start() {
FpsPlayer = GameObject.FindWithTag("Player");
}
function OnMouseOver () {
var targetDistance = Vector3.Distance(FpsPlayer.transform.position, transform.position);
if(targetDistance < 12.0){
crosshair01.guiTexture.color = Color(1,0,0,0.5);
}
}
function OnMouseExit () {
crosshair01.guiTexture.color = Color(1,1,1,0.5);
}
The typical way to handle a crosshair is to Raycast. That is you have a game object that is either locked to the center of the screen or tracks the mouse, and you cast a ray to see if that crosshair hits a target in the scene. Then your logic for turning it red will be with the crosshair code.
Your way does things in reverse. You make the targets responsible for telling when they are targeted. If all your targets have this script, your way will work. But to make it work this scrip (i.e. the targets) needs some kind of access to the game object that is displaying the crosshair. Posting the code you use to display the crosshair will be helpful for the list to come up with an accurate answer for your question.
Using new script but the problem is that the crosshair wont change color when its on the enemy
var crosshair: Texture2D; // drag the normal crosshair here
var redCrosshair: Texture2D; // drag the red crosshair here
var position :Rect;
var hit: RaycastHit;
private var isEnemy: boolean = false;
function Update(){
if (Physics.Raycast(transform.position, transform.forward, hit, 100))
{
isEnemy = (hit.transform.tag == "Enemy"); // assign the comparison result to is enemy
}
}
function OnGUI(){
var cross = crosshair; // assume normal crosshair
if (isEnemy) cross = redCrosshair; // change to red if isEnemy is true
GUI.DrawTexture(position, cross);
}
Answer by Complex77 · Feb 28, 2013 at 07:50 PM
You can use Physics.Raycast to achive this, send a ray from your cam.transform towards your Crosshair and if you hit an enemy turn it red.
http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html
As an example, you could use the following on your Crosshair or someplace for gemeral scripts, the main diff is that you only add this once, you need to tag your enemies, while in your idea, you must add that to all enemies just to check if mouse hovers over it.
void Update() {
GameObject cam = Camera.mainCamera;
Vector3 fwd = transform.TransformDirection(Vector3.forward);
RaycastHit OutHit;
if (Physics.Raycast(cam.transform.position, fwd, OutHit))
if(OutHit.transform.tag == "Enemy"){
crosshair01.guiTexture.color = Color(1,0,0,0.5);
}
else{
crosshair01.guiTexture.color = Color(1,1,1,0.5);
}
}
}
this code might be wrong, i did it without testing nor checking syntax, but it might help you out, good luck !
Nik
Answer by Complex77 · Feb 28, 2013 at 06:58 PM
You can use Physics.Raycast to achive this, send a ray from your cam.transform towards your Crosshair and if you hit an enemy turn it red.
http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html
As an example, you could use the following on your Crosshair or someplace for gemeral scripts, the main diff is that you only add this once, you need to tag your enemies, while in your idea, you must add that to all enemies just to check if mouse hovers over it.
void Update() {
GameObject cam = Camera.mainCamera;
Vector3 fwd = transform.TransformDirection(Vector3.forward);
RaycastHit OutHit;
if (Physics.Raycast(cam.transform.position, fwd, OutHit))
if(OutHit.transform.tag == "Enemy"){
crosshair01.guiTexture.color = Color(1,0,0,0.5);
}
else{
crosshair01.guiTexture.color = Color(1,1,1,0.5);
}
}
}
this code might be wrong, i did it without testing nor checking syntax, but it might help you out, good luck !
Nik
Answer by robertbu · Feb 28, 2013 at 07:38 PM
Here is a rewrite of your code that should work. You have several different things going on here. First your crosshair is in GUI coordinates. They start in the upper left of the screen. Second transform.position is the position of the game object you have this script attached to, can be anywhere, and has no relationship to the crosshair. So to do the raycast using your existing logic, you have to convert GUI Coordinates --> Screen Coordintates --> World Coordinates. Then you can raycast:
var crosshair: Texture2D; // drag the normal crosshair here
var redCrosshair: Texture2D; // drag the red crosshair here
var position :Rect;
private var isEnemy: boolean = false;
function Update(){
var hit: RaycastHit;
var v3Pos : Vector3 = position.center; // Get GUI Coordinates
v3Pos.y = Screen.height - v3Pos.y; // Convert to Screen Coordinates
var ray : Ray = Camera.main.ScreenPointToRay(v3Pos); // Make Ray in World Coordinates.
if (Physics.Raycast(ray, hit, 100)) {
isEnemy = (hit.transform.tag == "Enemy"); // assign the comparison result to is enemy
}
else
isEnemy = false;
}
function OnGUI(){
var cross = crosshair; // assume normal crosshair
if (isEnemy) cross = redCrosshair; // change to red if isEnemy is true
GUI.DrawTexture(position, cross);
}
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
A node in a childnode? 1 Answer
C# How to Check If a Gameobject's tag doesn't equal a Specific Tag 3 Answers
Third Person Help 4 Answers
Adding force to a bullet 3 Answers