- Home /
Problem with 3D mobile diagonal swipe controls
I've been trying out diagonal swipe controls on a 3D mobile game where the player moves North East, North West, South East, and South West depending on the diagonal direction the player swipes
(The main camera is set so that it looks at the plane that the playerobject is on from a diagonal of the plane instead of the side).
But When I test the program on the Phone, not matter which direction I swipe, the player will only travel South West.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MobileControls : MonoBehaviour
{
Vector3 initTouchPos;
private void FixedUpdate()
{
restart:
foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)// detects finger on screen
{
initTouchPos = touch.position;// sets initial finger position
}
if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
{
if (touch.phase == TouchPhase.Moved)
{
float initX = initTouchPos.x, initY = initTouchPos.y;
Vector3 touchDeltaPos = Input.GetTouch(0).deltaPosition;// gets coordinatesof delta position
float deltaX = touchDeltaPos.x, deltaY = touchDeltaPos.y;// sets sets x & y position of delta touch to "deltaX" & "deltaY"
if (deltaX < initX && deltaY < initY)//SW
{
transform.Translate(Vector3.left);
}
else if (deltaX > initX && deltaY < initY)//SE
{
transform.Translate(Vector3.back);
}
else if (deltaX < initX && deltaY > initY)//NW
{
transform.Translate(Vector3.forward);
}
else if (deltaX > initX && deltaY > initY)//NE
{
transform.Translate(Vector3.right);
}
}
else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)// detects end of touch
{
goto restart;// restarts touch detection fron "restart"
}
}
}
}
}
Your answer
Follow this Question
Related Questions
Deactivate Swipe Controls on Pause? 0 Answers
Swipe controls error "Cannot implicitly convert type 'UnityEngine.Touch' to 'Touch' " 1 Answer
Multi touches problem 0 Answers
Mobile Swipe Gesture to flick object in 3D space 0 Answers
How do I make a bowling ball curve and gain friction in certain points? 0 Answers