- Home /
Swipe controls work on Android but not on iOS?
Hi Everyone,
So I got a program which move a player to three places and the user can change places by swiping left or right. The issue is that it works on Android phones but on iOS phones? Why is this?
Here is the code:
//---------------------Touch Inputs------------------------- private void touch_controls() { tap = swipe_left = swipe_right = false;
//---------------mouse inputs-------------------
if (Input.GetMouseButtonDown(0))
{
tap = true;
is_dragging = true;
start_touch = Input.mousePosition;
}
else if (Input.GetMouseButtonUp(0))
{
is_dragging = false;
reset_movements();
}
//-------------mobile inputs--------------
//get touch information
if (Input.touches.Length > 0)
{
if(Input.touches[0].phase == TouchPhase.Began)
{
tap = true;
is_dragging = true;
start_touch = Input.touches[0].position;
}
else if(Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
{
is_dragging = false;
reset_movements();
}
}
//Calculate the distance
swipe_delta = Vector2.zero;
if(is_dragging)
{
if(Input.touches.Length > 0)
{
swipe_delta = Input.touches[0].position - start_touch;
}
else if (Input.GetMouseButton(0))
{
swipe_delta = (Vector2)Input.mousePosition - start_touch;
}
}
//the length of how long the swipe must be for player movement
float swipe_length = 75;
if(swipe_delta.magnitude > swipe_length)
{
//get direction
float x = swipe_delta.x;
float y = swipe_delta.y;
//horizontal swipes
if(Mathf.Abs(x) > Mathf.Abs(y))
{
// left or right swipe
if(x < 0)
{
//swipe left
swipe_left = true;
}
else
{
//swipe right
swipe_right = true;
}
}
//reset values
reset_movements();
}
}
private void reset_movements()
{
//reset position after swipe
start_touch = swipe_delta = Vector2.zero;
is_dragging = false;
}
public Vector2 get_swipe_delta()
{
return swipe_delta;
}
public bool get_swipe_left()
{
return swipe_left;
}
public bool get_swipe_right()
{
return swipe_right;
}
private void touch_movements()
{
//position for player
float left_side = -game_controller.get_camera_width() / 2 + 1;
float middle = 0;
float right_side = game_controller.get_camera_width() / 2 - 1;
//speed for movement
float player_speed = 8f;
//swipe left function
if (get_swipe_left())
{
if (player.transform.position.x == right_side)
{
target_position = new Vector3(middle, player.transform.position.y, player.transform.position.z);
}
else if (player.transform.position.x == middle)
{
target_position = new Vector3(left_side, player.transform.position.y, player.transform.position.z);
}
}
//swipe right function
if (get_swipe_right())
{
if (player.transform.position.x == left_side)
{
target_position = new Vector3(middle, player.transform.position.y, player.transform.position.z);
}
else if (player.transform.position.x == middle)
{
target_position = new Vector3(right_side, player.transform.position.y, player.transform.position.z);
}
}
player.transform.position = Vector3.MoveTowards(player.transform.position, target_position, player_speed * Time.deltaTime);
}
Kind regards
Comment
We can make a guess but there's no way to know exactly without taking a look at the code.
Your answer
Follow this Question
Related Questions
App will not appear on my iPhone! Help! 0 Answers
Accessing the volume buttons on iPhone for some functionality through Unity 1 Answer
Simultaneous Android and iOS development 1 Answer
How does updating a game work? 0 Answers
Device Auto-Rotation 1 Answer