Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Hingle joint rotate using Motor component

Discussion in 'Physics' started by Trild123787898, Nov 12, 2021.

  1. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    I have an object that I rotate using the HingleJoint.Motor component, I rotate when I press the key, and when I release the key it should stop but it slowly goes down, how can I fix it ???
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    We can only see this sentence which isn't clear and cannot see any script or your component set-up so....
    • We presume by "and when I release the key it should stop" you mean you turn off the motor. You didn't say.
    • We presume by "but it slowly goes down" you mean the angular velocity reduces because you've got angular drag set to something non-zero
    • We presume by "it should stop" you mean you want the angular velocity to be zero (no rotation) but you've already set it to zero and it doesn't stop
    So maybe this is just a case of you not actually telling it to stop by setting the angular velocity to zero? Obviously it won't do it by itself.

    If this is wrong then please provide some useful info. A majority of your posts you either don't reply to when someone helps you or you don't provide any useful information, only a short brief question. Please spend more time providing information in the future if you want help and try to include a script if it's a script question.
     
  3. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    Sorry for not providing the necessary information. Here's what I have if I press the key I'm changing. parameter targetvelocity = 5, if the values are 0, then the object along the X axis is slowly descending, most likely it should be so, then how can I fix it?
    https://drive.google.com/file/d/1mVOQXY6U2EPLTElgZW7OjPsT3aLPw9gG/view?usp=sharing

     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Your images don't show. The one that does is just cropped and you can't see the important motor part. Did you check your post?

    You can attach images.

    If you've followed the advice here then try what I said above; turn off the motor and set the angular velocity to zero.
     
  5. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    here is what I got but it didn't solve my problem
    https://drive.google.com/file/d/103YvvXhp48B-azFQTVVlXg9xuejUzx7j/view?usp=sharing

    Code (CSharp):
    1.  public Rigidbody rigidbody_1;
    2.     public HingeJoint hinge;
    3.  
    4.      void Start() {
    5.         rigidbody_1 = GetComponent<Rigidbody>();
    6.         hinge = GetComponent<HingeJoint>();
    7.     }
    8.     void FixedUpdate () {
    9.          var motor = hinge.motor;
    10.        rigidbody_1.angularVelocity = new Vector3(0,0,0);
    11.          motor.force = 100;
    12.         motor.targetVelocity = -20;
    13.         motor.freeSpin = false;
    14.         hinge.motor = motor;
    15.         hinge.useMotor = true;
    16.    
    17.    
    18. }
    19. }
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Why would that solve your problem?

    I said: "turn off the motor and set the angular velocity to zero". That's not what you're doing above. You seem to be doing this constantly per fixed-update.

    I cannot really advise you with snippets like this that are not connected to your original description. :(
     
  7. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    I'm sorry, I don’t know how to do this
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    You don't know how to turn off the motor? You turn the motor on above (useMotor = true) so you don't know how to turn it off? Set it to false!

    You don't know how to set the angular velocity to zero? You do it above " rigidbody_1.angularVelocity = new Vector3(0,0,0);" although this should be shortened to " rigidbody_1.angularVelocity = Vector3.zero;"

    Turn off the motor, set angular velocity to zero.

    Code (CSharp):
    1. void TurnOffMotorStopRotation()
    2. {
    3.     hinge.useMotor = false;
    4.     rigidbody_1.angularVelocity = Vector3.zero;
    5. }
     
  9. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    sorry, it looks like I misunderstood you. I did not change the settings of the joint, and in the script I did as you wrote, but it still slowly descends, can it be possible to brake it by force?
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    That means you didn't do what I said because setting the angular velocity to zero and the motor being off stops it so it means something else is keeping it going such as attached bodies (etc).

    Setup a new project. Add the same hinge on a single Rigidbody. Start it moving by turning on the motor. Turn off the motor and reset the angular velocity. The Rigidbody will stop. If this doesn't work you're misunderstanding something. If it does then you're doing something else on-top in your main project. This is basic debugging. I'd recommend trying this.

    It's almost impossible to help you remotely like this though with text back/forth. You've not provided any other information such as how this is configured, what it looks like, a video of it not working etc. Maybe someone else can help you.
     
  11. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    again sorry for taking your time, I haven't changed anything.
    https://drive.google.com/file/d/103YvvXhp48B-azFQTVVlXg9xuejUzx7j/view?usp=sharing

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FixJoints : MonoBehaviour
    6. {
    7.     public Rigidbody rigidbody_1;
    8.     public GameObject gm;
    9.     public HingeJoint hinge;
    10.     JointMotor jointMotor;
    11.     JointSpring jointSpring;
    12.     private bool test;
    13.  
    14.      void Start() {
    15.         rigidbody_1 = gm.GetComponent<Rigidbody>();
    16.         hinge = gm.GetComponent<HingeJoint>();
    17.         jointMotor = hinge.motor;
    18.         jointSpring = hinge.spring;
    19.        
    20.     }
    21. void Update()
    22. {
    23.      //rigidbody_1.Sleep();
    24.    
    25. }
    26. void TurnOffMotorStopRotation()
    27. {
    28.     hinge.useMotor = false;
    29.     rigidbody_1.angularVelocity = Vector3.zero;
    30.  
    31. }
    32. void FixedUpdate()
    33. {
    34.       TurnOffMotorStopRotation();
    35. }
    36.    
    37.  
    38. }
     
  12. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Your code makes no sense. You are constantly turning off the motor here! This won't even start moving.

    How does the code above relate to "when I release the key it should stop"?

    You seem to be saying one thing then posting code for something else which is why I'm struggling to help you.
     
  13. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    I can't figure out where I'm wrong, I thought this is how it should work
     
  14. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    for now I need it to just stand without touching the spring parameter
     
  15. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Neither can I.
     
  16. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    sorry for taking your time
     
  17. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    No need to be sorry. I am just not sure how to connect the code you're posting with the description you say of what you're doing therefore I don't know how to help.
     
  18. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    ok then just take a look at the code again, this is what I'm trying to do. I thought if you set the values to 0 for targetVelocity, then it will stop, but it doesn’t do it. I also tried to set angularVelocity = Vector3.zero; but the effect is the same
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FixJoints : MonoBehaviour
    6. {
    7.     public Rigidbody rigidbody_1;
    8.     public GameObject gm;
    9.     public HingeJoint hinge;
    10.     JointMotor jointMotor;
    11.     JointSpring jointSpring;
    12.     private bool test;
    13.      private bool test_1;
    14.  
    15.      void Start() {
    16.         rigidbody_1 = gm.GetComponent<Rigidbody>();
    17.         hinge = gm.GetComponent<HingeJoint>();
    18.         jointMotor = hinge.motor;
    19.         //jointSpring = hinge.spring;
    20.        
    21.     }
    22.         void Update()
    23.         {
    24.            if(Input.GetKeyDown(KeyCode.Q))
    25.             {
    26.                
    27.                 gmDown();
    28.             }
    29.             if(Input.GetKeyUp(KeyCode.Q))
    30.             {
    31.                
    32.                 gmUp();
    33.             }
    34.             if(Input.GetKeyDown(KeyCode.E))
    35.             {
    36.              
    37.                 gmDown_1();
    38.             }
    39.             if(Input.GetKeyUp(KeyCode.E))
    40.             {
    41.              
    42.                 gmUp();
    43.             }  
    44.        }
    45.      void gmDown()
    46.      {
    47.         jointMotor.targetVelocity = 5;
    48.         hinge.motor = jointMotor;
    49.      }
    50.      void gmUp()
    51.      {
    52.         jointMotor.targetVelocity = 0;
    53.         hinge.motor = jointMotor;
    54.        // rigidbody_1.angularVelocity = Vector3.zero;
    55.      
    56.      }
    57.      void gmDown_1()
    58.      {
    59.         jointMotor.targetVelocity = -5;
    60.         hinge.motor = jointMotor;
    61.      }
    62. }
    63.  
     
  19. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    So this code is completely different to code you posted before. Are you just changing it or are you not posting the code you're actually using? For instance, this code now doesn't even turn the motor on or off.

    I just don't get it.
     
  20. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    I always have it on, even if I turn it off and set it to velocity at 0, nothing will change, so I don’t know how to solve it, I also moved with the targetPosition spring, but there the force is large, and the joints break when lifting a large mass
     
  21. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Well you're not showing me code that does that, you're just saying you're doing it.

    I honestly cannot help you further.
     
  22. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    well, I have no other code ))
     
  23. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Well apparently you do ...

    From the very beginning (my first post) I expected to you show the code that does what I just quoted that also integrates what I said and you've yet to post that. This is my point. You are saying "you've tried it" but from the confusing posts, I'm not seeing that you're understanding which is why I keep saying I cannot help you further.

    The code you posted above DOESN'T TURN OFF THE MOTOR AND SET THE ANGULAR VELOCITY TO ZERO. This is not shouting, just trying to be super, super, super clear.

    We should've been at this point many posts ago so it's why I feel I cannot help you. There's confusion somewhere along the line here.
     
  24. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    sorry, I can't seem to understand you, but thanks for your time)))