var hit : RaycastHit;
var redCrosshair : GUITexture;
var blueCrosshair : GUITexture;
var LdidHit : boolean;
function Start () {
redCrosshair.enabled = false;
blueCrosshair.enabled = true;
}
function Update () {
if (Physics.Raycast (transform.position, transform.forward, hit, 20000))
{
if (hit.collider.gameObject.tag == "Large"){
LdidHit = true;
redCrosshair.enabled = true;
blueCrosshair.enabled = false;
}
else {
LdidHit = false;
redCrosshair.enabled = false;
blueCrosshair.enabled= true;
}
}
}
so the problem im having is trying to have my crosshair change color depending if its hitting a large, medium, small, or an enemy. This works for now slightly for the large object. I want to call it like (“Large”,“Medium”,“Small”) but that doesn’t work. May someone help me please?
Also, when the red crossair is enabled and im off the large object the gui still appears until I shoot. Can I get an answer for that too please?
2 Answers
2
You don’t have the other colors for ‘Medium’, ‘Small’, and ‘Enemy’ crosshairs, do I will just rough things in a bit. Your big problem in the code above is how the brackets match. If you are careful with your indenting, problems like the crosshair not going away would be easy to spot. Here is a restructure of your code that provides a foundation for you to build on as you introduce crosshairs of other colors:
#pragma strict
var hit : RaycastHit;
var redCrosshair : GUITexture;
var blueCrosshair : GUITexture;
var LdidHit : boolean;
function Start () {
redCrosshair.enabled = false;
blueCrosshair.enabled = true;
}
function Update () {
LdidHit = false;
redCrosshair.enabled = false;
blueCrosshair.enabled= true;
if (Physics.Raycast (transform.position, transform.forward, hit, 20000)) {
switch(hit.collider.gameObject.tag) {
case "Large":
LdidHit = true;
redCrosshair.enabled = true;
blueCrosshair.enabled = false;
break;
case "Medium":
LdidHit = true;
redCrosshair.enabled = true;
blueCrosshair.enabled = false;
break;
case "Small":
LdidHit = true;
redCrosshair.enabled = true;
blueCrosshair.enabled = false;
break;
case "Enemy":
LdidHit = true;
redCrosshair.enabled = true;
blueCrosshair.enabled = false;
break;
}
}
}
Note the ‘right’ way to code up this change of cursors is to have an array. It could be an array of GUITextures or better yet would be an array of textures and the code resets the texture used in the GUITexture.
Ah, I see…Thank you very much for your help and I appreciate you taking the time to clean up my mess. Take care.
Reformat your code please. It all has to be indented at least 4 spaces.. or use the "101010" button in the post editor.
– flaviusxvii