Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by Donvermo · Sep 27, 2015 at 09:57 AM · c#iosinputmobiletouch

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;
     }
 
 }

alt text alt text

canvas.png (155.8 kB)
zone1.png (398.1 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Donvermo · Sep 27, 2015 at 07:52 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image louis_iaeger · Jan 18, 2016 at 02:20 PM 0
Share

Thanks for adding this. I was wondering the same thing.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

32 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to pick up an object when touching a button(Mobile) 0 Answers

Mobile touching screen is pressing mouse 0 0 Answers

How to move a GUI Element 1 Answer

Code help with mobile touch drag 3D Top Down 0 Answers

iOS moving up and down question ( code problem ) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges