From b7db9e920f6c432968c81cb26b0b962854cc4d5a Mon Sep 17 00:00:00 2001 From: Benjamin Seo Date: Mon, 4 Apr 2016 11:08:46 -0700 Subject: [PATCH 1/2] mobile slide down for phasing through platform just added the the slide down option for mobile --- Assets/Scripts/PlayerController.cs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index a18dbce..fa77ba1 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -38,7 +38,7 @@ void Start () { } void Update () { - if(Input.GetKeyDown(KeyCode.S)) { + if(Input.GetKeyDown(KeyCode.S) ) { downButton = true; } else { downButton = false; @@ -76,8 +76,9 @@ void Update () { moveHorizontal = Input.GetAxis ("Horizontal"); } - #else - if (Input.touchCount > 0) { + +#else + if (Input.touchCount > 0) { if (leftside.Contains (Input.GetTouch(0).position)) { moveHorizontal = -1; } @@ -117,6 +118,16 @@ void Update () { rb2d.velocity = new Vector2(rb2d.velocity.x, jumpSpeed); } } + else if(swipeValue < 0) + { + downButton = true; + } + else + { + downButton = false; + } + + } break; From 28b68fc12cf6456da463fa87589af9239e29af6b Mon Sep 17 00:00:00 2001 From: ken_miller Date: Tue, 5 Apr 2016 15:12:45 -0700 Subject: [PATCH 2/2] Fixed Mobile Controls The swipe down platform works great. This update merely updates the mobile controls to mirror the performance and feel of the computer version. -Fixed the spacing of the code blocks, makes analyzing the code easier. Some blocks were indented too far. -Implemented the ray casting for mobile. -Before players would continuously fall through subsequent platforms. Took the swipe down for phasing through platforms and put it on top of the mobile controls block to fix this. Had to take variable swipeValue and turn it into a class data member instead of a local function data member to achieve this. --- Assets/Scripts/PlayerController.cs | 156 ++++++++++++++++------------- 1 file changed, 87 insertions(+), 69 deletions(-) diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index fa77ba1..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,82 +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); - } - } - else if(swipeValue < 0) - { - downButton = true; - } - else - { - downButton = false; - } - - - } - break; - - } - } + 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;