- Home /
Rigidbody.Force code working in editor, not on iPad (Gravity More on iPad?)
The following code is for inputs -- the editor gets a mouse click, instantiates a joystick, and follows the position around to move a character. Character goes left/right and "force" up (like a jet pack). Up = 1200 force and down (all the way) = 600 force. The character receives the x/y movement and the "force" from the controller script, so the result should be the same if the inputs are the same.
In the editor, everything works as expected.
In the iPad, left/right work just fine, but the character fails to rise in the air. I am printing the force value, so I know it's receiving it properly in both cases. I've removed all "touch" references in the scripts otherwise, though, just to make sure I'm not somehow causing conflicting commands.
Any idea why it won't work on the iPad but does in Editor? (I was thinking maybe gravity is acting different, but that doesn't make sense.)
[EDIT: I've when I add the following code to the character in Update(), basically constantly adding force up, it seems to work fine. Taking the code out breaks it again. However on the editor, this code obviously makes the character just float up non-stop.
rigidbody.AddForce (0, 900, 0);
Could Gravity be different on the different device?]
#if UNITY_EDITOR
if ( Input.GetMouseButtonDown(0))
{
if (Input.mousePosition.x < (screenWidth / 2.4))
{
print ("Touching Left Side: " + Input.mousePosition.x);
if (!controllerUp)
{
controllerUp = true;
var screenPose = Input.mousePosition;
screenPose.z = 20;
var worldPose = mainCamera.ScreenToWorldPoint(screenPose);
JoystickObject = Instantiate(JoystickController, worldPose, Quaternion.identity);
var JoystickTransforme = JoystickObject.transform;
JoystickTransforme.parent = transform;
joystickCenterX = JoystickObject.transform.position.x;
joystickCenterY = JoystickObject.transform.position.y;
}
}
}
if ( Input.GetMouseButton(0))
{
var screenPos2e = Input.mousePosition;
screenPos2e.z = 20;
var worldPos2e = mainCamera.ScreenToWorldPoint(screenPos2e);
joystickCenterX = JoystickObject.transform.position.x;
joystickCenterY = JoystickObject.transform.position.y;
currentTouchX = worldPos2e.x;
currentTouchY = worldPos2e.y;
moveX = currentTouchX - joystickCenterX;
moveY = currentTouchY - joystickCenterY;
if (moveX > 100)
{
moveX = 100;
}
if (moveX < -100)
{
moveX = -100;
}
if (moveY > 100)
{
moveY = 100;
}
if (moveY < -100)
{
moveY = -100;
}
JoystickButton = GameObject.Find("Joystick Button");
JoystickButton.transform.position.x = (joystickCenterX + moveX);
JoystickButton.transform.position.y = (joystickCenterY + moveY);
}
if ( Input.GetMouseButtonUp(0))
{
if (controllerUp)
{
controllerUp = false;
Destroy (JoystickObject);
moveX = 0.00;
moveY = 0.00;
}
}
#endif
#if UNITY_IPHONE
var hit2 : RaycastHit;
for (var i = 0; i < Input.touchCount; ++i)
{
if (Input.GetTouch(i).position.x < (screenWidth / 2.4))
{
//print ("Touching Left Side: " + Input.GetTouch(i).position.x);
if (Input.GetTouch(i).phase == TouchPhase.Began)
{
if (!controllerUp)
{
controllerUp = true;
var screenPos = Input.GetTouch(i).position;
var screenPosZ = 20;
var worldPos = mainCamera.ScreenToWorldPoint(screenPos);
JoystickObject = Instantiate(JoystickController, Vector3(screenPos.x, screenPos.y, screenPosZ), Quaternion.identity);
var JoystickTransform = JoystickObject.transform;
JoystickTransform.parent = transform;
joystickCenterX = JoystickObject.transform.position.x;
joystickCenterY = JoystickObject.transform.position.y;
}
}
else if (Input.GetTouch(i).phase == TouchPhase.Ended || Input.GetTouch(i).phase == TouchPhase.Canceled)
{
if (controllerUp)
{
controllerUp = false;
Destroy (JoystickObject);
moveX = 0.00;
moveY = 0.00;
}
}
else
{
var screenPos2 = Input.GetTouch(i).position;
var worldPos2 = mainCamera.ScreenToWorldPoint(screenPos2);
joystickCenterX = JoystickObject.transform.position.x;
joystickCenterY = JoystickObject.transform.position.y;
currentTouchX = worldPos2.x;
currentTouchY = worldPos2.y;
moveX = currentTouchX - joystickCenterX;
moveY = currentTouchY - joystickCenterY;
if (moveX > 100)
{
moveX = 100;
}
if (moveX < -100)
{
moveX = -100;
}
if (moveY > 100)
{
moveY = 100;
}
if (moveY < -100)
{
moveY = -100;
}
JoystickButton = GameObject.Find("Joystick Button");
JoystickButton.transform.position.x = (joystickCenterX + moveX);
JoystickButton.transform.position.y = (joystickCenterY + moveY);
//print ("X: " + moveX);
//print ("Y: " + moveY);
}
}
else
{
/*
// RIGHT SIDE TOUCH
var ray2 = mainCamera.ScreenPointToRay (Input.GetTouch(i).position);
if (Physics.Raycast (ray2,hit2))
{
if (hit2.collider.gameObject.name == "Fire Button")
{
firing = true;
}
}
else
{
firing = false;
}*/
}
}
#endif
I've confirmed with print ("Velocity: " + rigidbody.velocity); that without the extra force added, the character seems to TRY to go up, but is being held down. (Velocity goes into -1 etc, although I'm pushing force up)
I've solved this specific problem by re-writing the code to turn gravity off when the user is clicking the joystick-area. Gravity turns back on when they bring their thumb up, back off (with force) when they bring it back on. -force when holding down moves character down, but not nearly as much as with gravity.
However, the over all problem still exists.
Your answer