x


And statement?

In my game I have a script that when I hit mouse 0 (left mouse button) I crouch using this script:

function Update () {
if(Input.GetKey("mouse 0")){
animation.Play("crouch");
}
}

And now when I hit left mouse button, I crouch, but now I want it so that I crouch and as I am crouching If I move forward I crouch walk (I have that animation) How would I do this? thanks!

more ▼

asked Jul 01 '12 at 06:16 PM

Bruno Fludzinski gravatar image

Bruno Fludzinski
104 8 21 28

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

1 answer: sort voted first

Add a toggle that is set to true and test against it.

private var isCrouching : boolean = false;

function Update () {
    if(Input.GetKey("mouse 0")) {
        isCrouching = true;
        animation.Play("crouch");
    }
    if(Input.GetKey("W") && isCrouching) {
        animation.Play("crouchWalk");
    }
}
more ▼

answered Jul 01 '12 at 07:27 PM

Piflik gravatar image

Piflik
5.4k 15 26 44

While it might be tempting to nest the if(Input.GetKey("W")) inside the Input.GetKey("mouse 0") rather than creating a new isCrouching variable, just remember you can use the isCrouching elsewhere in the code, and you can also turn it into a toggle later if you want.

Jul 01 '12 at 08:16 PM Canazza

Thanks but it still doesn't work

Jul 01 '12 at 10:09 PM Bruno Fludzinski
(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:

x40
x10

asked: Jul 01 '12 at 06:16 PM

Seen: 274 times

Last Updated: Jul 01 '12 at 10:09 PM