- Home /
Making FPS Reticule Change
I am trying to make i so when My reticule(characters Z) goes over an object tagged enemy it changes the reticule to red.
Heres my code so far
var target:Transform; var range = 10.0; var reticule:GUITexture; var colorOn:Color; var colorOff:Color;
function Awake() { if(!target) { target = GameObject.FindWithTag("Enemy").transform; } }
function Update() { if(target && CanAttackTarget()) { reticule.color = colorOn; }
else// if(target && !CanAttackTarget())
{
reticule.color = colorOff;
//var defaultLookRotation = Quaternion.LookRotation(defaultLook.position - transform.position, Vector3.up);
//transform.rotation = Quaternion.Slerp(transform.rotation, defaultLookRotation, Time.deltaTime * damp);
print("NO");
}
}
function CanAttackTarget() { //Check if target is in range if(Vector3.Distance(transform.position, target.position) > range) { print("Not in Range"); return false; }
var hit : RaycastHit;
//Check if theres interferance between the two
if(Physics.Linecast(transform.position, target.position, hit))
{
if(hit.collider.gameObject.tag != "Enemy")
{
print("Interferance from : " + hit.collider.gameObject.name);
return false;
}
else
{
print("enemy detected");
return true;
}
}
return true;
}
the only problem is when even when Im not looking at it its red, but when Im not in the specified range it turns white. I wan it to turn white when Im not looking at it and red when I am (from the distance specified as range)
Answer by The_r0nin · Jan 06, 2011 at 02:00 AM
First of all, a raycast is from position to position... it has nothing to do with "looking at" something. It is simply a line drawn from one point to another. So when you raycast in your code, you are simply asking if a line can be drawn from you to the target, not if your camera can "see" it. What you want is Camera.main.ScreenPointToRay.
If you check the documentation, you'll find that this will allow you to project your ray through the camera's viewpoint towards a target.
var ray : Ray = camera.ScreenPointToRay (Vector3(mousePosition.x,mousePosition.y,0));
var hit : RaycastHit;
if(Physics.Raycast(ray, hit)){
// do your tag check here
}
That should determine if there is interference between your mouse position and the target. You may have to invert the mousePosition.y (with something like [Screen.height-mousePosition.y] to get the correct mouse position for the ScreenPointToRay... I forget).
Another thing you might want to look at is renderer.isVisible. That might be useful as well...
P.S.: If you put your distance in the Raycast, you no longer need your first "if" statement. Granted, you'll be Raycasting every frame, so you might want to keep it for optimization, but it isn't necessary:
if (Physics.Raycast (ray, hit, range)){
// do your tag check here
}
i get an error saying Assets/Halo/Scripts/reticuleControl.js(41,20): BCE0023: No appropriate version of 'UnityEngine.Physics.Linecast' for the argument list '(UnityEngine.Ray, UnityEngine.RaycastHit)' was found.
Ahhh! When I cut and pasted from your code, I missed that you had used Linecast. Use RayCast ins$$anonymous$$d... I've edited the code above.
Your answer
![](https://koobas.hobune.stream/wayback/20220613094223im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
gui.label color change 1 Answer
changing the color of particles using the legacy particle animator? 2 Answers
Invert The Color Of A GUIText Crosshair? 1 Answer
How would I change the colour of a prefab relative to the size of the character? 1 Answer
Blend into next color instead of jumping to next color 1 Answer