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 Dungeon · Jul 20, 2014 at 03:20 PM · freezepositiondragrigidbody

A Problem with freeze rotation and position.

Hello. I am writing a script that freezes the position of objects and allows them to travel on a single axis when given the inputs of the assigned keys X, Y, and Z. This script starts with the variables x, y, and z all deactivated. This leads to my first problem. On start, my object is not constrained in two of the axis. Also, I intended to keep all axis of rotation constrained but none of them are constrained.

Could it be that I am using a modified version of the dragrigidbody script to move the object? I changed lines 20 and 63 on the dragrigidbody script from:

 mainCamera.ScreenPointToRay (Input.mousePosition) 

to:

 mainCamera.ViewportPointToRay(new Vector3(0.5, 0.5, 0))

Here is my drag object script:

 #pragma strict
 var x : boolean = false;
 var y : boolean = false;
 var z : boolean = false;
 function Start () {
     x = false;
     y = false; 
     z = false;
     
 }
 
 function Update () {
     
     rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
     //If it is false
     if(x == false){
         rigidbody.constraints = RigidbodyConstraints.FreezePositionX;
     }
     
     if(y == false){
         rigidbody.constraints = RigidbodyConstraints.FreezePositionY;
     }
     
     if(z == false){
         rigidbody.constraints = RigidbodyConstraints.FreezePositionZ;
     }
     
     //If it is true
     if(x == true){
         rigidbody.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ;
     }
     
     if(y == true){
         rigidbody.constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionZ;
     }
     
     if(z == true){
         rigidbody.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionX;
     }
     
     if(Input.GetKeyDown(KeyCode.X)){
         x = true;
         y = false;
         z = false;
     }
     if(Input.GetKeyDown(KeyCode.Y)){
         x = false;
         y = true;
         z = false;
     }
     if(Input.GetKeyDown(KeyCode.Z)){
         y = false;
         x = false;
         z = true;
     }
 }


Are there any bugs that I can fix? For the rotation constraints I already tried "rigidbody.constraints = RigidbodyConstraints.FreezeRotation;" with no luck.

Here is my updated script with the rotation problem fixed:

 #pragma strict
 var x : boolean = false;
 var y : boolean = false;
 var z : boolean = false;
 function Start () {
 
     rigidbody.constraints = RigidbodyConstraints.FreezePosition;
 }
 
 function Update () {
     
     rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
         if(Input.GetKeyDown(KeyCode.X)){
         x = true;
         y = false;
         z = false;
     }
     if(Input.GetKeyDown(KeyCode.Y)){
         x = false;
         y = true;
         z = false;
     }
     if(Input.GetKeyDown(KeyCode.Z)){
         y = false;
         x = false;
         z = true;
     }
     //If it is false
     if(x == false){
         rigidbody.constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
     }
     
     if(y == false){
         rigidbody.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
     }
     
     if(z == false){
         rigidbody.constraints = RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
     }
     
     //If it is true
     if(x == true){
         rigidbody.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
     }
     
     if(y == true){
         rigidbody.constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
     }
     
     if(z == true){
         rigidbody.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
     }
 
 }

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 DarkGamer67 · Jul 21, 2014 at 05:31 PM

I am not sure. I think need to set the RigidbodyConstrains to none because the Rotatons stay freezed if they won't get reset. You need to reset them because you freeze all Layers every Frame and you don't need to do something if it's false. Your script should look like this :

     #pragma strict
     var x : boolean = false;
     var y : boolean = false;
     var z : boolean = false;
     function Start () {
     x = false;
     y = false;
     z = false;
      
     }
      
     function Update () {
      
     rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
 
      
     //If it is true
     if(x == true){
 //First reset the feezed layers
     rigidbody.constraints = RigidbodyConstraints.None;
 //Than freeze the Layers
     rigidbody.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ;
     }
      
     if(y == true){
 //First reset the feezed layers
     rigidbody.constraints = RigidbodyConstraints.None;
 //Than freeze the Layers
     rigidbody.constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionZ;
     }
      
     if(z == true){
 //First reset the feezed layers
     rigidbody.constraints = RigidbodyConstraints.None;
 //Than freeze the Layers
     rigidbody.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionX;
     }
      
     if(Input.GetKeyDown(KeyCode.X)){
     x = true;
     y = false;
     z = false;
     }
     if(Input.GetKeyDown(KeyCode.Y)){
     x = false;
     y = true;
     z = false;
     }
     if(Input.GetKeyDown(KeyCode.Z)){
     y = false;
     x = false;
     z = true;
     }
     }


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 Dungeon · Jul 21, 2014 at 07:58 PM 0
Share

Thank you DarkGamer67! I have actually fixed the rotation problem (I needed to put "freeze rotation" everywhere and now the script is a little messy). The only problem that remains is the fact that the object starts constrained in position in only one axis and not all three. Thank you for your version of the script! When I play your version, the object starts out movable in all axis and when I press X, Y, or Z, it is not constrained in the rotation.

Thank you again and I will post the updated script!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

DragRigidbody 1 Answer

Spin non kinematic object ? 1 Answer

Freezing Y position when using rootmotion 1 Answer

Dragging object inside TriggerZone 0 Answers

Freeze x,y position according to object's axis 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