Hi guys. I have a project going on where I control a flying object. It does not move on the y-axis, but it is able to move both on the x-axis and the z-axis.
What I want to achieve is to add a virtual joystick for mobile use. How would I go about doing this? I have only seen tutorials for this using GetAxis (“Horizontal/Vertical”).
Here is my script for it:
PlaneController2 : MonoBehaviour {
public Animator theAnimator;
float acceleration = 0.0f;
public Text speedText;
public GameObject breakLights;
public GameObject speedLights;
// Use this for initialization
void Start ()
{
theAnimator = GetComponentInChildren <Animator> ();
acceleration = 15.0f;
speedText.text = "Speed: " + Mathf.Round (acceleration);
}
// Update is called once per frame
void Update ()
{
MovePlane ();
RotatePlane ();
speedText.text = "Speed: " + Mathf.Round (acceleration);
}
void MovePlane ()
{
if (Input.GetKey (KeyCode.UpArrow))
{
if (acceleration < 25.0f)
{
acceleration += 0.1f;
}
speedLights.SetActive (true);
}
else
{
speedLights.SetActive (false);
}
if (Input.GetKey (KeyCode.UpArrow) && !Input.GetKey (KeyCode.LeftArrow) && !Input.GetKey (KeyCode.RightArrow))
{
if (acceleration < 25.0f)
{
acceleration += 0.1f;
}
PlaneIdle ();
}
if (Input.GetKey (KeyCode.DownArrow))
{
if (acceleration > 15.0f) {
acceleration -= 0.1f;
}
breakLights.SetActive (true);
}
else
{
breakLights.SetActive (false);
}
if (Input.GetKey (KeyCode.DownArrow) && !Input.GetKey (KeyCode.LeftArrow) && !Input.GetKey (KeyCode.RightArrow))
{
if (acceleration < 15.0f)
{
acceleration -= 0.1f;
}
PlaneIdle ();
}
if (!Input.GetKey (KeyCode.UpArrow) && !Input.GetKey (KeyCode.DownArrow))
{
if (acceleration > 20.0f)
{
acceleration -= 0.1f;
}
if (acceleration < 20.0f)
{
acceleration += 0.1f;
}
PlaneIdle ();
}
transform.Translate (transform.forward * acceleration * Time.deltaTime, Space.World);
}
void RotatePlane ()
{
if (Input.GetKey (KeyCode.LeftArrow))
{
transform.Rotate (transform.up, -100.0f * Time.deltaTime, Space.World);
//planeLeft = true;
//planeIdle = false;
//theAnimator.SetBool ("PlaneIdle", false);
//theAnimator.SetBool ("PlaneLeft", true);
PlaneLeft ();
}
if (Input.GetKey (KeyCode.LeftArrow) && Input.GetKey (KeyCode.LeftShift))
{
transform.Rotate (transform.up, -150.0f * Time.deltaTime, Space.World);
//planeLeft = true;
//planeIdle = false;
//theAnimator.SetBool ("PlaneIdle", false);
//theAnimator.SetBool ("PlaneLeft", true);
PlaneExtraLeft ();
}
if (Input.GetKey (KeyCode.RightArrow))
{
transform.Rotate (transform.up, +100.0f * Time.deltaTime, Space.World);
//planeRight = true;
//planeIdle = false;
//theAnimator.SetBool ("PlaneIdle", false);
//theAnimator.SetBool ("PlaneRight", true);
PlaneRight ();
}
if (Input.GetKey (KeyCode.RightArrow) && Input.GetKey (KeyCode.LeftShift))
{
transform.Rotate (transform.up, +150.0f * Time.deltaTime, Space.World);
//planeLeft = true;
//planeIdle = false;
//theAnimator.SetBool ("PlaneIdle", false);
//theAnimator.SetBool ("PlaneLeft", true);
PlaneExtraRight ();
}
}
void PlaneLeft ()
{
theAnimator.SetBool ("PlaneIdle", false);
theAnimator.SetBool ("PlaneExtraLeft", false);
theAnimator.SetBool ("PlaneExtraRight", false);
theAnimator.SetBool ("PlaneRight", false);
theAnimator.SetBool ("PlaneLeft", true);
}
void PlaneExtraLeft ()
{
theAnimator.SetBool ("PlaneIdle", false);
theAnimator.SetBool ("PlaneLeft", false);
theAnimator.SetBool ("PlaneRight", false);
theAnimator.SetBool ("PlaneExtraRight", false);
theAnimator.SetBool ("PlaneExtraLeft", true);
}
void PlaneRight ()
{
theAnimator.SetBool ("PlaneIdle", false);
theAnimator.SetBool ("PlaneLeft", false);
theAnimator.SetBool ("PlaneExtraLeft", false);
theAnimator.SetBool ("PlaneExtraRight", false);
theAnimator.SetBool ("PlaneRight", true);
}
void PlaneExtraRight ()
{
theAnimator.SetBool ("PlaneIdle", false);
theAnimator.SetBool ("PlaneLeft", false);
theAnimator.SetBool ("PlaneExtraLeft", false);
theAnimator.SetBool ("PlaneRight", false);
theAnimator.SetBool ("PlaneExtraRight", true);
}
void PlaneIdle ()
{
theAnimator.SetBool ("PlaneLeft", false);
theAnimator.SetBool ("PlaneExtraLeft", false);
theAnimator.SetBool ("PlaneRight", false);
theAnimator.SetBool ("PlaneExtraRight", false);
theAnimator.SetBool ("PlaneIdle", true);
}
void OnTriggerEnter (Collider other)
{
if (other.tag == "DoubleAcceleration")
{
acceleration = acceleration * 2;
}
}
}