简体   繁体   中英

Position and rotation of point in relation to two other points

To give you the setting: I'm making a Vive game in zero-g, where the player moves by grabbing handles and propelling themselves.

What I'd like is for the player to be able to rotate themselves, by grabbing a handle with both hands. Imagine how you'd move in zero-g if you held on to a bar with both hands.

To illustrate: On the left hand side the player has grabbed a handlebar with both hands. Left arm extended, right arm bent.

In the right hand side picture the player has now extended their right arm, which has rotated the player around the bar.

I guess it's easier to see it as if the player would be moving the entire world, when they do this.

My question is: How can I do this in Unity in 3 dimensions, either through math of Unity-trickery? It needs to roll, yaw and position the player relative to the hands.

Thank you!

Record the average of the three vectors. Then in the next frame, get the difference from the previous average. The difference can be used as euler angles to apply constant force to a rigidbody (or rotate an object by that amount, or other possibilities, depending on your goals). https://docs.unity3d.com/ScriptReference/Transform.Rotate.html https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html

Vector3 previousCenterPoint;

void Update() {
    Vector3 newCenterPoint = (leftHand.transform.position + rightHand.transform.position + player.transform.position) / 3f;
    if (previousCenterPoint != null)
    {
        Vector3 someEulerAngles = newCenterPoint - previousCenterPoint;
        someRigidBody.AddForce(someEulerAngles, ForceMode.VelocityChange);
    }
    previousCenterPoint = newCenterPoint;
}

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