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 youngapprentice · Apr 16, 2014 at 12:18 AM · iosgravitytilt

Changing Gravity Quickly?

Hi, all!

I'm experimenting with my very first iOS game, and it is going nicely. Currently, the player falls, and the gravity on the X axis is controlled by the tilt of the phone.

However, when the user tilts the phone in the opposite direction of the current gravity, it takes a long while to get the gravity to reverse.

Is there a way that I can get the gravity acting on the cube to change more quickly? (I would love to be able to control how responsive it is) Maybe a way to just reset the rigid body?

Current code:

 public float force;
 
 void FixedUpdate () {
 
         Physics.gravity = new Vector3( Input.acceleration.x*force, -9.81f, 0 );
     
     }

Thanks! - YA

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Apr 16, 2014 at 01:03 AM

While I understand your question in general, figuring out the right fix right fix will require experimenting on your part. I can think of half a dozen different approaches to the problem. Let me suggest three of them.

Try #1: First, try playing with drag and the strength of gravity. How quickly something moves in a new direction is partly how long it takes for existing velocity to decay and partly the strength of the new force. You may also have to place a limit on the velocity. You can do that by putting this line in FixedUpdate():

 rigidbody.velocity = Vector3.ClampMagnitude(rigidbody.velocity, maxVelocity);

Try #2: You can just redirect the velocity so that it is always aligned with gravity. Put this in FixedUpdate():

 rigidbody.velocity =  Physics.gravity.normalized * rigidbody.velocity.magnitude;

You may also have to limit velocity with this solution.

Try #3: You can add some extra force if the object is moving in the wrong direction.

 if (Vector3.Dot(rigidbody.velocity, Physics.gravity) < 0.0) {
     rigidbody.AddForce(Physics.gravity * someFactor, ForceMode.Acceleration)
 }

Bonus suggestion: Similar to #2 but using Vector3.Project(). In FixedUpdate():

 rigidbody.velocity = Vector3.Project(rigidbody.velocity, Physics.gravity);




  
Comment
Add comment · 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

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

20 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 avatar image avatar image avatar image

Related Questions

Accelometer controls for iOS and android 2 Answers

Anyone know where i can find a tutorial for tilt scripts for iOs 1 Answer

Rigidbody.Force code working in editor, not on iPad (Gravity More on iPad?) 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

library not found for -lGoogleUtilities,library not found for -lGoogleUtilitiesf 0 Answers


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