- Home /
FPS Game - I am making an Aim System and need help (Calling all game scripters)
Basicly i need to make an aim system (looking down the sight of the gun). I think i have the the right idea in using a switch statement, but i'm not sure if the code is right or written right ...
Here is the code :
var aimCamera : Camera;
private var aim = false; private var pressedAimButton;
function Awake () { pressedAimButton = Input.GetAxis ("Fire2"); } function Update () { switch (pressedAimButton) { case true: aim = true; break;
case false: aim = false; break; }
if(aim) { aimCamera.depth = 5; } if(aim == false) { aimCamera.depth = -20; }
}
Answer by Mickydtron · Dec 21, 2010 at 05:26 PM
That looks like one way to do it, but there is a better way. A switch is really not necessary here, particularly a two case switch, which is almost never preferable to a simple if statement. And even that is not necessary here. Let us use the Input object.
var aimCamera : Camera;
function Update(){ if(Input.GetButtonDown("Fire2")){ aimCamera.depth = 5; //anything else you want to have happen on aim button being pressed } if(Input.GetButtonUp("Fire2")){ aimCamera.depth = -20; //anything else you want to have happen when aim is released } }
Much cleaner, and more straightforward. If you so desire, the GetButtonDown and Up could be replaced with some comparisons with GetAxis, but I would not recommend it.
$$anonymous$$uch Thnx. I found out before you replied but i never fought about dat. This is wot i did ...
var aimCamera : Camera;
function Update () { if(Input.GetButton ("Fire2")) { aimCamera.depth = 5; } else { aimCamera.depth = -20; } }
because else basicly means if the above is not true then do this so basicly the same but different expression. $$anonymous$$uch Thnx
Your answer
Follow this Question
Related Questions
FPS main and side weapon question 1 Answer
Weapon Switch Animations 1 Answer
FPS Weapon inventory script help 0 Answers
How To Make Ammo & Realod for Gun & Spark for Gun ? 0 Answers
Camera switch after specific time 1 Answer