I was trying to change the speed on a hinge joint and had to settle on this code:
public class JointController : MonoBehaviour {
JointMotor hingeMotorThing;
void Start () {
this.hingeMotorThing=gameObject.GetComponent<HingeJoint>().motor;
}
void Update () {
if(Input.GetKey(KeyCode.LeftArrow)) {
Debug.Log("Moving left");
this.hingeMotorThing.targetVelocity = -10;
}
if(Input.GetKey(KeyCode.RightArrow)) {
Debug.Log("Moving right");
this.hingeMotorThing.targetVelocity = 10;
}
//had to add this line or nothing would happen
gameObject.GetComponent<HingeJoint>().motor=this.hingeMotorThing;
}
}
this.hingeMotorThing=gameObject.GetComponent().motor;
seems to just clone the Motor instance. Is this intentional? Using something like
gameObject.GetComponent().motor.targetVelocity = 10;
Also wouldn’t work as it says the value is read only.