Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by etikaram · Aug 20, 2019 at 09:36 AM · physicsunity5runner

How can i stop cube from rolling

Hi guys, i got a runner game and cube is my player, the problem is i can't stop cube from rolling. The ground is slippery(friction = 0) but it is still rolling. When i freeze rotation of y axis it seems like lagging so it doesn't work either. Please help me. There is my movement code: public Rigidbody rb; public float forwardForce = 2000f; public float sidewaysForce = 500f; public float acceleration; public PlayerMovement movement;

     void FixedUpdate()
     {
         rb.AddForce(0, 0, forwardForce * Time.deltaTime);
         forwardForce += Time.deltaTime * acceleration;
 
         if (Input.GetKey("d"))
         {
             rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
         }
         if (Input.GetKey("a"))
         {
             rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
         }
Comment
Add comment · Show 2
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 Eno-Khaon · Aug 20, 2019 at 10:07 PM 0
Share

To note, Rigidbody.AddForce() already applies a continuous force by default (using Force$$anonymous$$ode.Force).

With this in $$anonymous$$d, you can reduce all of your forces by 50x (using default physics update rate of 50-per-second) and remove any redundant use of Time.deltaTime.

Furthermore, if you switch to using a mass-independent Force$$anonymous$$ode (since this is for a player character's movement), you won't have to make any accommodations in case you change your player's mass at any point.

 public float forwardForce = 40f; // (2000 / 50)
 public float sidewaysForce = 10f; // (500 / 50)
 // ...
 rb.AddForce(0, 0, forwardForce, Force$$anonymous$$ode.Acceleration);
 // ... 
 rb.AddForce(sidewaysForce, 0, 0, Force$$anonymous$$ode.VelocityChange);
avatar image Owen-Reynolds · Aug 21, 2019 at 04:31 PM 0
Share

Check the physics material on the cube and also the FrictionCombine setting on the floor. It sounds there is a lot of friction, so when you prevent rolling, the friction grabs it.

Also, it seems like freezing x and z prevent rolling. y is just a spin -- like if one corner rams a doorframe, the cube can spin a little to try to fit through.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by IrshadKhan_ · Aug 23, 2019 at 05:26 AM

hey @etikaram try this Code

 public Rigidbody rb;
 public float forwardForce = 2000;
 public float acceleration = 250;
 public float sidewaysForce = 500;

 void FixedUpdate()
 {
   

     if (Input.GetKey("d"))
     {
         rb.AddForce(0, 0, forwardForce * Time.deltaTime);
         forwardForce += Time.deltaTime * acceleration;
         rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
     }
     if (Input.GetKey("a"))
     {
         rb.AddForce(0, 0, forwardForce * Time.deltaTime);
         forwardForce += Time.deltaTime * acceleration;
         rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
     }
 }

Or

 public Rigidbody rb;
 public float forwardForce = 2000;
 public float acceleration = 250;
 public float sidewaysForce = 500;

 void FixedUpdate()
 {
   

     if (Input.GetKeyDown("d"))
     {
         rb.AddForce(0, 0, forwardForce * Time.deltaTime);
         forwardForce += Time.deltaTime * acceleration;
         rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
     }
     if (Input.GetKeyDown("a"))
     {
         rb.AddForce(0, 0, forwardForce * Time.deltaTime);
         forwardForce += Time.deltaTime * acceleration;
         rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
     }
 }
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 GhostlyFedoras · Aug 20, 2019 at 07:38 PM

Hey, Assuming you do not want it to rotate what so ever You can always use,

 transform.rotation = Quaternion.identity;

this just means the cube will have no rotation Just add this into your Update/FixedUpdate and your character wont roll in any direction, Hope this helps. you can see more on Quaternions etc here if this doesn't work or isn't what you need, https://docs.unity3d.com/ScriptReference/Quaternion.html

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

197 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 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 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 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 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 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 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 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 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 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

CharacterController falling through walls and ceiling 1 Answer

Throw Ball Forwards 1 Answer

How do you move a second character controller object on top of a first one when jumping? 0 Answers

How to change the gravity of my scene? 2 Answers

Is it possible to make Unity portable? 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