Search Unity

Plane Look at Camers script, help needed

Discussion in 'Scripting' started by jessica1986, Jan 19, 2014.

  1. jessica1986

    jessica1986

    Joined:
    Feb 7, 2012
    Posts:
    621
    Hi,

    My script is fully functional, but what i want to do is, instead of allowing my billboard to face camera by completely turning, i only want it to turn a maximum of 15 degrees left and right..

    Like if i am in front of the plane, the angle of plane will be eith 90 or 180 or 360, what i want is it should maximum turn left or right to 15 degrees...

    how can i achieve this ?


    This is my script below -

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Billboard : MonoBehaviour
    6. {
    7.     Camera referenceCamera;
    8.  
    9.     public enum Axis {up, down, left, right, forward, back};
    10.     public bool reverseFace = false;
    11.     public Axis axis = Axis.up;
    12.  
    13.     // return a direction based upon chosen axis
    14.     public Vector3 GetAxis (Axis refAxis)
    15.     {
    16.         switch (refAxis)
    17.         {
    18.             case Axis.down:
    19.                 return Vector3.down;
    20.             case Axis.forward:
    21.                 return Vector3.forward;
    22.             case Axis.back:
    23.                 return Vector3.back;
    24.             case Axis.left:
    25.                 return Vector3.left;
    26.             case Axis.right:
    27.                 return Vector3.right;
    28.         }
    29.  
    30.         // default is Vector3.up
    31.         return Vector3.up;     
    32.     }
    33.  
    34.     void  Awake ()
    35.     {
    36.         // if no camera referenced, grab the main camera
    37.         if (!referenceCamera)
    38.             referenceCamera = Camera.main;
    39.     }
    40.  
    41.     void  Update ()
    42.     {
    43.         // rotates the object relative to the camera
    44.         Vector3 targetPos = transform.position + referenceCamera.transform.rotation * (reverseFace ? Vector3.forward : Vector3.back) ;
    45.         Vector3 targetOrientation = referenceCamera.transform.rotation * GetAxis(axis);
    46.         transform.LookAt (targetPos, targetOrientation);
    47.     }
    48. }
    49.  
    50.  
    51.  

    I mean i dont want this to happen
    https://www.dropbox.com/s/e6mu7d7pypc4bv9/Screenshot 2014-01-19 12.42.53.png


    I want it to turn max 15 degrees both sides from its base angle and only in the X - AXIS
     
    Last edited: Jan 19, 2014
  2. jessica1986

    jessica1986

    Joined:
    Feb 7, 2012
    Posts:
    621
    I think i can do it if i can change this line

    Code (csharp):
    1.  
    2. transform.LookAt (targetPos, targetOrientation);
    3.  

    to something like

    Code (csharp):
    1.  
    2. transform.LookAt(Vector3(targetPos.x,targetPos.y,targetPos.z),targetOrientation);
    3.  

    but this line gives me error, i think this way i can reduce the percentage of turning for my x axis

    what do you think ?