[FIXED] Changing the Target Velocity of a Hinge Joint Motor in script
I'm trying to get a Rigidbody to turn itself as it moves around a point by using a hinge joint motor. The only value I need to affect is the Target Velocity. When I initially tried this, I got an error telling me to store hingejoint.motor.targetvelocity as a temporary variable before altering it and reassigning it (getting it and setting it;) however, now that I've done that, I get no errors, but the target velocity remains how it was set before runtime. Here's my code:
using UnityEngine;
using System.Collections;
public class MerryGoRound : MonoBehaviour {
public GameObject funnel;
public float turnSpeed = 1f;
HingeJoint joint;
JointMotor motor;
void Start () {
joint = GetComponent<HingeJoint>();
motor = joint.motor;
}
void FixedUpdate () {
Vector3 line1 = funnel.transform.position - transform.position;
Vector3 line2 = (funnel.transform.position + new Vector3(0f, 10f, 0f)) - transform.position;
Vector3 perp = Vector3.Cross(line1, line2);
Vector3 wheelForwardVector = transform.forward;
float tempTargetVelocity = motor.targetVelocity;
tempTargetVelocity = Vector3.Angle(wheelForwardVector, perp) * turnSpeed;
motor.targetVelocity = tempTargetVelocity;
}
}
Here's the code that works:
using UnityEngine;
using System.Collections;
public class $$anonymous$$erryGoRound : $$anonymous$$onoBehaviour {
HingeJoint myJoint;
Joint$$anonymous$$otor myJoint$$anonymous$$otor;
public float targVel = 2f;
void Start () {
myJoint = GetComponent<HingeJoint>();
}
void Update()
{
myJoint$$anonymous$$otor = myJoint.motor;
myJoint$$anonymous$$otor.targetVelocity = targVel;
myJoint.motor = myJoint$$anonymous$$otor;
}
}
Answer by Tronyc · Dec 27, 2017 at 04:02 AM
This didn't work for me. I entered the code exactly as shown in the comment to your post, except I'm using motorSpeed instead of targetVelocity. Does that make a difference? I really need to be able to control the movement of the Joint.
Your answer
Follow this Question
Related Questions
Hinge Joint Doesn't Move Unity 2018.4.17f1 0 Answers
step 10 space shooter asteroid random rotation not working 4 Answers
Problem with Rigidbody.rotation and AddTorque() being together 0 Answers
Rotating kinematic rigidbody at run time(2D sidescrolling project) 0 Answers
How to AddForce to a Rigidbody from Game Objects Spawned from an Array, One Prefab at a Time? 2 Answers