Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Adam_is_learning_all_the_time · Jul 20, 2018 at 03:20 AM · jointshingejointhinge jointmotor

HingeJoint Motor's values not being changed by script (show in debug, but not inspector)

Hi everyone,

I am working on a script where the player's triggers turn a motorized hinge in two directions, and everything is working smoothly if I manually edit TargetVelocity in the inspector window. However, if I edit the values using c# the changes are not being reflected in the game, but still show in the console as intended.

Here's a picture of the inspector, showing my most of my hinge settings, and what I'm seeing in the editor: alt text

Here is my script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class caneTorque : MonoBehaviour
 {
     //Input Setup
     public float leftTrigger;
     public float rightTrigger;
     public float stickSensitivity;
     public float leftRightBalance;
     //Physics
     public Rigidbody rb;
     public HingeJoint caneHinge;
     public JointMotor caneHingeMotor;
 
     void Start()
     {
         rb = GetComponent<Rigidbody>();
         caneHinge = GetComponent<HingeJoint>();
         caneHingeMotor = caneHinge.motor;
     }
 
     void Update()
     {
         //Getting controller input and getting desired direction
         leftTrigger = Input.GetAxis("LTrigger");
         rightTrigger = Input.GetAxis("RTrigger");
         leftRightBalance = ((-leftTrigger) + rightTrigger) * stickSensitivity;
     }
     void FixedUpdate()
     {
         //Setting target velocity
         caneHingeMotor.targetVelocity = -leftRightBalance;
         //Attempt to refresh the physics suggested by @kinggryan 
         rb.AddForce(Vector3.zero);
         //In console this will work as expected, but inspector & game does not update
         print(caneHingeMotor.targetVelocity);
     }
 }

I found This question where @kinggryan suggested applying force to the rigidbody in order to force Unity to go to work on applying the new values. Unfortunately in my case, it does not seem to have worked, so I thought it best to admit defeat and ask for any insight or ideas on how to solve this puzzle. I'm on Unity 2017.3.1f1 in case it's any help.

Thank you so much for your time,

Adam

screenshot-65.png (319.8 kB)
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
1
Best Answer

Answer by JVene · Jul 20, 2018 at 03:40 AM

In order to change the values, you actually have to assign the motor member like this:

  void FixedUpdate()
  {
      //Setting target velocity
      caneHingeMotor.targetVelocity = -leftRightBalance;

      caneHinge.motor = caneHingeMotor;

  }

The motor property doesn't "gather" the values you change until the motor property member is assigned.

Also, make sure you assign a force to the motor (caneHingeMotor.force), or it still won't move.

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 Adam_is_learning_all_the_time · Jul 20, 2018 at 04:26 AM 1
Share

Hi @JVene ,

You were absolutely right, I wish I'd seen your incredibly speedy reply before diving into the documentation!

I copy/pasted the HingeJoint example and then worked backwards taking out all the lines, and wound up with the same extra lines as your answer.

It's interesting that the motor needs assigning in each fixedUpdate rather than once in start(), it's a curious one. You were bang on with assigning a force to the motor in fixedUpdate as well, as I found that taking out the code for it always reverts it back to zero, regardless of the inspector? :)

Thank you so much for explaining this to me, I didn't understand the reasons behind the issue I was having, which beats flailing around with trial and error as I was doing. I'm really grateful for your help, thanks again. :)


In case it helps anyone who stumbles on this, here's the code with your corrections

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class caneTorque : $$anonymous$$onoBehaviour
 {
     //Input Setup
     public float leftTrigger;
     public float rightTrigger;
     public float stickSensitivity;
     public float leftRightBalance;
     //Physics
     public Rigidbody rb;
     public HingeJoint caneHinge;
     public Joint$$anonymous$$otor caneHinge$$anonymous$$otor;
 
     void Start()
     {
         HingeJoint caneHinge = GetComponent<HingeJoint>();
         Joint$$anonymous$$otor caneHinge$$anonymous$$otor = caneHinge.motor;
     }
 
     void Update()
     {
         //Getting controller input and getting desired direction
         leftTrigger = Input.GetAxis("LTrigger");
         rightTrigger = Input.GetAxis("RTrigger");
         leftRightBalance = ((-leftTrigger) + rightTrigger) * stickSensitivity;
     }
     void FixedUpdate()
     {
         //if this isn't set here, force reverts to zero, ignoring the inspector
         caneHinge$$anonymous$$otor.force = 100;
         caneHinge$$anonymous$$otor.targetVelocity = leftRightBalance;
         //if this isn't set here it see$$anonymous$$gly forgets what the motor is part of
         caneHinge.motor = caneHinge$$anonymous$$otor;
     }
 }

avatar image JVene Adam_is_learning_all_the_time · Jul 20, 2018 at 11:34 AM 0
Share

Cheers!

Under the hood, motor is implemented as a property, so assigning it fires off functions which configure the motor for operation. Changing values in the motor itself doesn't do that, which I admit seems a bit odd.

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

88 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

Related Questions

How to make an arm with multiple, controllable joints? 2 Answers

Hinge joint spring variable (c#) 1 Answer

How to effectively control hinge joint motors (scene file included) ? 4 Answers

accelerate and brake using Math.lerp ? 0 Answers

unity3d hinge motor 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