- Home /
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
Best Answer
Answer by naf456 · Sep 22, 2011 at 05:36 PM
Just Found Out that Print Function's are extremely Expensive! Doh! :D
Your answer
![](https://koobas.hobune.stream/wayback/20220612222307im_/https://answers.unity.com/themes/thub/images/avi.jpg)