this is My Code:
if ( run && Input.GetKeyDown (KeyCode.K)) {
anim.SetInteger ("State", 4);
slide = true;
}
if (Input.GetKeyUp (KeyCode.K)) {
slide = false;
anim.SetInteger ("State", 0);
}
My problem: if I hold the ( KeyCode.K
) than slide
is still true
which I don´t want.
I need set slide
to true
just once, or to false
after 2 seconds only.
I hope you all understand what I want.
I would suggest making slide
a property which will set your integer :
public bool Slide
{
get { return slide; }
set
{
if ( value == slide ) return;
slide = value;
if( slide ) anim.SetInteger("State", 4);
else anim.SetInteger("State", 0);
}
}
One of the many possible options would be to create a Coroutine
that just resets the flag back to false
:
IEnumerator StartSlide()
{
const float MAX_TIME = 2.0f; // time to wait before resetting the flag
Slide = true;
float timeNow = 0.0f;
while ( timeNow < MAX_TIME && Slide )
{
timeNow += Time.deltaTime;
yield return new WaitForEndOfFrame();
}
Slide = false;
}
And to use this you can call :
if ( run && Input.GetKeyDown(KeyCode.K) && !Slide )
{
StartCoroutine(StartSlide());
}
You can also use this with GetKeyUp
method :
if ( Input.GetKeyUp(KeyCode.K) && Slide )
{
Slide = false;
}
void Update{
if ( key_K && run && Input.GetKeyDown (KeyCode.K)) {
key_K = false;
StartCoroutine ("coRoutine_keyK");
}
}
IEnumerator coRoutine_keyK(){
slide = true;
anim.SetInteger ("State", 4);
yield return new WaitForSeconds (0.5f);
slide = false;
anim.SetInteger ("State", 0);
key_K = true;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.