- Home /
Adding force to a rigid body fails on iOS.
Hey guys,
I have a cube that uses the unity-included "Metal" physics material. When my character controller "pushes" it from one of its sides, the character controller script does an AddForce to the cube's rigid body. This allows the character to push the cube, and it works great on PC/Mac/Webplayer. When I compile the same project for iOS however, the player is unable to push the cube.
I know the force is working to an extent, because I can move the cube a few pixels if i keep pushing it for a long time. I also have a similar pushable cube suspended in the air by a rope that I can push on the iOS build with no problems. This leads me to believe that the problem might be related to friction. Does the unity-included "Metal" physics material work on iOS? Is there any other reason anyone can think of for why my pushable cube physics is behaving differently on iOS?
Any help with this is greatly appreciated. Thanks!
EDIT
Here is the character controller's "push" code. The context here is that everything basically occurs on a 2D plane in a sidescroller-like fashion, so the player can only push an object either left or right depending on which direction they are pushing from. Also, the cube has locked rotation so that it's corners do not "catch" on the floor.
function OnControllerColliderHit ( hit : ControllerColliderHit ) {
// push objects
if ( hit.gameObject.CompareTag( 'Pushable' ) ) {
var body : Rigidbody = hit.rigidbody;
if ( transform.position.y > hit.collider.bounds.min.y && transform.position.y < hit.collider.bounds.max.y ) {
var pushDirection : float;
if ( hit.moveDirection.x > 0 ) {
pushDirection = 1;
} else {
pushDirection = -1;
}
body.AddForce( Vector3( pushDirection * 30, 0, 0 ) );
}
}
}
The physic is the same. The problem is probably in how the Input. Like you are computing the force weighted by number of pixels change between frames. It will behave differente from a 320x240 and retina display resolution.
@Sisso This is not the case at all. The force is a constant number in a set direction, either Vector3( 30, 0, 0 ) or Vector3( -30, 0, 0 ) depending on which direction the character is pushing from.
I do however believe that it is not just the physics material, because after removing the physics material all together, I am still able to push the cube on the pc/mac/webplayer builds.
@Sisso I have added the push code to the original post. $$anonymous$$aybe it will provide a clue.
Ok... I don't know... The only thing that I can think is the method OnControllerColliderHit add force each time that is called.
Try to figure out how many times it is called in each platofrm. Add a Debug.Log to print each frame and other right after the AddForce.
Answer by Sisso · Oct 04, 2013 at 08:34 PM
Ok... I don't know... The only thing that I can think is the method OnControllerColliderHit add force each time that is called.
Try to figure out how many times it is called in each platform. Add a Debug.Log to print each frame and other right after the AddForce.
You should execute all physic logic inside the FixedUpdate (http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.FixedUpdate.html). I think that simply moving "body.AddForce( Vector3( pushDirection * 30, 0, 0 ) );" will fix.
i would, but then i would have no way to know whether the collision is happening or not.
Not necessary, you do everything outside and store in variables what must be change during physic. For example:
var moveX = 0.0;
function Update() {
if (Input.IsDown("a")) moveX = 1.0;
}
function OnTriggerEnter() {
moveX = -1.0;
}
function FixedUpdate() {
rigidbody.AddForce(Vector3(moveX, 0, 0);
}
But rember that FixedUpdate is called multiple times a frame, and there is some logic (like distance from waypoint) must be place there.
This will not work. I am colliding my character controller with a cube. How will the character controller know if/which cube it is pushing. The only way to detect collision is with OnControllerColliderHit, which would allow me to store a variable for the cube, but would never know when pushing of the cube has stopped.
If there was a OnControllerCollisionEnter and OnControllerCollisionExit, I'd be set.
Ah, so moving my character controller's $$anonymous$$ove() into fixed update may solve? I'll try it.
I know this is really old, but I was having a very similar problem (addforce working in editor but not in build). $$anonymous$$oving the block of code to fixed update worked for me. Thanks @Sisso
Your answer
Follow this Question
Related Questions
iOS controls 1 Answer
AddForce to Camera 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Unity iPhone: AddForce to Object after Touch 0 Answers
Getcomponent returns null on ios 1 Answer