Search Unity

2D sprite has to transform to 0,0 position before game start

Discussion in 'Scripting' started by Llits, Apr 1, 2019.

  1. Llits

    Llits

    Joined:
    Feb 8, 2017
    Posts:
    6
    Alright, I'm going to explain this as best as I can since I've never encountered this sort of problem before. So I'm working on a small project with another gentlemen, and it's a 2D top down game. We found a script that can incorporate both mouse movement and swipe touch movement. The tutorial seemed to work for the most part, even if it was meant for a 3D project. However, we have our player placed more on the left on the X axis and a little higher on the Y axis. So let's just say the current position of the player is -3,5. So, when we press play, the sprite actually moves by itself to position to 0,0 on both axis. Only then can we move freely like intended. We don't want that, and it won't work for us to have the sprite start on 0,0. We are hoping there is someone with more experience than us who can maybe help us with this work around. I've attached our scripts to the thread. To make it run, we attached the swipetest.cs to an empty game object, then dragged and dropped the swipe.cs to the inspector of the empty object and left it in the swipe controls line, then selected a sprite to run the scripts on. Please let me know your suggestions on what we can do to fix our problem.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class SwipeTest : MonoBehaviour
    7. {
    8.     private Vector3 startPosition;
    9.     public Swipe swipeControls;
    10.     public Transform player;
    11.     private Vector3 desiredPosition;
    12.  
    13.  
    14.     private void Start()
    15.     {
    16.      
    17.     }
    18.  
    19.     private void Update()
    20.     {
    21.         if (swipeControls.SwipeLeft)
    22.             desiredPosition += Vector3.left;
    23.         if (swipeControls.SwipeRight)
    24.             desiredPosition += Vector3.right;
    25.         if (swipeControls.SwipeUp)
    26.             desiredPosition += Vector3.up;
    27.         if (swipeControls.SwipeDown)
    28.             desiredPosition += Vector3.down;
    29.  
    30.         player.transform.position = Vector3.MoveTowards(player.transform.position, desiredPosition, 3f * Time.deltaTime);
    31.     }
    32. }
    33.  
    34.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Swipe : MonoBehaviour
    6. {
    7.     private bool tap, swipeLeft, swipeRight, swipeUp, swipeDown;
    8.     private bool isDragging = false;
    9.     private Vector2 startTouch, swipeDelta;
    10.  
    11.     private void Update()
    12.     {
    13.  
    14.         tap = swipeLeft = swipeRight = swipeUp = swipeDown = false;
    15.  
    16.         //Standalone Inputs
    17.         if (Input.GetMouseButtonDown(0))
    18.         {
    19.             tap = true;
    20.             isDragging = true;
    21.             startTouch = Input.mousePosition;
    22.         }
    23.         else if (Input.GetMouseButtonUp(0))
    24.         {
    25.             isDragging = false;
    26.             Reset();
    27.         }
    28.  
    29.         //Mobile Inputs
    30.         if (Input.touches.Length > 0)
    31.         {
    32.             if (Input.touches[0].phase == TouchPhase.Began)
    33.             {
    34.                 tap = true;
    35.                 isDragging = true;
    36.                 startTouch = Input.touches[0].position;
    37.             }
    38.             else if (Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
    39.             {
    40.                 isDragging = false;
    41.                 Reset();
    42.             }
    43.         }
    44.  
    45.         //Calculate Distance
    46.         swipeDelta = Vector2.zero;
    47.         if (isDragging)
    48.         {
    49.             if (Input.touches.Length > 0)
    50.                 swipeDelta = Input.touches[0].position - startTouch;
    51.             else if (Input.GetMouseButton(0))
    52.                 swipeDelta = (Vector2)Input.mousePosition - startTouch;
    53.         }
    54.  
    55.         //Crossing Deadzone
    56.         if (swipeDelta.magnitude > 120)
    57.         {
    58.             //Which Direction?
    59.             float x = swipeDelta.x;
    60.             float y = swipeDelta.y;
    61.             if (Mathf.Abs(x) > Mathf.Abs(y))
    62.             {
    63.                 //Left or Right
    64.                 if (x < 0)
    65.                     swipeLeft = true;
    66.                 else
    67.                     swipeRight = true;
    68.             }
    69.             else
    70.             {
    71.                 //Up or Down
    72.                 if (y < 0)
    73.                     swipeDown = true;
    74.                 else
    75.                     swipeUp = true;
    76.             }
    77.  
    78.  
    79.             Reset();
    80.         }
    81.     }
    82.  
    83.     private void Reset()
    84.     {
    85.         startTouch = swipeDelta = Vector2.zero;
    86.         isDragging = false;
    87.     }
    88.  
    89.     public Vector2 SwipeDelta { get { return swipeDelta; } }
    90.     public bool SwipeLeft { get { return swipeLeft; } }
    91.     public bool SwipeRight { get { return swipeRight; } }
    92.     public bool SwipeUp { get { return swipeUp; } }
    93.     public bool SwipeDown { get { return swipeDown; } }
    94. }
    95.  
     
    Last edited: Apr 2, 2019