Search Unity

screen position delta to local rotation around an objects own Vector3.Up

Discussion in 'Scripting' started by Ben-Howard, May 30, 2017.

  1. Ben-Howard

    Ben-Howard

    Joined:
    Jul 21, 2014
    Posts:
    23
    Hi guys,

    I am having a tough time wrapping my head around this problem. I have an object that rotates based on a rotation handle and screen position delta. This rotates the object and the handles. The handles are intended to allow rotation around only its set axis. I have a set of rotation handles for rotation around the objects' x, y, and z axis.

    So, when everything starts, it is all good. For example, rotation around y takes screenPosDelta.x and rotates the entire object left and right as expected. The problem comes in when I rotate around z for instance, 90 degrees so that my once left right handle for rotation around y now becomes an up down handle. Intuitively, it makes more sense to then check screenPosDelta.y at this point.

    After much time trying to figure out an eloquent solution for this problem, I finally resolved myself to having to check for the rotation of the object and switching the axis for screenPosDelta based on this. As you can imagine, it clutters up my code quite a bit.

    My question is, then, is there a better way to do this?
    Code (CSharp):
    1. if (RotateAroundAxis == RotationHandleTypes.aroundY)
    2.         {
    3.             startingRot.x = Mathf.Abs(startingRot.x);
    4.             if(startingRot.x >= 0.0f && startingRot.x <= 45.0f)
    5.                 HostTransform.RotateAround(HostTransform.localPosition, HostTransform.up, deltaSourcePos.x * _RotSpeed * -1.0f);
    6.             else if(startingRot.z >= 0.0f && startingRot.z <= 45.0f)
    7.                 HostTransform.RotateAround(HostTransform.localPosition, HostTransform.up, deltaSourcePos.x * _RotSpeed * -1.0f);
    8.             else if(startingRot.x >=45.0f)
    9.                 HostTransform.RotateAround(HostTransform.localPosition, HostTransform.up, deltaSourcePos.y * _RotSpeed * -1.0f);
    10.             else if (startingRot.z >= 45.0f)
    11.                 HostTransform.RotateAround(HostTransform.localPosition, HostTransform.up, deltaSourcePos.y * _RotSpeed * -1.0f);
    Thanks!