- Home /
Directly Rotate a Hinge?
Hey guys, I've got most of this game worked out, and it's actually functioning great, but I'm trying to add another control scheme and I can't seem to get it working... In the game, you're arms are attached to the body via hinge joint, and you press Left/Right to rotate them around using this type of action:
if(Input.GetKey("d")){
hingeJoint.motor.force = force;
hingeJoint.motor.targetVelocity = 350;
hingeJoint.motor.freeSpin = false;
}
Now, I also set this up for use with my PS3 controller, as such:
if(Input.GetAxis("LHorz")){
hingeJoint.motor.force = force;
hingeJoint.motor.targetVelocity = velo*rotspeed;
hingeJoint.motor.freeSpin = false;
}
What I want to be able to do, is instead of just adding to the motor's velocity, I want to be able to rotate the arm to point exactly where the analog stick is pointing. The code that I can use that works fine for a regular object is this:
var axisHorizontal = Input.GetAxis("Horizontal");
var axisVertical = Input.GetAxis("Vertical");
transform.localRotation = Quaternion.Euler((Mathf.Atan2(axisHorizontal, axisVertical) * Mathf.Rad2Deg), 0.0 , 0.0);
The problem is, is when you apply that code to the arms, which again are attached to the body by a hinge joint, the whole character just goes absolutely haywire...
Here's a pic so you can get the idea of what motion I'm trying to get at:
Thanks so much for any help!
Answer by KalebGraceTwistory · Feb 03, 2015 at 12:31 AM
Try this:
float angle = 45f;
JointLimits jl = new JointLimits();
jl.min = angle;
jl.max = angle;
hingeJoint.limits = jl;
It seems to work great for me. Note: If you're actually using the limits for the standard purpose, you'll have to cache off the original JointLimits struct and reapply it later.
Will this solution working fine if the body is continuously in contact with other non kinematic rigidbodies which are resting on this joint. and u want to slowly rotate the joint to let them fall ...
i meam will there be any issues in their force exchange?
Your answer
Follow this Question
Related Questions
Make a rope 0 Answers
advanced problem for me regarding transfering rotation from the gizmo to the object 0 Answers
Rotate An Object Toward The Direction The Joystick Is Pressed 3 Answers
stay the rotation in character 1 Answer
Making my Game Object face according to direction of mobile joystick 2 Answers