Search Unity

Moving from point to point with Swipe.

Discussion in 'Scripting' started by Batka, Jul 12, 2016.

  1. Batka

    Batka

    Joined:
    May 23, 2016
    Posts:
    22
    Heyas,

    First time asking question here, mostly because you are an awesome community, and I've found help for anything I've needed. :)

    I am trying to build an endless runner with 3 lanes (subway surfers).
    I mostly accomplished to move the character on the X axis as I wanted it to move (3 values: -3.33, 0, 3.33), but there are some bugs I've encountered and can't manage to fix.

    First, let me share the script:

    Code (CSharp):
    1. public class PlayerController : MonoBehaviour
    2. {
    3.     public static PlayerController pc;
    4.     public float zVelocity = 5f;
    5.     public float increaseValue = 2f;
    6.     public float jumpSpeed = 10f;
    7.     public float gravity = 9.81f;
    8.  
    9.     private CharacterController cc;
    10.  
    11.     private float lerpValue = 0.5f;
    12.     private float direction = 3.33333333f;
    13.     private Vector2 firstPressPos;
    14.     private Vector2 secondPressPos;
    15.     private Vector2 currentSwipe;
    16.     private Vector3 moveDirection = Vector3.zero;
    17.  
    18.  
    19.     void Start()
    20.     {
    21.         if (pc == null)
    22.             pc = this;
    23.         cc = GetComponent<CharacterController>();
    24.        
    25.     }
    26.  
    27.     void Update()
    28.     {
    29.         moveDirection.y -= gravity * Time.deltaTime;
    30.         moveDirection.z = zVelocity;
    31.         cc.Move(moveDirection * Time.deltaTime);
    32.     }
    33.  
    34.     void FixedUpdate()
    35.     {
    36.        
    37.         #region Touch Controller  
    38.         if (Input.touches.Length > 0)
    39.         {
    40.             Touch t = Input.GetTouch(0);
    41.             if (t.phase == TouchPhase.Began)
    42.             {
    43.                 //save began touch 2d point
    44.                 firstPressPos = new Vector2(t.position.x, t.position.y);
    45.             }
    46.             if (t.phase == TouchPhase.Ended)
    47.                 {
    48.                 //save ended touch 2d point
    49.                 secondPressPos = new Vector2(t.position.x, t.position.y);
    50.  
    51.                 //create vector from the two points
    52.                 currentSwipe = new Vector2(secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);
    53.  
    54.                 //normalize the 2d vector
    55.                 currentSwipe.Normalize();
    56.  
    57.                 //swipe up
    58.                 if (currentSwipe.y > 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f && cc.isGrounded)
    59.                 {
    60.                     moveDirection.y = jumpSpeed;
    61.                    
    62.                 }
    63.  
    64.                 //swipe down
    65.                 if (currentSwipe.y < 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f)
    66.                 {
    67.                     Debug.Log("down swipe");
    68.                 }
    69.  
    70.                 //swipe left
    71.                 if (currentSwipe.x < 0 && currentSwipe.y > -0.5f && currentSwipe.y < 1f && lerpValue != 0)
    72.                 {
    73.                     lerpValue-= 0.5f;
    74.                 }
    75.                 //swipe right
    76.                 if (currentSwipe.x > 0 && currentSwipe.y > -0.5f && currentSwipe.y < 1f && lerpValue != 1)
    77.                 {
    78.                     lerpValue += 0.5f;
    79.                 }
    80.  
    81.                 transform.position = new Vector3(Mathf.Lerp(-direction, direction, lerpValue), transform.position.y, transform.position.z);
    82.             }
    83.         }
    84.         #endregion

    Ok so the character is moving, and jumping, but first of all it's not seamless.
    It has some weird movement when changing lanes, but maybe this issue is not due this script (maybe camera or animation, even though I tried smooth camera script).

    Anyway, my main problems are:
    - not all swipes are recognized, sometimes when I swipe nothing happens, no matter which direction.
    - very rarely, like once in every 10 play/tests, the character falls under the path he is running over. I can't find why that's happening because it happens really rarely.


    Thanks in advance,
     
  2. Batka

    Batka

    Joined:
    May 23, 2016
    Posts:
    22
    Bump
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    don't try to pick up Input in FixedUpdate, use Update for that. FixedUpdate runs at default at 50fps, so it might miss quick input in the between frames


    edit: in fact you shouldn't be using fixedupdate at all, you're not making any physics based movement calls in your script.
     
  4. SkaredCreations

    SkaredCreations

    Joined:
    Sep 29, 2010
    Posts:
    296
    Since you're using a CharacterController (so physics enabled), really you shouldn't change the transform position manually like you're doing in FixedUpdate else your character will fall when the controller is not over a collider after your change (that is probably what's happening), also changing transform position can easily break your physics logic. Why aren't you just changing moveDirection also for X axis at line 81?
     
  5. Batka

    Batka

    Joined:
    May 23, 2016
    Posts:
    22
    Yea that helped a lot, thanks!


    I would like to, but I can't seem to get the logic. I tried with mathf.Lerp but I was not successful.
    I am really new to both programming and unity, actually this is my first project in both, programming and unity, so if you have any idea on mind I'd like to try it.

    Anyway, the game looks nice when I replaced everything in Update, and smoothed some values with deltaTime.
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    nonsense, CharacterController is specifically not based on the physics engine.

    https://docs.unity3d.com/Manual/class-CharacterController.html