I’m trying to prototype a foosball game and having trouble with the controls. I basically have a long thin cylinder, which has a box in the middle. ( image attached for reference). I want to control it’s rotation along the Y axis. In the image attached, I have the box as a child of the cylinder. The cylinder has a rigidbody with ‘Use Gravity’ turned off. Calling “AddTorque” works great while the model is floating in the air, but has all sorts of problems when I place it context with the other colliders/game objects (ball, game box, etc).
So I’m thinking I can’t use physics on this part and have to use rotation, but I want the effect I get when using AddTorque. Basically when you release control of the ‘rod’, it keeps spinning and if untouched, will spin to a stop. Anyone have any ideas?
Thanks!
Psx
You want to use joints for this. Most likely the hingejoint. You can then control the rotation using target angles and a motor.
That works perfectly, Thanks!
I ended up giving the ‘player (box in example above)’ the hinge component and setting it’s rigidbody to the cylinder. This way I’m able to rotate the player(s) and move the whole lot forward and back.
So far so good! I have the following script attached to the player object. It almost does everything I want, but it’s not as fast as I’d like, and I can’t seem to make it any faster. The only way I’ve been able to make it faster is to increase the TimeScale for the project. This is fine, since my game is actually a bit larger in scale than it’s real life counterpart, but it’s still not as snappy as I’d like. So here’s the script. Would I be any better off using this motor stuff? If so… how would I go about doing it?
public var handed : String;
function Start() {
if(!handed) {
handed == "right";
}
}
function FixedUpdate () {
// put on the brakes! possibly unnecessary?
if(Input.GetButtonDown("Fire1")) {
rigidbody.angularDrag = 1000;
}
if(Input.GetButton("Fire1")) {
// turn Off drag
rigidbody.angularDrag = 0;
var mouse_x = Input.GetAxis("Mouse X");
// negative for lefties, positive for righties!
mouse_x = (handed=="right") ? mouse_x : -mouse_x;
if(mouse_x != 0) {
rigidbody.AddTorque ( mouse_x, 0, 0, ForceMode.Impulse);
} else {
// we're not moving! lock down RotationDriveMode
rigidbody.angularDrag = 1000;
}
}
if(Input.GetButtonUp("Fire1")) {
rigidbody.angularDrag = .5;
}
}
Thanks again!
psx
You might want to halve the fixed time step in Edit → Project Settings → Time
And increase the maximum angular velocity in the physics settings.
Thanks again! That seemed to do the trick nicely!
psx