0
\$\begingroup\$

i give the running animation to the character and created a flip script...when press A then player move and flip to leff and animation is played ....when press D then player move and flip to write and animation is played.. ..when i press both A and B Simultanious then animation is played but when pressing both at the same time and release one key the player moves in that direction but the animation is not playing

Here are the script of the player:

[SerializeField] float _xSpeed = 10f; [SerializeField] float _jumpForce = 10f; private Rigidbody2D _rb; private float _movement; private float _extraJumps; private bool _isFacingRight; void Start() { _rb = GetComponent<Rigidbody2D>(); } void Update() { _movement = Input.GetAxisRaw("Horizontal"); } void FixedUpdate() { _rb.velocity = new Vector2(_movement * _xSpeed ,_rb.velocity.y); { if(_isFacingRight == true && _movement > 0.1f) { Flip(); } else if(_isFacingRight == false && _movement < -0.1f) { Flip(); } } if(Input.GetKeyDown(KeyCode.Space)) { _rb.velocity = new Vector2(_rb.velocity.x,_jumpForce * Time.deltaTime); } } void Flip() { _isFacingRight = !_isFacingRight; transform.Rotate(0,180f,0); } 

And here is the script for the animation:

[SerializeField] Transform _player; private Animator _animator; void Start() { _animator = GetComponent<Animator>(); } void Update() { transform.position = _player.position; if (Input.GetKeyDown(KeyCode.D)) { _animator.SetBool("Running", true); } else if (Input.GetKeyDown(KeyCode.A)) { _animator.SetBool("Running", true); } else { _animator.SetBool("Running", false); } } 
\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

The method Input.GetKeyDown only returns true during the one Update where the player starts pressing the key, but not on the following updates where they hold it.

I would assume that your intention is to display the running animation while the player is holding down either A or D, and stop it when they are holding neither A nor D? In that case you might want to use the method Input.GetKey, which returns true as long as the player keeps holding the button You could write it like that:

if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D) ) { _animator.SetBool("Running", true); } else { _animator.SetBool("Running", false); } 

But there is also a much shorter way to write this:

_animator.SetBool("Running", Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D)); 

This works because whenever you have a method which expects a true or false as a parameter, you can substitute that by a boolean condition.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ On an unrelated note, I would recommend you to not derive your animation state from your input but instead derive it form what the character is actually doing. But that's a different topic. \$\endgroup\$ Commented Mar 31, 2021 at 15:37
  • \$\begingroup\$ Thank you phillipp i will implement on ur suggestion \$\endgroup\$ Commented Mar 31, 2021 at 16:02

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.