• 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 virtouso · Feb 05, 2019 at 03:50 PM · physics

how can i restrict rigidbody only in one direction?

hi. im working on roller coaster system. the roller should be able to move only in one direction. and other directions should be locked. how can i do that?

i know i can set velocity and force to a certain direction but when you add rigid body there are lots of forces like gravity and.... but in rail movement only physics can affect in only one dimmension. how can i do that?

Comment
Add comment · Show 1
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 misher · Feb 07, 2019 at 04:52 PM 0
Share

I think your problem is not to set the "right" velocity/force direction but rather to "keep" your roller on track, now your track is basically a spline, a curve in the 3d space. Overall it would be much easier to make your own physics, at least for the rollers, to follow the spline.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by blueshark- · Feb 05, 2019 at 07:04 PM

In the Editor under Rigidbody > Constraints you can force the object to not rotate or move on a certain axis.

This can be done with code too for example:

 using UnityEngine;
 
 public class Example : MonoBehaviour
 {
     Rigidbody rb;
 
     void Start()
     {
         rb = GetComponent<Rigidbody>();
         // Freeze Position on axis Z
         rb.constraints = RigidbodyConstraints.FreezePositionZ;
        // Freeze rotation on axis Z
        rb.constraints = RigidbodyConstraints.FreezeRotationZ;
     }
 }

Comment
Add comment · Show 1 · 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 virtouso · Feb 07, 2019 at 09:50 AM 0
Share

i meant direction not specific world axis

avatar image
0

Answer by xxmariofer · Feb 07, 2019 at 09:58 AM

you can force it in the fixedupdate,

 void FixedUpdate()
 {
      rb.velocity = transform.Forward * 10; // will only add force in the forward direction, change 10 to your desired velocity.
 }
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 virtouso · Feb 07, 2019 at 01:54 PM 0
Share

yes. right now im using my own physics rules to affect velocity but i think its much simpler to use unity physics engine. as we know in the space many thing like gravity and engine and other objects force makes object move. foe a roller coaster maybe i could use one direction force as other direction are locked by rails and force onle in direction of rails are accepted

avatar image xxmariofer virtouso · Feb 07, 2019 at 02:28 PM 0
Share

oh i think i understood you, then you only need to multiply the current velocity * transform.forward vector, your object will continue having the acceleration of physics drag etc... but with your desired direction (forward in this case)

avatar image
0

Answer by GuirieSanchez · Nov 07, 2021 at 08:30 AM

This is excellent. I'm going to give it a shot in bolt unity

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

172 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

Related Questions

2D 360 degress platformer example needed 0 Answers

Jump doesn't work properly 1 Answer

ways to get Collision2D 2 Answers

RaycastAll failing inconsistently 1 Answer

Every time my character hits or touches the walls he gets stuck or acts as if the object the character hit was a magnet. 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