This question was
closed Jan 18, 2017 at 05:48 PM by
AurimasBlazulionis for the following reason:
Asks for a script, is not specific enough
Question by
Guilherme_Aguiar · Jan 18, 2017 at 05:39 PM ·
movementnoobtouch controlsdiagonal
How could I implement diagonal movement in this code?
public class PlayerC : MonoBehaviour
{
public Vector2 startPos;
public Vector2 endPos;
public bool fingerHold = false;
public float speed = 50f;
void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
startPos = touch.position;
fingerHold = true;
}
else if (touch.phase == TouchPhase.Moved)
{
endPos = touch.position;
}
else if (touch.phase == TouchPhase.Ended)
{
fingerHold = false;
}
}
if (fingerHold)
{
float deltaX = endPos.x - startPos.x;
float deltaY = endPos.y - startPos.y;
bool horizontal = false;
if (Mathf.Abs(deltaX) > Mathf.Abs(deltaY))
horizontal = true;
if (horizontal)
{
if (deltaX < 0)
transform.Translate(Vector3.left * Time.deltaTime * speed);
else if (deltaX > 0)
transform.Translate(Vector3.right * Time.deltaTime * speed);
}
else
{
if (deltaY < 0 )
transform.Translate(Vector3.down * Time.deltaTime * speed);
else if (deltaY > 0 )
transform.Translate(Vector3.up * Time.deltaTime * speed);
}
}
}
}
Comment
Follow this Question
Related Questions
Character Diagonal Movement Issue 3 Answers
Swipe camera rotation controller (HELP) 0 Answers
Problems with mobile touch movements. 0 Answers
Problem with Swipe and rb.MovePosition 0 Answers
Diagonal character movement 0 Answers