- Home /
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:
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
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.
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;
}
}
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.