x


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;
}

}

more ▼

asked Dec 21 '10 at 11:36 AM

Kieran gravatar image

Kieran
13 1 2 2

(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest

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.

more ▼

answered Dec 21 '10 at 05:26 PM

Mickydtron gravatar image

Mickydtron
342 1 9

Much 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. Much Thnx

Jan 19 '11 at 07:46 PM Kieran
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3465
x1176
x237
x61
x30

asked: Dec 21 '10 at 11:36 AM

Seen: 950 times

Last Updated: Dec 21 '10 at 11:36 AM