Skip to content

Commit 471bdf3

Browse files
committed
UI Btn Functionality + Viewport cam scaler
1 parent 3602015 commit 471bdf3

17 files changed

Lines changed: 2102 additions & 1425 deletions

Assets/2D Platformer/Assets/Prefabs/Main Camera.prefab

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ GameObject:
1313
- component: {fileID: 5439596532191873265}
1414
- component: {fileID: 5439596532191873266}
1515
- component: {fileID: 1300292159520650254}
16+
- component: {fileID: 614257429980546107}
1617
m_Layer: 0
1718
m_Name: Main Camera
1819
m_TagString: MainCamera
@@ -67,10 +68,10 @@ Camera:
6768
width: 1
6869
height: 1
6970
near clip plane: 0.3
70-
far clip plane: 1000
71+
far clip plane: 10000
7172
field of view: 60
7273
orthographic: 1
73-
orthographic size: 5
74+
orthographic size: 8
7475
m_Depth: -1
7576
m_CullingMask:
7677
serializedVersion: 2
@@ -153,3 +154,15 @@ MonoBehaviour:
153154
m_MipBias: 0
154155
m_VarianceClampScale: 0.9
155156
m_ContrastAdaptiveSharpening: 0
157+
--- !u!114 &614257429980546107
158+
MonoBehaviour:
159+
m_ObjectHideFlags: 0
160+
m_CorrespondingSourceObject: {fileID: 0}
161+
m_PrefabInstance: {fileID: 0}
162+
m_PrefabAsset: {fileID: 0}
163+
m_GameObject: {fileID: 5439596532191873264}
164+
m_Enabled: 1
165+
m_EditorHideFlags: 0
166+
m_Script: {fileID: 11500000, guid: 040fa9b2ff3cf3b47a4fbf3a01bc15b0, type: 3}
167+
m_Name:
168+
m_EditorClassIdentifier:

Assets/2D Platformer/Scripts/PlayerController.cs

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections;
22
using System.Collections.Generic;
33
using UnityEngine;
4+
using myUIEvents;
45

56

67
namespace 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()

Assets/Images/game_arrow_3919.png

3.43 KB
Loading

Assets/Images/game_arrow_3919.png.meta

Lines changed: 169 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)