Space Shooter Mobile Touch Controls Issue
Hey there everyone,
So a while back I worked my way through the Unity Space Shooter tutorial to acquaint myself more with the 3D aspects of unity (I have been primarily developing in 2D). Everything went fine after looking into the differences between the older and newer Unity versions until I tried modifying the controls to work with touch.
I used this live training as a reference to get me started but after completing it I noticed a peculiar bug. Whenever I am using my left thumb to fly the ship around and press the "fire button" at the same time the ship veers strongly to the right side of the screen as if the touch event is being picked up by the script in my "movement zone"
I have been scanning the code back and forth and have tried several rounds of debugging, using the debug logger and a number of the touch events, yet have been unable to find the cause of the bug.
I am becoming more and more convinced that it could be a setting for the canvas in my editor that is wrong but I have run out of ideas.
Can anyone shed some light on this for me?
SimpleTouchAreaButton
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
public class SimpleTouchAreaButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
private bool touched;
private int pointerID;
private bool canFire;
void Awake () {
touched = false;
}
public void OnPointerDown (PointerEventData data) {
if (!touched) {
touched = true;
pointerID = data.pointerId;
canFire = true;
}
}
public void OnPointerUp (PointerEventData data) {
if (data.pointerId == pointerID) {
canFire = false;
touched = false;
Debug.Log (Input.touchCount);
}
}
public bool CanFire () {
return canFire;
}
}
SimpleTouchPad
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
public class SimpleTouchPad : MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler {
public float smoothing;
private Vector2 origin;
private Vector2 direction;
private Vector2 smoothDirection;
private bool touched;
private int pointerID;
void Awake () {
direction = Vector2.zero;
touched = false;
}
public void OnPointerDown (PointerEventData data) {
if (!touched) {
touched = true;
pointerID = data.pointerId;
origin = data.position;
}
}
public void OnDrag (PointerEventData data) {
if (data.pointerId == pointerID) {
Vector2 currentPosition = data.position;
Vector2 directionRaw = currentPosition - origin;
direction = directionRaw.normalized;
Debug.Log (direction);
}
}
public void OnPointerUp (PointerEventData data) {
if (data.pointerId == pointerID) {
direction = Vector2.zero;
touched = false;
}
}
public Vector2 GetDirection () {
smoothDirection = Vector2.MoveTowards (smoothDirection, direction, smoothing);
return smoothDirection;
}
}
PlayerController
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary {
public float xMin, xMax, zMin, zMax;
}
public class PlayerController : MonoBehaviour {
public Rigidbody rigidbody;
public float speed;
public float tilt;
public Boundary boundary;
public GameObject shot;
public Transform shotSpawn;
public float fireRate;
public SimpleTouchPad touchPad;
public SimpleTouchAreaButton areaButton;
private float nextFire;
private Quaternion calibrationQuaternion;
private AudioSource sound;
void Start () {
sound = GetComponent<AudioSource> ();
CalibrateAccelerometer ();
}
void Update () {
if (Time.time > nextFire) {
if (areaButton.CanFire ()) {
//if (areaButton.CanFire () || (Application.platform != RuntimePlatform.IPhonePlayer && Input.GetButton("Fire1"))) {
nextFire = Time.time + fireRate;
Instantiate(shot, shotSpawn.position, shotSpawn.rotation) ;
sound.Play();
}
}
}
void FixedUpdate () {
// The Vector3 movement is overwritten once each update by a platform dependant code block
Vector3 movement;
//if (Application.platform == RuntimePlatform.IPhonePlayer) {
Vector2 direction = touchPad.GetDirection ();
movement = new Vector3(direction.x, 0.0f, direction.y);
// Iphone input scheme using the accelerometer
// Vector3 accelerationRaw = Input.acceleration;
// Vector3 acceleration = FixAccelleration (accelerationRaw);
// Vector3 movement = new Vector3(acceleration.x, 0.0f, acceleration.y);
// } else {
// float moveHorizontal = Input.GetAxis ("Horizontal");
// float moveVertical = Input.GetAxis ("Vertical");
//
// movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
// }
rigidbody.velocity = movement * speed;
rigidbody.position = new Vector3
(
Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
);
rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
}
void CalibrateAccelerometer () {
Vector3 accelerationSnapshot = Input.acceleration;
Quaternion rotateQuaternion = Quaternion.FromToRotation (new Vector3 (0.0f, 0.0f, -1.0f), accelerationSnapshot);
calibrationQuaternion = Quaternion.Inverse (rotateQuaternion);
}
Vector3 FixAcceleration (Vector3 acceleration) {
Vector3 fixedAcceleration = calibrationQuaternion * acceleration;
return fixedAcceleration;
}
}
So I tried some more things and I wondered if maybe my touchID's were not setting correctly, I noticed that the touchID for my movement zone would ALWAYS be -1 and the touchID for my fireZone would always be -2.
Adding the following check to my movement zone did not alter the behaviour in any way:
public void OnPointerDown (PointerEventData data) {
if (!touched && data.pointerId == -1) {
touched = true;
pointerID = data.pointerId;
origin = data.position;
}
}
It just seems that as long as I am holding my finger down on the "joystick" any other touch will influence the movement regardless.
Answer by Donvermo · Oct 01, 2015 at 06:02 PM
It would seem that the problem lies not in the code but in a possible problem with the Unity Remote app. Compiling the game and running it from the actual device solves the issue.
Thanks for adding this. I was wondering the same thing.