11using System . Collections ;
22using System . Collections . Generic ;
33using UnityEngine ;
4+ using myUIEvents ;
45
56
67namespace Platformer
@@ -10,7 +11,9 @@ public class PlayerController : MonoBehaviour
1011 public float movingSpeed ;
1112 public float jumpForce ;
1213 private float moveInput ;
13-
14+ private bool jumpFlag = false ;
15+ private bool keyboardControlFlag = false ;
16+
1417 private bool facingRight = false ;
1518 [ HideInInspector ]
1619 public bool deathState = false ;
@@ -40,11 +43,62 @@ private void FixedUpdate()
4043 CheckGround ( ) ;
4144 }
4245
46+ private void OnEnable ( )
47+ {
48+ MobileHandler . goLeftEvent . AddListener ( updateLeftMove ) ;
49+ MobileHandler . goRightEvent . AddListener ( updateRightMove ) ;
50+ MobileHandler . stopMovingEvent . AddListener ( stopMove ) ;
51+ MobileHandler . goUpEvent . AddListener ( jump ) ;
52+ Debug . Log ( "All listeners added" ) ;
53+ }
54+
55+ private void OnDisable ( )
56+ {
57+ MobileHandler . goLeftEvent . RemoveListener ( updateLeftMove ) ;
58+ MobileHandler . goRightEvent . RemoveListener ( updateRightMove ) ;
59+ MobileHandler . stopMovingEvent . RemoveListener ( stopMove ) ;
60+ MobileHandler . goUpEvent . RemoveListener ( jump ) ;
61+ Debug . Log ( "All listeners removed" ) ;
62+ }
63+
64+ private void updateLeftMove ( ) // new
65+ {
66+ moveInput = - 1 ;
67+ Debug . Log ( "Left event received" ) ;
68+ }
69+
70+ private void updateRightMove ( ) // new
71+ {
72+ moveInput = 1 ;
73+ }
74+
75+ private void stopMove ( ) // new
76+ {
77+ moveInput = 0 ;
78+ }
79+
80+ private void jump ( )
81+ {
82+ Debug . Log ( "Jump event received" ) ;
83+ jumpFlag = true ;
84+ }
85+
86+
4387 void Update ( )
4488 {
45- if ( Input . GetButton ( "Horizontal" ) )
89+ if ( Input . GetButton ( "Horizontal" ) )
4690 {
4791 moveInput = Input . GetAxis ( "Horizontal" ) ;
92+ keyboardControlFlag = true ;
93+ }
94+ else
95+ {
96+ keyboardControlFlag = false ;
97+ }
98+
99+
100+ if ( moveInput != 0 )
101+ {
48102 Vector3 direction = transform . right * moveInput ;
49103 transform . position = Vector3 . MoveTowards ( transform . position , transform . position + direction , movingSpeed * Time . deltaTime ) ;
50104 animator . SetInteger ( "playerState" , 1 ) ; // Turn on run animation
@@ -53,13 +107,18 @@ void Update()
53107 {
54108 if ( isGrounded ) animator . SetInteger ( "playerState" , 0 ) ; // Turn on idle animation
55109 }
56- if ( Input . GetKeyDown ( KeyCode . Space ) && isGrounded )
110+
111+
112+
113+ if ( ( Input . GetKeyDown ( KeyCode . Space ) || jumpFlag ) && isGrounded )
57114 {
58115 rigidbody . AddForce ( transform . up * jumpForce , ForceMode2D . Impulse ) ;
59116 AudioSource . PlayClipAtPoint ( coinSound . clip , transform . position ) ;
117+ jumpFlag = false ;
60118 }
61119 if ( ! isGrounded ) animator . SetInteger ( "playerState" , 2 ) ; // Turn on jump animation
62120
121+
63122 if ( facingRight == false && moveInput > 0 )
64123 {
65124 Flip ( ) ;
@@ -68,6 +127,13 @@ void Update()
68127 {
69128 Flip ( ) ;
70129 }
130+
131+
132+ /* The eventListener method is triggered only once even if we keep holding the button on UI.
133+ * Whereas for the keyboard navigation it keeps sending the event.
134+ * So setup custom flag to reset moveInput for every frame in case of Keyboard */
135+ if ( keyboardControlFlag )
136+ moveInput = 0 ; // reset moveInput after processing
71137 }
72138
73139 private void Flip ( )
0 commit comments