- Home /
keyboard controlled configurable joint
Hi there, can any1 tell me how i setup configurable joint which i can control by W.A.S.D. keys?
i cant simply use the fpswalker for example. as the anchor is placed out its center.
TIA! :)
Answer by duck · Jan 29, 2010 at 04:22 PM
Well, this answer would depend on what particular part of the configurable joint do you want to control? There are a lot of different settings, and you could control many of them via keyboard input.
First off, the WSAD keys (and the arrow keys) are mapped to the "Vertical" axis and "Horizontal" axis by default, so unless you've changed the input settings, you can grab these values as a range from -1 to 1 by using Input.GetAxis, like this:
Input.GetAxis("Vertical")
// and...
Input.GetAxis("Horizontal")
Then, for instance, if you wanted to control the targetRotation value of your configurable joint, you could use something like the script below in your Update() function. 'rotateSpeed' would be a float variable that determines the speed at which the target angle changes (in degrees per second), and "thisConfJoint" would be a reference to the joint being controlled. The script rotates the targetAngle of the joint around its x axis.
void Update()
{
targetAngle += Input.GetAxis("Horizontal") * Time.deltaTime * rotateSpeed;
Quaternion targetRotation = Quaternion.AngleAxis( targetAngle, Vector3.right );
thisConfJoint.targetRotation = targetRotation;
}
Of course, adjusting the configurableJoint's target rotation will only have an effect if you've set up the joint's other settings correctly so that the joint automatically applies a torque force to actually reach the specified target rotation. By default, all these settings are off so it won't do anything.
To have a joint apply torque to move towards its targetAngle (in our case, just the X axis is used) you need to set up the joint properly. For this particular example, these are the settings you'd need:
- XMotion : Locked
- YMotion : Locked
- ZMotion : Locked
- Angular XMotion : Limited
- Angular YMotion : Locked
- Angular ZMotion : Locked
- Rotation Drive Mode : X & YZ
- Angular XDrive:
- Mode : Position
- Position Spring : 1000
- Position Damper : 200
- Maximum Force : 10
You will no doubt want to configure your own spring, damper and max force values to suit the mass of the objects in your scene.
I used a system very similar to the above described method to the Excavator Arm Controls in this Christmas Demo! (You can smash up ice sculptures with an excavator)
Skive Christmas Excavator Demo
(the link to play is at the bottom of the blog post)
Thank you!. well the use of my configjoints will be pretty much the same as in your excavator in your excavator game.. is there no way to stop the hinges from swinging around? if you rapidly rotate left/right. you can see the stick swinging out of the official joints :)
Thanks!
Hi, I managed to achieve a excavator that doesn't swing, but it's using some extrem setup: for physics time step and such: http://www.fabrejean.net/projects/excavator/ more infos on avoiding swingings: http://answers.unity3d.com/questions/14358/non-springy-configurable-joint.html
@duck If I could upvote this answer five times, I would. Thanks. You helped me to solve 2 issues. First "+=" I did not realise that I need to add the intended angle to the Quaternion. Secondly, I missed the setup on the limiting joint around axis of interest.
Answer by konzon · Nov 22, 2010 at 10:57 PM
Duck,
Did you use configurable joints for the "hydraulic" components of the arms as well? If so, did you find a quick way to calculate the target angles for the piston and cylinder? I've been beating my head on this one.... Tried colliders as well. Anything you can offer in regards to the hydraulic motions would be very helpful.
I like your excavator demo - it's that time of year again!
Never$$anonymous$$d Duck... I figured out that by removing physics from the hydraulics I can simply use transform.LookAt to aim the hydraulic components at the appropriate target.
Your answer
Follow this Question
Related Questions
configurable joint targetrotation: How to compute it 3 Answers
Lock axis/rotation on 2d plane with capsule collider 2 Answers
Flip over an object (smooth transition) 3 Answers
How to restore a rigidbody rotation 1 Answer
Configurable joint around 2 axis 3 Answers