Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 naf456 · Sep 22, 2011 at 05:34 PM · c#javascriptspeedslowsame

Why is C# joystick taking 300 times slower to execute then javascript Joystick?

hello. I've create a new script in c#. it's the converted joystick script from mobile standard assets, with the joystick functionality take out, leaving the touchPad functionality.

I was probing my game with the profiler, and I have noticed, my new smaller c# joystick script is taking up 25 more frames then the regular javascript joystick.

it's all the same, just miniumised, take a look:

     using UnityEngine;
 using System.Collections;
 
 public class TouchPad : MonoBehaviour {
 
 static private TouchPad[] touchPads;                    // A static collection of all touchpad
 static private bool enumeratedTouchPad = false;
 static private float tapTimeDelta = 0.3f;                // Time allowed between taps
 
 public Rect touchZone;
 public Vector2 deadZone = Vector2.zero;                        // Control when position is output
 public bool normalize = false;                             // Normalize output after the dead-zone?
 public Vector2 position;                                     // [-1, 1] in x,y
 public int tapCount;                                            // Current tap count
 
 private int lastFingerId = -1;                                // Finger last used for this joystick
 private float tapTimeWindow;                            // How much time there is left for a tap to occur
 private Vector2 fingerDownPos;
 private float fingerDownTime ;
 private float firstDeltaTime = 0.5f;
 private Touch touch;
 
 void Start()
 {
     touchZone = RectRelative.Convert(touchZone);
 }
 
 
 void Disable()
 {
     gameObject.active = false;
     enumeratedTouchPad = false;
 }
 
 void ResetTouchPad()
 {
     // Release the finger control and set the joystick back to the default position
     lastFingerId = -1;
     position = Vector2.zero;
     fingerDownPos = Vector2.zero;    
 }
 
 public bool IsFingerDown()
 {
     return (lastFingerId != -1);
 }
     
 public void LatchedFinger(int fingerId)
 {
     // If another joystick has latched this finger, then we must release it
     if ( lastFingerId == fingerId )
         ResetTouchPad();
 }
 
 void Update()
 {    
     if ( !enumeratedTouchPad )
     {
         // Collect all joysticks in the game, so we can relay finger latching messages
         touchPads = FindObjectsOfType(typeof(TouchPad)) as TouchPad[];
         enumeratedTouchPad = true;
     }    
         
     int count = Input.touchCount;
     
     // Adjust the tap time window while it still available
     if ( tapTimeWindow > 0 )
         tapTimeWindow -= Time.deltaTime;
     else
         tapCount = 0;
     
     if ( count == 0 )
         ResetTouchPad();
     else
     {
         for(int i = 0;i < count; i++)
         {
             touch = Input.GetTouch(i);            
             bool shouldLatchFinger = false;
                 
             if ( touchZone.Contains( touch.position ) )
                     shouldLatchFinger = true;
                     
     
             // Latch the finger if this is a new touch
             if ( shouldLatchFinger && ( lastFingerId == -1 || lastFingerId != touch.fingerId ) )
             {                
                     lastFingerId = touch.fingerId;
                     fingerDownPos = touch.position;
                     fingerDownTime = Time.time;
             }
                 
                 lastFingerId = touch.fingerId;
                 
                 // Accumulate taps if it is within the time window
                 if ( tapTimeWindow > 0 )
                     tapCount++;
                 else
                 {
                     tapCount = 1;
                     tapTimeWindow = tapTimeDelta;
                 }
                                             
                 // Tell other joysticks we've latched this finger
                 foreach ( TouchPad t in touchPads )
                 {
                     if ( t != this )
                         t.LatchedFinger( touch.fingerId );
                 }                        
             }                
     
             if ( lastFingerId == touch.fingerId )
             {    
                 // Override the tap count with what the iPhone SDK reports if it is greater
                 // This is a workaround, since the iPhone SDK does not currently track taps
                 // for multiple touches
                 if ( touch.tapCount > tapCount )
                     tapCount = touch.tapCount;
                 
                 //let's just set the position directly based on distance from initial touchdown
                 position.x = Mathf.Clamp( ( touch.position.x - fingerDownPos.x ) / ( touchZone.width / 2 ), -1, 1 );
                 position.y = Mathf.Clamp( ( touch.position.y - fingerDownPos.y ) / ( touchZone.height / 2 ), -1, 1 );
                 
                 //The the Touch is gone, Reset the Touchpads
                 if ( touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled )
                     ResetTouchPad();                    
             }            
         }
     
     
     // Adjust for dead zone    
     var absoluteX = Mathf.Abs( position.x );
     var absoluteY = Mathf.Abs( position.y );
     
     if ( absoluteX < deadZone.x )
     {
         // Report the touchpad as being at the center if it is within the dead zone
         position.x = 0;
     }
     else if ( normalize )
     {
         // Rescale the output after taking the dead zone into account
         position.x = Mathf.Sign( position.x ) * ( absoluteX - deadZone.x ) / ( 1 - deadZone.x );
     }
         
     if ( absoluteY < deadZone.y )
     {
         // Report the touchpad as being at the center if it is within the dead zone
         position.y = 0;
     }
     else if ( normalize )
     {
         // Rescale the output after taking the dead zone into account
         position.y = Mathf.Sign( position.y ) * ( absoluteY - deadZone.y ) / ( 1 - deadZone.y );
     }
     print(position);
 }
 }

If you don't believe me look : http://postimage.org/image/h25jh6zo/

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
1
Best Answer

Answer by naf456 · Sep 22, 2011 at 05:36 PM

Just Found Out that Print Function's are extremely Expensive! Doh! :D

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 Bunny83 · Sep 22, 2011 at 09:29 PM 0
Share

Exactly! :D

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to set speed limit of car? 3 Answers

Multiple Cars not working 1 Answer

how to read a txt file faster ? my projects' loading time is so long ! about 30 minutes!!! 2 Answers

Distribute terrain in zones 3 Answers

Smoothed Rotation with 2D Top-Down View 1 Answer


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