- Home /
Question by
ShiningShisa · Jan 31, 2013 at 03:15 PM ·
animationbooleandelay
Detect input, return true for 3s then return false
Basically I started scripting for the first time yesterday and want to detect a button input, "down", that returns a value ,true, for a given amount of time (roughly 2 seconds). This is so that an animation can play all the way through once it's triggered by the true value rather than stopping after a split second.
the code is:
//A custom Controller for a side scrolling character, Horizontal for back and forth, vertical for look/aim up down, button for jump, crouch, action
var speed : float = 3.0;
var jumpSpeed : float = 6.0;
var gravity : float = 20.0;
// Back and forward movement code
private var moveDirection : Vector3 = Vector3.zero;
function Update(){
var horizmove : float = Input.GetAxis("Horizontal");
Debug.Log(horizmove);
var controller : CharacterController = GetComponent(CharacterController);
if(controller.isGrounded) {
moveDirection = Vector3(0,0,Input.GetAxis("Horizontal"));
moveDirection *= speed;
if(Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
animator = GetComponent(Animator);
var charwalkspeed : float;
GetComponent(Animator);
charwalkspeed == "Walkspeed";
animator.SetFloat("Walkspeed",horizmove);
//Turn around on button press code
var pressturnbutton : boolean;
if(Input.GetButton ("Downbutton")) {
pressturnbutton = true;
}else{
pressturnbutton = false;
}
animator.SetBool("PressTurn",pressturnbutton);
}
Comment
Answer by iwaldrop · Jan 31, 2013 at 04:17 PM
Check out coroutines. Start the animation with GetButtonDown. You can block any number of things from happening when your coroutine starts by settings flags.
http://docs.unity3d.com/Documentation/ScriptReference/index.Coroutines_26_Yield.html
Your answer
