- Home /
Question by
Sayden96 · Sep 08, 2021 at 10:19 PM ·
inputscript.checkinput.getbutton
Unable to check input action
I migrate my old player controller to the new input system, but I don't know how to check if my player moves and run the walking animation. If someone shows me a way to do it, I appreciate it. The Script Player:
public class PlayerMovement : MonoBehaviour
{
Rigidbody2D rb;
public Transform groundCheck;
public Transform groundCheckL;
public Transform groundCheckR;
public LayerMask groundLayer;
private float horizontal;
private float speed = 2.5f;
private float jumpingPower = 6f;
private bool isFacingRight = true;
public bool isGrounded;
private bool isShooting;
private bool isAttackPressed;
private bool isAttacking;
bool isFacingLeft;
bool crouch = false;
Animator animator;
private string currentState;
public bool isWalking = false;
//Animation States
const string PLAYER_IDLE = "Player_idleV2";
const string PLAYER_WALK = "Player_walkV2";
const string PLAYER_JUMP = "Player_jump";
const string PLAYER_DOUBLE_JUMP = "Player_doublejump";
const string PLAYER_ATTACK = "Player_throwV2";
const string PLAYER_MOVE_ATTACK = "Player_walk_throw";
const string PLAYER_CROUCH = "Player_crouchV2";
AudioSource audioSource;
public AudioClip ThrowClip;
public AudioClip JumpClip;
public AudioClip CoinClip;
public AudioClip DamageClip;
public AudioClip PickUpClip;
[SerializeField]
private float shootDelay = .5f;
[SerializeField]
private float attackDelay = .5f;
[SerializeField] private GameObject bulletPrefab;
[SerializeField] private Transform bulletSpawnPos;
public GameObject ColliderPlayer;
public GameObject ColliderCrouch;
void Start()
{
rb = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
audioSource = GetComponent<AudioSource>();
}
void ChangeAnimationState(string newState)
{
//stop the same animation from interrupting itself
if (currentState == newState) return;
//play the animation
animator.Play(newState);
//reassign the current state
currentState = newState;
}
// Update is called once per frame
void Update()
{
if (Physics2D.Raycast(groundCheck.position, Vector2.down, 0.25f, groundLayer) || Physics2D.Raycast(groundCheckL.position, Vector2.down, 0.25f, groundLayer) || Physics2D.Raycast(groundCheckR.position, Vector2.down, 0.25f, groundLayer))
{
isGrounded = true;
}
else
{
isGrounded = false;
}
rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
if (!isFacingRight && horizontal > 0f)
{
Flip();
isFacingLeft = false;
//if (isGrounded && !isAttacking)
}
else if (isFacingRight && horizontal < 0f)
{
Flip();
isFacingLeft = true;
//if (isGrounded && !isAttacking)
}
else
{
if (isGrounded && !isAttacking)
{
isWalking = false;
if (isWalking == false)
{
ChangeAnimationState(PLAYER_IDLE);
}
else
{
ChangeAnimationState(PLAYER_WALK);
}
}
}
if (isAttackPressed)
{
isAttackPressed = false;
if (!isAttacking)
{
isAttacking = true;
if (isGrounded && !isWalking)
{
ChangeAnimationState(PLAYER_ATTACK);
}
else if (isGrounded && isWalking)
{
ChangeAnimationState(PLAYER_MOVE_ATTACK);
}
Invoke("AttackComplete", attackDelay);
}
}
}
private bool IsGrounded()
{
return Physics2D.Raycast(groundCheck.position, Vector2.down, 0.25f, groundLayer) || Physics2D.Raycast(groundCheckL.position, Vector2.down, 0.25f, groundLayer) || Physics2D.Raycast(groundCheckR.position, Vector2.down, 0.25f, groundLayer);
}
private void Flip()
{
isFacingRight = !isFacingRight;
Vector3 localScale = transform.localScale;
localScale.x *= -1f;
transform.localScale = localScale;
}
void AttackComplete()
{
isAttacking = false;
}
public void Move(InputAction.CallbackContext context)
{
horizontal = context.ReadValue<Vector2>().x;
if (context.performed)
{
//button is press
isWalking = true;
}
else if (context.canceled)
{
//button is released
isWalking = false;
}
}
public void Jump(InputAction.CallbackContext context)
{
ChangeAnimationState(PLAYER_JUMP);
if (context.performed && IsGrounded())
{
rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
}
if (context.canceled && rb.velocity.y > 0f)
{
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
}
}
public void Attack(InputAction.CallbackContext context)
{
if (context.performed)
{
if (isShooting) return;
//shoot
//ChangeAnimationState(PLAYER_ATTACK);
//animator.Play("Player_throw");
isShooting = true;
isAttackPressed = true;
ColliderPlayer.SetActive(true);
ColliderCrouch.SetActive(false);
crouch = false;
audioSource.PlayOneShot(ThrowClip, 0.5F);
//instantiate and shoot bullet
GameObject b = Instantiate(bulletPrefab);
b.GetComponent<KnifeScript>().StartShoot(isFacingLeft);
b.transform.position = bulletSpawnPos.transform.position;
Invoke("ResetShoot", shootDelay);
}
}
void ResetShoot()
{
isShooting = false;
}
public void SetBulletPrefab(GameObject newBullet)
{
bulletPrefab = newBullet;
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613042759im_/https://answers.unity.com/themes/thub/images/avi.jpg)