- Home /
Why are the physics different between the editor and device (iOS)?
I have noticed a bit of a difference between the physics in the editor and the physics in the iOS device. Here is the code I am using to apply a force to a ball so that it moves in the direction of the touch:
void FixedUpdate()
{
if (Input.touchCount > 0)
{
OnTap(Input.touches[Input.touchCount - 1].position);
}
if (Input.GetButton("Fire1") == true)
{
OnTap(Input.mousePosition);
}
}
void OnTap(Vector2 pt)
{
Ray ray = Camera.main.ScreenPointToRay(pt);
RaycastHit hit;
if (Physics.Raycast(ray, out hit) )
{
Vector3 kForce = KickForce(hit.point); // calculate the force and kick!
rigidbody.AddForce(kForce * forceMultiplier);//, ForceMode.Acceleration);
}
}
Vector3 KickForce(Vector3 dest)
{
Vector3 dir = dest - transform.position; // get target direction
dir.y = 0; // Zero-ize the vertical aspect
return dir.normalized * rigidbody.mass;
}
The ball moves much faster on the device as if FixedUpdate() is being called more often than in the editor. Does anyone know why this is happening?
me : Possibly due to the time-step. iOS runs at 60 Hz, so you need to change the time-step in the editor.
In Unity, click Edit > Project Settings > Time
Now in the Inspector window, change 'Fixed Timestep' to 0.0166667
The editor should now run at the same framerate as the iOS device.
zmeinaz : I gave it a shot and it appears to exhibit the same behavior. Doesn't Fixed Timestep apply to both the device and the editor?
me : not sure then, shall convert my answer to a comment =]
As left mouse down. The idea is to apply the force while the left mouse button is held down (for editor) or while the player is touching the screen (device).
Answer by whydoidoit · Jul 04, 2012 at 06:25 AM
So your problem is, on the device, the code is running once for the touch and the touch is also triggering your Fire1 iteration. I suggest an else
in front of your Fire1 test...
well spotted (have to admit I didn't read that thoroughly). For editor controls and touch controls I usually use a configuration like this :
// check if in Unity Editor or on Device
if ( Application.isEditor || Application.isWebPlayer )
{
// $$anonymous$$ouse Inputs
if ( Input.Get$$anonymous$$ouseButtonUp(0) )
{
// do stuff
}
}
else
{
// TOUCH Inputs
var count : int = Input.touchCount;
// do stuff
}
(if i have to edit another answer 'cos the script formatting is now stuffed thanks to UDN , I shall lose it .... )
EDIT : aaaarrrggghh !
Hey it's totally screwed up my forum access too. If I Google something and it is a forum message - clicking on it just loses the link and takes me to the forum home :(
That was it! Thank you, I didn't realize that 'Fire1' is also triggered from a touch.
Your answer
Follow this Question
Related Questions
Difference Unity3 and Unity3+iOS 2 Answers
Objects that appear in the editor's game view do not appear in the game build 0 Answers
60FPS constant, but serious visual stutter - only on iOS 1 Answer
How to assign a joint's 'connected body'? 1 Answer
Why does Unity 2019.1 constantly (re)import things? 0 Answers