Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 dqhendricks · Oct 04, 2013 at 06:25 PM · iosaddforcephysics material

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 ) );
         }
     }
 }
Comment
Add comment · Show 6
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 Sisso · Oct 04, 2013 at 07:01 PM 0
Share

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.

avatar image dqhendricks · Oct 04, 2013 at 07:23 PM 0
Share

@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.

avatar image dqhendricks · Oct 04, 2013 at 07:27 PM 0
Share

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.

avatar image dqhendricks · Oct 04, 2013 at 07:31 PM 0
Share

@Sisso I have added the push code to the original post. $$anonymous$$aybe it will provide a clue.

avatar image Sisso · Oct 04, 2013 at 07:42 PM 0
Share

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.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 8 · 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 dqhendricks · Oct 04, 2013 at 09:00 PM 0
Share

i would, but then i would have no way to know whether the collision is happening or not.

avatar image Sisso · Oct 04, 2013 at 09:07 PM 0
Share

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.

avatar image dqhendricks · Oct 04, 2013 at 09:10 PM 0
Share

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.

avatar image dqhendricks · Oct 04, 2013 at 09:18 PM 0
Share

If there was a OnControllerCollisionEnter and OnControllerCollisionExit, I'd be set.

avatar image dqhendricks · Oct 04, 2013 at 09:34 PM 1
Share

Ah, so moving my character controller's $$anonymous$$ove() into fixed update may solve? I'll try it.

avatar image musickgm dqhendricks · Feb 26, 2018 at 11:11 PM 0
Share

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

Show more comments

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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