Question by
unity_pOJsuJBpb_HCGA · Jul 07, 2018 at 11:36 PM ·
keyboard input
My getkeydown is not being recognized
Hey its a simple script whit animation params , but I don't know why my if with getkeydown is not executing!
public class PlayerMovement : MonoBehaviour {
private Rigidbody2D rigidbodyMurrilo;
public float speed;
public Animator anim;
public bool FacingRigth;
public Transform transform;
private void Start()
{
rigidbodyMurrilo = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
FacingRigth = true;
}
void FixedUpdate()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
HandleMovement(horizontal);
Flip(horizontal);
Debug.Log(anim.GetBool("KeyUp"));
}
void HandleMovement(float horizontal)
{
rigidbodyMurrilo.velocity = new Vector2(horizontal * speed, rigidbodyMurrilo.velocity.y);
}
private void Update()
{
if(Input.GetKeyDown("d") || Input.GetKeyDown("left") || Input.GetKeyDown("a") || Input.GetKeyDown("rigth"))
{
anim.Play("MoveRigth");
anim.SetBool("KeyUp", false);
}
if(Input.GetKeyUp("d") || Input.GetKeyUp("left") || Input.GetKeyUp("a") || Input.GetKeyUp("rigth"))
{
anim.SetBool("KeyUp",true);
}
}
private void Flip (float horizontal){
if(horizontal > 0 && !FacingRigth || horizontal<0 && FacingRigth)
{
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
FacingRigth = !FacingRigth;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Mobile into PC 1 Answer
Remap keyboard/keys for InputField 0 Answers
How to detect a keyboard key held down? 2 Answers
A keybinding system in unity 1 Answer
How can I use a navmesh with keyboard input to move an object? 1 Answer