Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

189 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Wanting timer to run after single click 2 Answers

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

Using input.getbuttondown to play audio = NOT WORKING? :( 2 Answers

Accelerometer or Gyroscope for movement? 0 Answers

How do I disable player input action map and enable the UI action map? 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges