- Home /
Mouse Crosshair not replacing
I have added a crosshair script and added Crosshair textures, such that the Crosshair texture follows the mouse pointer, while the mouse pointer is disabled.
Here's the script
var normalCrosshairTexture : Texture2D; //Displayed normal mouse texture
var attackCrosshairTexture : Texture2D; //Displayed over enemies
var friendlyCrosshairTexture : Texture2D; //Displayed over friendly objects
var attack = false;
var friend = false;
function Update(){
Screen.showCursor = false; //Default mouse pointer disabled
}
function OnGUI () {
GUI.depth = 0;
var mousePos = Event.current.mousePosition;
if(!attack){
GUI.DrawTexture(Rect(mousePos.x - (normalCrosshairTexture.width/2), mousePos.y - (normalCrosshairTexture.height/2), normalCrosshairTexture.width, normalCrosshairTexture.height), normalCrosshairTexture);
//Displays the normal mouse texture
}
if(attack){
attack = true;
GUI.DrawTexture(Rect(mousePos.x - (attackCrosshairTexture.width/2), mousePos.y - (attackCrosshairTexture.height/2), attackCrosshairTexture.width, attackCrosshairTexture.height), attackCrosshairTexture);
//Displays the 'attack' mouse texture
}
if(friend){
GUI.DrawTexture(Rect(mousePos.x - (friendlyCrosshairTexture.width/2), mousePos.y - (friendlyCrosshairTexture.height/2), friendlyCrosshairTexture.width, friendlyCrosshairTexture.height), friendlyCrosshairTexture);
//Displays the 'friendly' mouse texture
}
}
The problem is that it draws the GUI texture over each other. For Example, when the attack variable comes true, it displays the 'attack' mouse texture over the previous mouse texture, say the friendly mouse texture. And due to the transparency of the textures, the previous textures can be seen, giving it an ugly look.
I don't know the problem and how to remove the previous GUI texture, so any help would be greatly appreciated...
Answer by mattssonon · Aug 21, 2013 at 12:45 PM
Don't draw the friend texture when attack
is true by setting friend
to false, e.g.:
if(attack){
attack = true;
friend = false;
GUI.DrawTexture(Rect(mousePos.x - (attackCrosshairTexture.width/2), mousePos.y - (attackCrosshairTexture.height/2), attackCrosshairTexture.width, attackCrosshairTexture.height), attackCrosshairTexture);
}
Secondly, it makes no sense to set attack = true
in an if conditional which is only run if attack
is true.
Thanks a lot. I knew the problem was in my head not to think of this. But still, Thank you very, very, very much.
Your answer
Follow this Question
Related Questions
Assigning UV Map to model at runtime 0 Answers
Delta event out of Editor Problem 4 Answers
Creating a visual toolbar using an array of GUI Textures 2 Answers
Scaling bg gui texture without stretching it in unity 4.6 0 Answers
Rect, Texture, GUI, Detection. 1 Answer