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 SparkZWolf · Jan 28, 2014 at 08:42 PM · physicsrigidbodyscriptingbasicsrigidbodies

Why is my object tilting when moving?

Hello. I am using addRelativeForce to control a rectangle collider. However, it tilts whenever it is supposed to only move forward. How do I prevent this?

 var forwardMoveAmount = -30; //Sets forward  speed.
 var turnForce = 2; //Sets turning speed.
 var canDrive = 0;//Sets up ground detection (For now, works with all objects)
 
 function Start () 
 { 
     rigidbody.centerOfMass = Vector3 (0, 0, 0);//Keeps object upright. 
 }
 
 function OnCollisionEnter (col : Collision)
 {
     canDrive = 1;//Gives player control entering on ground.
     if (col.gameObject.name == "GoingUp")//Check if on a jump pad.
     {
         rigidbody.velocity = (Vector3.up * 30);//Makes player fly upwards. 
     }
 }
 function OnCollisionStay (col : Collision)//Gives player control on ground.
 {
     canDrive = 1;
 }
 
 
 function OnCollisionExit (col : Collision)//Removes player control off ground
 {
     canDrive = 0;
 }
 
 function Update () {
 
     var forwardMoveAmount = Input.GetAxis("Vertical") * forwardMoveAmount * canDrive;//Get input, multiplies it by movement amount and if the player can drive. 
     var turnForce = Input.GetAxis("Horizontal") * turnForce;//Get input and multiplies it by turnFroce
     transform.Rotate (0,turnForce * canDrive,0);//Rotates if canDrive is not 0
     rigidbody.AddRelativeForce (Vector3.right * forwardMoveAmount);//Applies relative force to object to move it
 }
 

Here is my whole player code in case it helps. Apologies if its a bit unprofessional, I'm quite new to unity.

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

2 Replies

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

Answer by SparkZWolf · Jun 16, 2014 at 01:09 PM

The friction of the ground was rubbing on the collider. Sorry for necroposting, but someone helped me over on reddit. Thanks though!

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

Answer by nesis · Jan 28, 2014 at 10:03 PM

You can prevent the rigidbody from rotating by setting its Freeze Rotation parameter in the Inspector tab.

Comment
Add comment · Show 2 · 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 SparkZWolf · Jan 29, 2014 at 12:56 AM 0
Share

Thank you, but the object need to be able to rotate when I want it to. A freeze would prevent it from turning entirely. I just want to apply a force so that it moves only in one direction without turning.

avatar image nesis · Jan 29, 2014 at 02:16 PM 0
Share

You can store your rigidbody's angularVelocity in a Vector3, add the force, then set angularVelocity to the value you stored, if that approach works as you want.

I still suspect that locking the rotation of your rigidbody on one axis is what you're wanting...?

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

19 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

Related Questions

Spaceship physics - adding resistance 2 Answers

Set velocity of kinematic rigidbody appropriately 1 Answer

how to create the magnet similar to the real magnet ? 1 Answer

Problem with Rigidbody Parent/Child Relationship with "Teeter Totter" 0 Answers

When I try to save the velocity of a rigidbody and use it later it doesn't work? 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