Search Unity

Question Help with a Swipe to Dash move

Discussion in 'Scripting' started by ProductiveHippo, Jan 17, 2022.

  1. ProductiveHippo

    ProductiveHippo

    Joined:
    Jan 7, 2022
    Posts:
    9
    Hi there, I'm working on a game where the user can swipe on the screen and the transform will move in the direction of the swipe over a pre-defined distance (dashDistance).

    The start dashing method receives the fingerDown and fingerUp vector2 data, which I then convert into world coordinates and calculate the angle. I then use this angle to adjust the deltas. If i replace adjustedDelta values with baseDelta values I get a mirror of the original swipe, which is good, but not what I want. The transform should continue along that vector defined by dashDistance.

    I think there is something wrong with how I am calculating the original angle of the swipe and the resulting adjustedDeltaX / Y values. The transform moves all over the place.

    I think I've been staring at this too long! Any help much appreciated :)

    Code (CSharp):
    1.     private IEnumerator StartDashing(Vector2 fingerDown, Vector2 fingerUp)
    2.     {
    3.         //convert from screen to world coordinates
    4.         Vector2 start = Camera.main.ScreenToWorldPoint(fingerDown);
    5.         Vector2 end = Camera.main.ScreenToWorldPoint(fingerUp);
    6.  
    7.         //establish geometry of input swipe
    8.         float c = Vector2.Distance(start, end);
    9.         float baseDeltaX = end.x - start.x;
    10.         float baseDeltaY = end.y - start.y;
    11.  
    12.  
    13.         //create enlarged "triangle" geometry version based on dashdistance, providing new deltas
    14.         Vector2 swipe = end - start;
    15.         float angle = Vector2.Angle(Vector2.up, swipe);
    16.  
    17.         float adjustedDeltaX = dashDistance * Mathf.Sin(angle);
    18.         float adjustedDeltaY = dashDistance * Mathf.Cos(angle);
    19.        
    20.         //load new target from revised geometry
    21.         Vector2 targetPosition = new Vector2(this.transform.position.x + adjustedDeltaX, this.transform.position.y + adjustedDeltaY;
    22.  
    23.  
    24.         //begin dash to new desination
    25.         normalMovementEnabled = false;
    26.         canDash = false;
    27.  
    28.         float timer = 3;
    29.         Vector2 playerStartingPosition = this.transform.position;
    30.  
    31.         while (Vector2.Distance(this.transform.position, targetPosition) > 0.1f
    32.             && timer >= 0 && dashInterupt == false
    33.             && Vector2.Distance(playerStartingPosition, this.transform.position) < dashDistance)
    34.         {
    35.             transform.position = Vector3.MoveTowards(this.transform.position, targetPosition, Time.deltaTime * dashSpeed);
    36.             timer -= Time.deltaTime;
    37.             yield return new WaitForEndOfFrame();        
    38.         }
    39.         canDash = true;
    40.         normalMovementEnabled = true;
    41.  
    42.         //debug
    43.         Debug.Log("PLAYER CONTROLLER DASH DEBUGGER. Touch started at: " + fingerDown + " (World pos: " + start + ") . " +
    44.             "Touch finished at: " + fingerUp + "(World pos:" + end + "). Angle of swipe: " + angle + "Distance Between Swipe in World Pos (c):" + c +
    45.             "deltaX: " + baseDeltaX + " adjustedDeltaX:  " + adjustedDeltaX + "baseDeltaY: " + baseDeltaY + "adjustedDeltaY :" + adjustedDeltaY);
    46.         yield return null;
     
    Last edited: Jan 17, 2022
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,727
    I suspect you're giving bad data to ScreenToWorldPoint. You must fill out the Z coordinate of the finger position but you don't show that code here. See the docs for what it expects. It's important.

    Set yourself up for a win like this:

    - make a new test scene
    - put primitives in the scene at known locations ( say (0,0) and (5,0)) that the camera can see
    - run the swipey code and nail down the coordinates, proving they are at least close to where the visible primitives are

    This will isolate if you are giving bad data and help you nail down the behavior you want.