Search Unity

Quaternion rotation to face camera based on child normal?

Discussion in 'Scripting' started by MongooseJV, Feb 20, 2017.

  1. MongooseJV

    MongooseJV

    Joined:
    Feb 25, 2014
    Posts:
    20
    So here is my dilemma. Please look at the following screen:

    rotation.jpg

    I want to be able to rotate the entire object (Puzzle) based on the selected face (Front). Front is a child object with a normal that points outwards. I have faces for all sides (6) that point outwards. Given the selected face's world normal, I want to be able to create a Quaternion to rotate the entire puzzle so that the select face is facing the camera.
     
  2. MongooseJV

    MongooseJV

    Joined:
    Feb 25, 2014
    Posts:
    20
    Just an update to this. I used the following code so far to get the plane to face the camera:

    Code (CSharp):
    1. Puzzle.transform.rotation = Quaternion.FromToRotation(focus.transform.forward, -Camera.main.transform.forward) * Puzzle.transform.rotation;
    Where the focus object consists of the child (Front). Below is what it looks like after applying the code.

    rotation2.jpg

    Now I just need to figure out how to also rotate it so it is perpendicular to the cameras view frustum....
     
  3. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Depending on how your getting your faces and setting the focus normal.. you might be able to do this: Each face is linked with a side face. This side face has to be pointing along the positive X direction. So top links to right, back to left, right with the back, etc
    have another object setup like focus, but called sideFocus. And set its normal to the linked face's normal. Then i think this code should do the trick.:

    Code (CSharp):
    1. Puzzle.transform.rotation = Quaternion.FromToRotation(focus.transform.forward, -Camera.main.transform.forward) * Puzzle.transform.rotation;
    2. Puzzle.transform.rotation = Quaternion.FromToRotation(sideFocus.transform.forward, Camera.main.transform.right) * Puzzle.transform.rotation;
    To be clear when the program decides that the front face is the focus, it looks up in its table and sees that the right face should be the normal to set sideFocus too.