Search Unity

Using RotateAround so object always "faces" the point

Discussion in 'Scripting' started by vinfang, Sep 19, 2013.

  1. vinfang

    vinfang

    Joined:
    Jul 13, 2013
    Posts:
    14
    The scenario is I have positioned and rotated the object in a certain way so that it looks nice from our main camera before hitting play in Unity. This means having its position be located at 0.05, 0, 14, having the main camera look at the X, Y plane with the world Z axis being the forward, and having the front side of the mesh be facing towards the camera by flipping it to 270 on its X axis. Here's an image to give an idea with the World Z axis in blue facing away from the camera.

    $screencap.png
    $screencap2.png


    The problem I'm trying to solve is that based on where the user clicks on that green puzzle piece and as the user drags it around the center, which is origin of this world, the puzzle piece should always face the origin by rotating based on where the user clicked on the object. I've used the this.transform.rotatearound(mousegameworldcoordinates, Vector3.front, amountToRotate) function and I calculated the amountToRotate by using Vector3(mouseGameWorldCoordinates, previousMouseGameWorldCoordinates) along with some if statements so that it rotates left or right. The problem is that if the user drags this object across the middle, the rotation becomes off.

    One way I thought would have worked was store the objects original rotation as a quarternion, so that during a click and drag event, I just reset the object's rotation back to the initial and then calculate the angle between the current mouse coordinates and Vector3.Right so that it'll always rotate the correct amount, but doing that first and then calling RotateAround causes the object to just rotate around the mouse cursor in a sporatic way.

    I've tried all sorts of different ways without success and I'm wondering if I'm making this more complicated than it should be or if there's a more simple way to do this?
     
  2. bearcat

    bearcat

    Joined:
    Dec 8, 2012
    Posts:
    25