Search Unity

convert rotation into distance

Discussion in 'Scripting' started by Gnimmel, Aug 29, 2017.

  1. Gnimmel

    Gnimmel

    Joined:
    Apr 21, 2010
    Posts:
    358
    I am presently trying to finish up a PC version of Jogger, which is a VR game where you jog on the spot to move forward.

    On Mobile I used a asset from the asset store called VR Step to handle the forward motion which I then tweaked to work on the Oculus. I have one problem I can't seem to work out though and was hoping someone here could point me in the right direction.

    On mobile it used the gyro to get the up and down motion used to detect running on the spot, but on the Oculus I use the camera position because it's tracking the users head.

    This works until I look up or down because the camera is where the eyes are and you rotate your head from the base of the skull which means the main camera translates up and down while looking in the same directions.

    What I need is a way to subtract the head rotation from the vertical movement caused by looking up or down, but I have no idea how to convert the camera upwards angle into distance.

    For a better understanding of what I'm doing, this is the plug-in I used and the second video on the page is the mobile version of Jogger.

    https://www.assetstore.unity3d.com/en/#!/content/60450
     
  2. unit_nick

    unit_nick

    Joined:
    Jul 15, 2017
    Posts:
    23
    If I understand what you are asking then here is what I think you need to do.

    When the camera rotates you receive an angle. To subtract the camera height due to rotation you need to know 2 things.
    1. The angle around the pivot point.
    2. The distance the camera is from the pivot point.
    Once you have those 2 you can use trigonometry to calculate the height of the opposite side. Something like:
    Code (csharp):
    1.  
    2. float HeightOfRotation(float angleDegrees, float cameraDistance)
    3. {
    4.     return Mathf.Sin(angleDegrees * Mathf.Deg2Rad) * cameraDistance;
    5. }
    6.  
     
  3. Gnimmel

    Gnimmel

    Joined:
    Apr 21, 2010
    Posts:
    358
    Thanks for the reply.

    That is nicer way to solve it than what I ended up doing. I added a few extra null objects, one connected to the camera and measured the height difference between them to get a value I could work with.

    It ended up working well, but I will remember your reply if I ever need this again.