Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by BlacKatFever · Nov 22, 2011 at 09:44 AM · iosperformancetouch controls

Dodgy Touch Controls / Performance Issues on iOS

If it isn't one thing...

With a bit of help from some other Unity Answers members, I managed to get my iOS program up to the point where it can detect flick inputs (as well as direction) and Instantiate game objects at touch. It can even distinguish between the two inputs.

The problem is that it often makes mistakes. I know that some performance issues are to be expected when using Unity Remote but even when run directly off our iPhone 3G - it misreads inputs, converts some swipes into taps, or completely reads a swipe backwards.

Just looking at my code, is there something I'm doing wrong? I mean, it works so doesn't that mean that the code is correct? How am I supposed to compensate for the hardware?

Anway, if you could take a look at what I have and tell me if I'm doing something wrong (or any other red flags) I'd really appreciate it.

Thanks.

 using UnityEngine;
 using System.Collections;
 
 public class Flick : MonoBehaviour {
 
 //public GameObject attractorPrefab;    
 public GameObject exploderPrefab;
 public GameObject petal;
     
 public float coolDown;
 public float speed;
 public float startTime;
 public float swipeTime;
 public float comfortZone;
 public float minSwipeDistance;
 public float maxSwipeTime;
 public float swipeThresh;        
        float swipeSpeed;
        float inputX;
        float inputY;    
 
 public int count;
 public int tapCount;
     
 public bool couldBeSwipe;
 public bool swipeWasActive;
 public bool gotStartPoint;
 public bool gotEndPoint;    
 public bool inFlight;
     
 public Vector2 startPosition;
 public Vector2 endPosition;
        Vector2 swipeStart;
        Vector2 swipeEnd;
     
 public Vector3 hp;
 public Vector3 hitPointA;
 public Vector3 hitPointB;
 public Vector3 changeCoordinates;
       
        Touch touch;
         
     void Start () 
     {
         speed = 3.0f;             //speed for wind stream
         swipeThresh = 0.5f;     //swipe sensitivity
         
         gotStartPoint = false;
         gotEndPoint = false;
         swipeWasActive = false;
                 
         petal = GameObject.Find("petal");
         
         coolDown = 0;             //input delay counter
         inFlight = false;         //check to see if petal is in wind stream or not
     }
 
     void Update () 
     {
         coolDown -= Time.deltaTime;
         
         if (coolDown < 0)
             coolDown = 0;
         if(((Input.touchCount == 1) && (Input.GetTouch(0).phase == TouchPhase.Moved)) && (coolDown == 0))
         {
             processSwipe();
         }
         else
         if(((Input.touchCount == 1) && (Input.GetTouch(0).phase == TouchPhase.Ended)) && ((coolDown == 0) && (inFlight == false)))
         {
             Debug.Log("Instantiate explosion");
                     
                 
             //touchPosition = new Vector3(touchPosition.x, touchPosition.y, 0);
             Ray ray = camera.ScreenPointToRay(new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, 0));
             //Input.GetTouch(0).position.z = 10;
             RaycastHit hit;
                         
             if(Physics.Raycast(ray, out hit))
             {
                 Vector3 hp = hit.point;
                 hp.z = 0;
                     
                 //Create explosion
                 Instantiate (exploderPrefab, hp, Quaternion.identity);
                 coolDown = 0.5f;
             }
         }else
         if(((Input.touchCount == 1) && (inFlight == true)) && (Input.GetTouch(0).phase == TouchPhase.Began))
         {
             Reset();
         }
         
         if((gotStartPoint == true) && (gotEndPoint == true))
         {
             petal.rigidbody.useGravity = false;
             petal.transform.Translate(changeCoordinates.normalized * (Time.deltaTime * speed), Space.World);
             inFlight = true;
             Debug.Log("touchCount: "+Input.touchCount);
         }
     }
     
     void processSwipe()
     {
         if (Input.touchCount != 1)
         {
             return;
         }
         
         Touch theTouch = Input.touches[0];
         
         //Skip the frame if deltaPosition is zero.
         
         if (theTouch.deltaPosition == Vector2.zero)
         {
             return;
         }
         
         Vector2 speedVec = theTouch.deltaPosition * theTouch.deltaTime;
         float theSpeed = speedVec.magnitude;
         
         bool swipeIsActive;
         swipeIsActive = (theSpeed > swipeThresh);
         
         if (swipeIsActive)
         {
             if(!swipeWasActive)
             {
                 swipeStart = theTouch.position;
                 Ray ray = camera.ScreenPointToRay(swipeStart);
                 RaycastHit hit;
                 
                 if (Physics.Raycast(ray, out hit))
                 {
                     hitPointA = hit.point;
                     hitPointA.z = 0;
                     Debug.Log("Hit Point A: "+hitPointA);
                 }
                 Debug.Log("Swipe start pos: "+swipeStart);
                 
                 gotStartPoint = true;
                 Debug.Log("gotStartPoint =  true!");
             }
         }
         
         else
         {
             if(swipeWasActive)
             {
                 swipeEnd = theTouch.position;
                 Ray ray =  camera.ScreenPointToRay(swipeEnd);
                 RaycastHit hit;
                 
                 if(Physics.Raycast(ray, out hit))
                 {
                     hitPointB = hit.point;
                     hitPointB.z = 0;
                     Debug.Log("Hit Point B: "+hitPointB);
                 }
                 Debug.Log("Swipe end pos: "+swipeEnd);
                 gotEndPoint = true;
                 Debug.Log("gotEndPoint = true!");
                 
                 changeCoordinates = (hitPointB - hitPointA);
                 Debug.Log("Change Coordinates: "+changeCoordinates);
             }
         }
         
         swipeWasActive = swipeIsActive;
     }
     
     void Reset()
     {
         Debug.Log("Reset!");
         
         inFlight = false;
         gotStartPoint = false;
         gotEndPoint = false;
         
         petal.rigidbody.useGravity = true;
         
         coolDown = 0.5f;
     }
 }
         
Comment
Add comment
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

1 Reply

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

Answer by BlacKatFever · Dec 05, 2011 at 03:15 AM

Will close this question as I managed to figure out a passable solution. Needed to construct a system based around bools to determine whether a key was in a "pressed" state and then to check if there was actual input on the device.

Was a pain the butt, but happy with the results.

Comment
Add comment · 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

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

2 People are following this question.

avatar image avatar image

Related Questions

Best way to optimize scene with multiple instances of a destroyable object 1 Answer

Should I increase tris and vertex in favor of reducing alpha overdraw for iOS? 0 Answers

Vsync on or off? 1 Answer

Mystery performance loss on ios 2 Answers

Performance hit when using large font - why? 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