- Home /
How to control the angle of a joint using a variable
I want to use a variable to control the angle between two connected objects in unity, but I'm not sure what syntax to use for scripting the joint.
So I have a variable between 0 and 255, and I want the angle of a joint to match that number. How can I script the transform?
P.S. I'm pretty new to unity, and I'm using C# because I'm getting the angle data from a sensor attached to my computer.
Answer by MikeNewall · Apr 26, 2014 at 09:01 PM
Attach a script to the root joint and set its rotation by multiplying a vector by the input value. For example this would rotate around the X axis the number of degrees specified by the variable:
transform.rotation = Quaternion.Euler(Vector3.right * inputVariable);
Quaternion.Euler( inputVariable, 0, 0);
is the same thing, and I find it easier to read.
I'm used to thinking in vectors :p But yeah that is easier to read. I suppose the benefit of using a vector like that is you can change the rotation axis on the fly.
Answer by Marcopolo1613 · Apr 30, 2014 at 03:56 AM
Thank you all for your help, I was able to do what I wanted with a Hinge Joint, and this code:
JointSpring spr = hingeJoint.spring;
spr.targetPosition = myInputData; // myInputData is the input from my sensor
hingeJoint.spring = spr;
This makes the joint move toward the angle value I am receiving from my sensor.
How do you stop the joint from bouncing back and forth when it reaches the angle you specify? I want to set the angle and prevent it from moving anymore.
Your answer
![](https://koobas.hobune.stream/wayback/20220613143641im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
joint with two axis 1 Answer
How to detect when joint reaches its limits? 0 Answers
Joint system scale problems 2 Answers
How angle limit in hingle joint 2d work ? 0 Answers
How target rotation and target angular velocity work in configurable joint ? 1 Answer