Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Trouble with rotation based on other object

Discussion in 'Scripting' started by JBOMobile, Aug 26, 2021.

  1. JBOMobile

    JBOMobile

    Joined:
    Mar 4, 2019
    Posts:
    1
    Hey,

    I'm working on a two handed grabbing system, where the position and rotation is based off the hands positions and rotations.

    Here's a video of what I have currently:


    I have the position and 2 axis of the rotation working well, everything based off the hands position. But the third axis of rotation I want based off the rotation of the hands, so if the user were to lay the object on the floor, the bottom would swing towards them and the top away. This works if the hands just rotate on that one axis, as shown in the video, but if it changes on the other axis at all then it doesn't rotate correctly. Additionally, if it changes on any other axis then it also rotates the object which isn't ideal.

    Here's the code that I have for the third axis of rotation, it's currently based on the difference between the up angle of the hands and the global up (will eventually compare against angle of hand when it first grabbed and also will use median of both hands rather than just one, but want to get it working first).


    Code (CSharp):
    1. float zAngle = Vector3.SignedAngle(Vector3.up, grabbedHands[0].transform.up, Vector3.right);
    2.                 Debug.Log(zAngle);
    3.                 Debug.DrawRay(grabbedHands[0].transform.position, grabbedHands[0].transform.up, Color.red, Time.deltaTime);
    4.                 //zAngle = -zAngle;
    5.                 if (grabbedHands[0].transform.rotation.eulerAngles.z > 180)
    6.                 {
    7.                     zAngle = -zAngle;
    8.                 }
    9.                 eulerRot.z = zAngle;
    10.                 transform.rotation = Quaternion.Euler(eulerRot);

    Thanks for any help!
     
  2. Beloudest

    Beloudest

    Joined:
    Mar 13, 2015
    Posts:
    245
    I'm trying to solve the issue as well. Did you ever come to a nice solution?