• 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 w$$anonymous$$le 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

W$$anonymous$$le I understand your question in general, figuring out the right fix right fix will require experimenting on your part. I can t$$anonymous$$nk 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 somet$$anonymous$$ng 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 t$$anonymous$$s 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 t$$anonymous$$s in FixedUpdate():

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

You may also have to limit velocity with t$$anonymous$$s 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

iOS Tilt input and constant force 0 Answers

Bike wobbles like pendulum after applying gravity 1 Answer

iPad tilt gives weird behaviour 0 Answers

Adding gravity and texture to newly added game objects 1 Answer

How to create tilt control similar to temple run? 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