简体   繁体   中英

How to Hold position on cylinder/pendulum using unity3d

I'm new on Unity and trying to create a cylinder moving as a rigid pendulum. I use a hinge joint in top of cylinder and a motor to ad force.

How do I get the cylinder to hold position and not fall back to "dead" position when the I stop applying force?

JointMotor m = new JointMotor();
m.force = 10000;
m.targetVelocity = 0;
m.freeSpin = true;
GetComponent<HingeJoint>().motor = m;

You can try to calculate it manually. Create an empty object as a pivot, make the arm and bob it's child objects and rotate your pivot using Lerp() . Something like this might work:

public class Penduluum : MonoBehaviour {
 public float angle = 45.0f;
 public float speed = 1.5f;

 Quaternion Start, End;

 void Start () {
     Start = Quaternion.AngleAxis ( angle, Vector3.forward);
     End   = Quaternion.AngleAxis (-angle, Vector3.forward);
 }

 void Update () {
   transform.rotation = Quaternion.Lerp (Start, End, (Mathf.Sin(Time.time * speed) + 1.0f) / 2.0f);
 }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM