- Home /
 
 
               Question by 
               Guilherme_Aguiar · Jan 18, 2017 at 09:25 PM · 
                movementtouchmovement scripttouch controlstouchphase  
              
 
              How could I implement diagonal movement in this code?
So, I am trying to make a top-down airship game for mobile. But I'm having trouble implementing the movement to it. Currently I have a 4 movement system working (which I got from a script on stack overflow).
I was hoping someone could give me an idea to implement diagonal movement to it. Here's the 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
              
 
               
              Your answer
 
             Follow this Question
Related Questions
Touch buttons for step movememt 3 Answers
How to make basic touch screen controls 1 Answer
Unity Touch Help! 0 Answers
Trying to get the player to face the direction of a touch 1 Answer
Paddle script for Pong-based video game for Android 0 Answers