Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Move player forward relative to direction (pointing towards) using touch input

Discussion in 'Scripting' started by mhedges, Feb 25, 2017.

  1. mhedges

    mhedges

    Joined:
    Feb 23, 2017
    Posts:
    13
    Hello -

    Continuing to port my first game to Unity, I've noticed that many tutorials are based on the use of the computer keyboard and not touch input from a mobile device. That has stalled me a bit, since I am using touch input.

    That said, I have a script for the buttons and a script for the player controller. I am convinced the script for the button works fine, but am stuck with the player controller. This is what I have in the player controller script thus far:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerShipControl : MonoBehaviour {
    5.  
    6.     public float rotationspeed = 5f;
    7.     private Rigidbody2D rb;
    8.  
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.         rb = GetComponent<Rigidbody2D> ();
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update () {
    17.  
    18.         if (TouchJoyPad.Button01) {
    19.             transform.Rotate (0, 0, rotationspeed);
    20.         }
    21.         if (TouchJoyPad.Button02) {
    22.             transform.Rotate (0, 0, rotationspeed * -1);
    23.         }
    24.         if (TouchJoyPad.Button03) {
    25.             //insert code to move forward
    26.         }
    27.     }
    28. }
    I know I must create a variable to assign a speed. Ideally I should accelerate to a certain max speed when touch is pressed, and coast and stop when touch is released.

    My object PlayerShip starts pointing up. How do I know its direction and how do I accelerate relative to its direction?

    Thanks, regards,
    Marcos
     
  2. mhedges

    mhedges

    Joined:
    Feb 23, 2017
    Posts:
    13
    If it helps:

    My PlayerShip faces up, and rotates on its Z axis. Thus it's not a top-down, but a front-facing scene.