Search Unity

Rotate object to face away from wall

Discussion in 'Scripting' started by kityanlam3, Jul 26, 2016.

  1. kityanlam3

    kityanlam3

    Joined:
    Feb 2, 2016
    Posts:
    41
    I have a game similar to the sims where you can place furniture and design your house. Now I'm stuck at placing of the wall furniture, things like windows or paintings etc.

    Below you can see the picture frame model that I'm using for testing. The player selects the object and when he clicks within the boundaries of the room,it fires raycasts to check shortest distance to wall and then the object sticks to the wall closest to it.

    I want the picture frame to rotate automatically to align with the wall it sticks to, I'm not good with rotations and Quaternions, but a quick google check tells me I'm looking for Quaternion.LookRotation? I'm not sure if it's the one I'm looking for and if it is I'm not sure how to use it. Any help is appreciated, thanks!

    example.png
     
  2. zoran404

    zoran404

    Joined:
    Jan 11, 2015
    Posts:
    520
    Well first you need to get the GameObject of the wall you're trying to snap the image to.
    Then the simplest way to solve this is to have a component on the wall that would hold the rotation for the objects that snap to it, which you can just apply to your image.

    Anther way people solve this involves raycasts (where you can use the `normal' of the raycast hit). But I can't give you a good explanation on how to solve this without knowing how you place the image.
     
  3. phaem

    phaem

    Joined:
    Jan 5, 2015
    Posts:
    76
    Rotate all your wall in the editor the way theirs forward vectors (blue arrow) face the center of the room and set the furniture transform.rotation = wall.transform.rotation.
     
  4. zoran404

    zoran404

    Joined:
    Jan 11, 2015
    Posts:
    520
    Another good idea.
    Although if the model's rotation doesn't allow it (eg. if the model is rotated strangely) you'll still have to either make a component to store an offset or place the GameObject (with the model) inside of a container GameObject who's rotation you ca then use.