How to handle character movement and behavior?
In order to make character movement seamless we need to create Character Controller 2D script. It will emulate Character Controller behavior to provide all neccessary functionality.
To repeat:
0. Create new script 'CharacterController2D.cs' in 'Character' folder then open script
1. Add private float '_jumpForce', '_MovementSmoothing' and bool 'AirControl' Serialize fields
2. Add private LayerMask '_WhatIsGround', Transform '_GroundCheck' Serialize fields
3. Add private bool 'm_Grounded', '_FacingRight' and RigidBody2D '_RigidBody2D' fields
4. Add private Vector3 '_Velocity' field and public UnityEvent 'OnLandEvent'
5. Add float 'k_GroundedRadius' constant
6. Define private void method 'Awake': GetComponent for _RigidBody2D and OnLandEvent null check init
7. Define private void method 'FixedUpdate':
8. Save _Grounded value, Get colliders with Physics2D OverlapCircleAll method on ground check
9. Iterate over colliders and check if their gameObjects differs from script gameObject.
10. If it is set _Grounded true, Invoke OnLandEvent if _Grounded wasn't true before
11. Define private void method 'Flip': inverse _FacingRight value and transform localScale X axis
12. Define public void method 'Move' with float move, bool jump parameters:
13. Set target Velocity axis X to move multiplied by 10 and Y to same _Rigidbody2D velocity Y axis
14. Set _Rigidbody2D velocity to SmoothDamp between original and target with MovementSmoothing
15. Check _FacingRight state and if move greater or less 0 then Flip character accordingly
16. Check if is _Grounded and jump then reset _Grounded state to false and add jump force
To be continued:
0. Create new script 'Movement.cs' in 'Character' folder then open script
1. Add bool 'jump', float 'horizontalMove' and public float 'runSpeed' fields
2. Add public CharacterController2D 'controller2D' and Animator 'animator' fields
3. Define void method 'Update': set animator isRun float to horizontal axis
4. Then check if jump button is pressed then set animator isJumping bool to true
5. Define public void method 'OnLanding': set animator isJumping bool to false
6. Define private void method 'FixedUpdate': move controller with fixed delta time and jump