How do I make a Hinge joint move slowly?
I made a long cube with a hinge joint in the middle of it. I want the player to jump on the cube and I want the cube to slowly rotate on the Z axis as long as force is applied to it. What is happening is as soon as the player jumps on the cube it rotates quickly and the player instantly falls off.
This is a visual example of what I want to happen.
https://youtu.be/K8_-IJYj-8w?t=119
Notice how when the player jumps onto the platform how it moves?
That's what I want to happen.
Does your platform have a rigidbody? You could set the angular drag higher.
Yes it has a rigidbody and I tried setting the angular drag already. That doesn't solve the issue.
I think the video example is scripted, the platform's rotation didn't seem to accelerate. However you could stop the harsh impact by using RigidbodyConstraints.FreezePositionY
for one frame in OnCollisionEnter() and then RigidbodyConstraints.None
.
Increasing the mass of the object should cause the player jumping on it have less of an effect.
And, I think - it's been awhile - a high spring value and a high damper will slow things down. A problem is that springs get stronger the more of a tilt there is, which means it will only tilt so far before the spring stops it. But you can tweak it so it tilts enough for the player to slide off. It will take forever, the first time, but it can be done.
Answer by theonerm2 · Jun 09, 2019 at 05:25 AM
I figured out a solution. I had to make 5 zones on my object. 1 zone with -0.05 rotation on the z axis. Another zone with -0.025 rotation on the z axis. A middle zone with no rotation. The 4th zone with 0.025 rotation on the z axis and the 5th zone with 0.05 rotation on the z axis. Each zone is a cube parented to the mainobject which is the platform the player walks on that tilts. The zones are cubes with the mesh renderer disabled and istrigger is selected on the box collider in it.
This is my code for a zone. Each zone has to have the script attached and the Z axis value has to be changed on each one to allow it to work. It works beautifully.
public class RotateCubeZone2 : MonoBehaviour
{
public bool PlayerInZone = false;
public GameObject MainObject;
public Vector3 Rotation;
public Vector3 RotateBack;
public Vector3 RotateForward;
public GameObject Player;
public bool PlayerWasInZone;
public Quaternion ZeroRotate = Quaternion.Euler(new Vector3(0f, 0f, 0));
// Update is called once per frame
void Update()
{
if (PlayerInZone == true)
{
MainObject.transform.Rotate(Rotation);
}
if (MainObject.transform.rotation ==ZeroRotate)
{
return;
}
if (PlayerInZone== false && MainObject.transform.rotation != Quaternion.Euler(new Vector3(0f, 0f, 0)) )
{
if (MainObject.transform.rotation.z > 0)
{
MainObject.transform.Rotate(RotateForward);
}
if (MainObject.transform.rotation.z < 0)
{
MainObject.transform.Rotate(RotateBack);
}
}
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject == Player)
{
PlayerInZone = true;
PlayerWasInZone = false;
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject == Player)
{
PlayerInZone = false;
PlayerWasInZone = true;
}
}
}
Answer by BradyIrv · Jun 09, 2019 at 01:59 AM
Scene Setup: In Hierarchy create an empty game object and add the Hinge Joint component to that object, rename it Hinge (for this example). Then create a cube (or place your model) as a child of the Hinge and add the Rigidbody component to it as well a the collider (I named this one Platform for testing).
Component Setup: On the Hinge object, set the Rigidbody to be "IsKinematic = true". In the Hinge Joint component, drag the Platform object into the "Connected Body". Then there are some variables in the Hinge Joint component that you will want to play with until it feels right for your game.
Use Spring: True
Spring Value: How much the platform will push back to its original position
Damper: "The give", what will stop the platform from spinning fast when you hop on
Use Limits: true
Min/Max: The maximum amount of angle that the platform can tilt
I've added a picture of the setup that worked for my test using a standard 3D cube and Rigidbody (Mass 1) as the player jumping onto the platform.