- Home /
Crouch Script Help
Okay so I have this script for crouching and I want it so as long as you hold down C you're crouching, if you let go no more crouching. What happens is I press c and I stay crouched even when I lift my finger off the key. Help please.
function Update()
{
if(Input.GetKeyDown("c"))
{
if(!isCrouched)
{
crouch();
}
else
{
stand();
}
}
}
function Update()
{
bool isCrouching = false;
if(Input.Get$$anonymous$$eyDown("c"))
{
isCrouching = true;
}
if(isCrouching == true)
{
crouch();
}
else
{
stand();
}
}
Assets/Standard Assets/Scripts/Character Scripts/Crouch.js(3,9): UCE0001: ';' expected. Insert a semicolon at the end.
You need Get$$anonymous$$ey ins$$anonymous$$d of Get$$anonymous$$eyDown.
At Nelia: You mix c# and js.
boot isCrounching = false;
must be
var crounching : boolean = false;
Answer by AlucardJay · Feb 11, 2013 at 02:32 AM
The problem is you are using a temporary variable to store your current state, so each frame, it is set to false. Make isCrouching a global variable
var isCrouched : boolean = false;
function Update()
{
if ( Input.GetKeyDown(KeyCode.C) )
{
if ( !isCrouched )
{
isCrouched = true;
crouch();
}
else
{
isCrouched = false;
stand();
}
}
}
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Change something in a script with other script 1 Answer
lighting click help 3 Answers
Crouch jump bug. 1 Answer
How to make my game objects spawn when i walk through collider 3 Answers