diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index a18dbce..8a7c8d8 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -18,13 +18,14 @@ public class PlayerController : MonoBehaviour { public float minSwipeDistY; private Vector2 startPos; public bool downButton; - + private float swipeValue; //stores the specific block the player touches at an instance in time private GameObject currentBlock; // Use this for initialization - void Start () { + void Start () + { //Reference so I don't have to type this long thing out repeatedly rb2d = GetComponent (); anim = GetComponent(); @@ -37,13 +38,19 @@ void Start () { } } - void Update () { - if(Input.GetKeyDown(KeyCode.S)) { - downButton = true; - } else { - downButton = false; + void Update () + { + + #if UNITY_STANDALONE + if(Input.GetKeyDown(KeyCode.S) ) + { + downButton = true; + } + else + { + downButton = false; } - #if UNITY_EDITOR + //The jump mechanic if (blockJumpTimer > 0) { @@ -52,7 +59,8 @@ void Update () { else if (Input.GetKeyDown(KeyCode.Space)) { //Player can jump if they are falling or reached max height - if (rb2d.velocity.y <= 0) { + if (rb2d.velocity.y <= 0) + { //Making it directly alter vertical velocity so jump is instantaneous as well //as not super powerful. rb2d.velocity = new Vector2(rb2d.velocity.x, jumpSpeed); @@ -62,71 +70,92 @@ void Update () { blockJumpTimer = blockJumpTimerDuration; } } + //Fixes infinite jump on the ceiling if(raycast.collisionUp) { - blockJumpTimer = blockJumpTimerDuration; + blockJumpTimer = blockJumpTimerDuration; } //Fixes the wall climb bug for when you get stuck on a wall if you press the direction key on a wall that you are facing if(raycast.collisionLeft && !raycast.collisionDown){ - moveHorizontal = Input.GetKey(KeyCode.A) ? 0 : Input.GetAxis ("Horizontal"); + moveHorizontal = Input.GetKey(KeyCode.A) ? 0 : Input.GetAxis ("Horizontal"); } else if (raycast.collisionRight && !raycast.collisionDown){ - moveHorizontal = Input.GetKey(KeyCode.D) ? 0 : Input.GetAxis ("Horizontal"); + moveHorizontal = Input.GetKey(KeyCode.D) ? 0 : Input.GetAxis ("Horizontal"); } else { - moveHorizontal = Input.GetAxis ("Horizontal"); + moveHorizontal = Input.GetAxis ("Horizontal"); } - #else - if (Input.touchCount > 0) { - if (leftside.Contains (Input.GetTouch(0).position)) { - moveHorizontal = -1; - } - else if (rightside.Contains (Input.GetTouch(0).position)) { - moveHorizontal = 1; - } - } - else { - moveHorizontal = 0; - } - - //swipe up to move up - //to dowuble jump, finger has to go past the minimum distance and swip again. - - if (Input.touchCount > 0) - { - Touch touch = Input.touches[0]; - - switch (touch.phase) - { - case TouchPhase.Began: - startPos = touch.position; - break; - - case TouchPhase.Ended: - float swipeDistVertical = (new Vector3(0, touch.position.y, 0) - new Vector3(0, startPos.y, 0)).magnitude; - - if (swipeDistVertical > minSwipeDistY) - { - float swipeValue = Mathf.Sign(touch.position.y - startPos.y); - - if (swipeValue > 0) - { - if (rb2d.velocity.y <= 0) - { - rb2d.velocity = new Vector2(rb2d.velocity.x, jumpSpeed); - } - } - } - break; - - } - } +#else + if(swipeValue < 0) + { + downButton = true; + swipeValue = 0; + } + else + { + downButton = false; + } - #endif + if (Input.touchCount > 0) + { + if (leftside.Contains (Input.GetTouch(0).position)) + { + moveHorizontal = (raycast.collisionLeft && !raycast.collisionDown) ? 0 : -1; + } + else if (rightside.Contains (Input.GetTouch(0).position)) + { + moveHorizontal = (raycast.collisionRight && !raycast.collisionDown) ? 0 : 1; + } + } + else + { + moveHorizontal = 0; + } + //swipe up to move up + //to dowuble jump, finger has to go past the minimum distance and swip again. + //Fixes infinite jump on the ceiling + if(raycast.collisionUp) + { + blockJumpTimer = blockJumpTimerDuration; + } + if (blockJumpTimer > 0) + { + blockJumpTimer -= Time.deltaTime; + } + else if (Input.touchCount > 0) + { + Touch touch = Input.touches[0]; + switch (touch.phase) + { + case TouchPhase.Began: + startPos = touch.position; + break; + case TouchPhase.Ended: + float swipeDistVertical = (new Vector3(0, touch.position.y, 0) - new Vector3(0, startPos.y, 0)).magnitude; + if (swipeDistVertical > minSwipeDistY) + { + swipeValue = Mathf.Sign(touch.position.y - startPos.y); + if (swipeValue > 0) + { + if (rb2d.velocity.y <= 0) + { + rb2d.velocity = new Vector2(rb2d.velocity.x, jumpSpeed); + } + //If player tries to jump before apex, they cannot jump for a set time + else + { + blockJumpTimer = blockJumpTimerDuration; + } + } + } + break; + } + } + #endif currentVelocity = new Vector2(moveHorizontal * speed, rb2d.velocity.y); rb2d.velocity = currentVelocity;