Frustrated and desperate over HingeJoint.motor through script

I am simply trying to use C# to assign the HingeJoints into the inspector, and have the variables within the HingeJoint to be changed through script. But no matter every different single method I have tried, even the official Unity Documentation, it still gives me errors.

Always start with the documentation: Unity - Scripting API: HingeJoint.motor

In this case, look at how they use it in the sample code.

It is a struct, so it has to be copied out to a temporary variable, then changed, then assigned back.

Well. I just did that, and there was no error, but when I looked in the inspector. The hinge motor force didn’t even budge. And the wheel didn’t even turn.

With physics it can be hard to reason about scales. If 8 doesn’t work, try 80. If 80 doesn’t work, try 800… get a feel for where the useful ranges magnitudes are located.

I said that I did look in the inspector of the HingeJoint component for changes, even setting it at 8000 didn’t do anything.

Edit: Sorry for appearing too frustrated. Unity’s documentation is too vague to even give me any hints to make such a simple task work.

Here is the entire script btw.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EntityScript : MonoBehaviour{

public Transform thisTF;
public Rigidbody thisRB;
public Transform theObserver;
public float obsDist;

public string entityName;

public enum entityType {Object, Being, Controllable, CelestialBody};
public entityType myEntityType;

public float entityPhysicalExtents;
public float entityOuterExtents;

public bool isUsingWheels;

public GameObject Wheel_FR;
public GameObject Wheel_FL;
public GameObject Wheel_RR;
public GameObject Wheel_RL;

public bool isDirringRight;
public bool isDirringLeft;
public float dirForceX;

public bool isDirringUp;
public bool isDirringDown;
public float dirForceY;

public bool isDirringForward;
public bool isDirringBack;
public float dirForceZPlus;
public float dirForceZMinus;

public bool isAngingUp;
public bool isAngingDown;
public float angForceX;

public bool isAngingRight;
public bool isAngingLeft;
public float angForceY;

public bool isAngingRollRight;
public bool isAngingRollLeft;
public float angForceZ;

void Start(){
WorldInformation.entities.Add(this);

thisTF = gameObject.transform;

theObserver = ObserverScript.thisTF;

switch (myEntityType) {
case entityType.Object:
InitializeObject();
break;
case entityType.Being:
InitializeBeing();
break;
case entityType.Controllable:
InitializeControllable();
break;
case entityType.CelestialBody:
InitializeCelestialBody();
break;
}

}


void Update(){

obsDist = Vector3.Distance(thisTF.position, theObserver.position);

}

void FixedUpdate(){
if(!isUsingWheels){

if(isDirringRight)
thisRB.AddForce(thisTF.right * dirForceX);
if(isDirringLeft)
thisRB.AddForce(thisTF.right * -dirForceX);
if(isDirringUp)
thisRB.AddForce(thisTF.up * dirForceY);
if(isDirringDown)
thisRB.AddForce(thisTF.up * -dirForceY);
if(isDirringForward)
thisRB.AddForce(thisTF.forward * dirForceZPlus);
if(isDirringBack)
thisRB.AddForce(thisTF.forward * -dirForceZMinus);

if(isAngingRight)
thisRB.AddTorque(thisTF.up * angForceY);
if(isAngingLeft)
thisRB.AddTorque(thisTF.up * -angForceY);
if(isAngingUp)
thisRB.AddTorque(thisTF.right * angForceX);
if(isAngingDown)
thisRB.AddTorque(thisTF.right * -angForceX);
if(isAngingRollRight)
thisRB.AddTorque(thisTF.forward * angForceZ);
if(isAngingRollLeft)
thisRB.AddTorque(thisTF.forward * -angForceZ);
}else{

if(isDirringRight)
thisRB.AddForce(thisTF.right * dirForceX);
if(isDirringLeft)
thisRB.AddForce(thisTF.right * -dirForceX);
if(isDirringForward){
var FRM = Wheel_FR.GetComponent<HingeJoint>();
var motor = FRM.motor;
motor.force = 8000;
}
if(isDirringBack)
thisRB.AddForce(thisTF.forward * -dirForceZMinus);

}
}

void InitializeObject(){

}

void InitializeBeing(){
entityPhysicalExtents = thisTF.localScale.y;
entityOuterExtents = entityPhysicalExtents * 1.2f;
}

void InitializeControllable(){
entityOuterExtents = entityPhysicalExtents * 1.2f;
}

void InitializeCelestialBody(){
entityPhysicalExtents = thisTF.localScale.y;
entityOuterExtents = entityPhysicalExtents * 1.1f;
}


}

Here’s my functioning example… it works pretty much as I would expect it. Pluck it apart and see what I mean, or just press PLAY.

The script included in the package below is:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HingeMotors : MonoBehaviour
{
    HingeJoint hj;

    IEnumerator Start ()
    {
        hj = GetComponent<HingeJoint>();   

        while( true)
        {
            // @kurtdekker - pick random target speed and forces
            var motor = hj.motor;
            motor.force = Random.Range( 10, 1000);
            motor.targetVelocity = Random.Range( 10, 100);
            hj.motor = motor;

            yield return new WaitForSeconds( 1.0f);
            yield return new WaitForFixedUpdate();
        }
    }
}

7011739–829513–HingeJointMotors.unitypackage (64.1 KB)

It worked! Thank you, friend.

I have no bloody idea how this fixed it, but it will be of very good usage in the future. Maybe I guess it was the fact that it by force re-fed it back to the variable with that extra line of code?

Kind of a dumb and unnecessary bit, if you ask me.

1 Like

Welcome to physics!!! Physics configuration in general causes the most-intense screen-squinting when I’m setting it up. Did I get the axis right? Motor force? Motor velocity? Wrong anchor? And then … it works. Aaaah! Like getting a splinter out.