- Home /
How do i get rid of my Crosshair when i aim?
Hello everyone, im pretty new at unity and java coding, and i was wondering how to make my cross hair disappear when i click the right mouse button. Any ideas? If anyone could point me the way to a script that would be great!
Answer by save · Dec 18, 2012 at 11:28 AM
You would make a boolean which gets true or false at certain conditions. Now nothing from the Input-class should live inside the OnGUI function, so we have to go around that:
private var isAiming : boolean = false;
function OnGUI () {
if (isAiming) {
//Hide the crosshair
} else {
//Show the crosshair
}
}
function Update () {
if (Input.GetButtonDown("Fire2")) isAiming = true;
if (Input.GetButtonUp("Fire2")) isAiming = false;
}
If your crosshair isn't a GUI component, rather a GameObject living in 3d space, same method would apply but you can cut the intermediary boolean:
var crosshairRenderer : Renderer;
function Update () {
if (Input.GetButtonDown("Fire2")) crosshairRenderer.enabled = false;
if (Input.GetButtonUp("Fire2")) crosshairRenderer.enabled = true;
}
Lastly you could also work with the alpha channels of your crosshair (3d space example):
var crosshairRenderer : Renderer;
var crosshairShowHideSpeed = 5.0;
function Update () {
if (Input.GetButtonDown("Fire2")) HideCrosshair();
if (Input.GetButtonUp("Fire2")) ShowCrosshair();
}
function HideCrosshair () {
while (Input.GetButton("Fire2") && crosshairRenderer.material.color.a>.0) {
crosshairRenderer.material.color.a -= crosshairShowHideSpeed*Time.deltaTime;
yield;
}
}
function ShowCrosshair () {
var stop : boolean = false;
while (!stop && crosshairRenderer.material.color.a<1.0) {
crosshairRenderer.material.color.a += crosshairShowHideSpeed*Time.deltaTime;
if (Input.GetButton("Fire2")) stop = true;
yield;
}
}
Your answer
Follow this Question
Related Questions
Crosshair disappears? 2 Answers
GUI Button Disappears 1 Answer
How to make this text box disappear after a set amount of time? 1 Answer
How do I get rid of the cursor? 2 Answers
Joysticks disappears Android 1 Answer