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 infinitypbr · Dec 30, 2012 at 08:51 AM · rigidbodyiosgravity

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
Comment
Add comment · Show 3
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 infinitypbr · Dec 30, 2012 at 07:45 PM 0
Share

$$anonymous$$aybe the daytime people have a thought?

avatar image infinitypbr · Dec 30, 2012 at 08:14 PM 0
Share

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)

avatar image infinitypbr · Dec 30, 2012 at 09:09 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

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

8 People are following this question.

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

Related Questions

how to keep a "ship" over a platform or rigidbody? 0 Answers

Very simple water bouyancy script not working? 1 Answer

How can i make my object like bouncy on finger move? 1 Answer

Gravity is not working no matter what 0 Answers

How would I change a value in a 2D Rigidbody 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