Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Camera Look at script

Discussion in 'Scripting' started by keithsoulasa, Jul 9, 2012.

  1. keithsoulasa

    keithsoulasa

    Joined:
    Feb 15, 2012
    Posts:
    2,126
    Here's a script I wrote that lets a camera look at an object and switch what object your looking at by pressing a gui button ( this is just an example, you can call the function from anywhere ) .
    Put the objects you want to look at in the LookObjects array , enjoy and feel free to use and give me suggestions if i'm not doing this right
    http://unifycommunity.com/wiki/index.php?title=Camera_Target_Swap

    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class SwapCamE : MonoBehaviour {
    7. // Writen by Keith Wilson
    8. // License , Enjoy , and hopefully add to it and pass it on
    9.     // Highly modified version of the Smooth LookAt Script
    10. // has target  
    11. public Transform[] LookObjects ;
    12. public int CamNum ;
    13. public int MaxCam = 1 ;
    14.     public int x  =1 ;
    15.     public int y = 1 ;
    16.     public int b = 1 ;
    17.     public int c = 1 ;
    18.    
    19.    
    20.    
    21.    
    22. public Transform target ;
    23. public float damping = 6;
    24. public bool smooth = true;
    25. public bool Getswap  = true ;
    26. //public GameObject ;
    27. //@script AddComponentMenu("Camera-Control/Smooth Look At")
    28.  
    29. void LateUpdate () {
    30. if (Getswap == true ) {
    31.  
    32. target = LookObjects[CamNum] ;
    33.     }  
    34.    
    35.    
    36.     if (target) {
    37.         if (smooth)
    38.         {
    39.             // Look at and dampen the rotation
    40.             var rotation = Quaternion.LookRotation(target.position - transform.position);
    41.             transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
    42.         }
    43.         else
    44.         {
    45.             // Just lookat
    46.             transform.LookAt(target);
    47.         }
    48.     }
    49. }
    50.  
    51. void Start () {
    52. MaxCam  = LookObjects.Length - 1 ;
    53.        
    54.         // Make the rigid body not change rotation
    55.     if (rigidbody)
    56.         rigidbody.freezeRotation = true;
    57. }  
    58.    
    59.    
    60.    
    61.     // Use this for initialization
    62.     void CamSwap () {
    63.    
    64.     if( CamNum == MaxCam)
    65.         {CamNum = 0 ;  
    66.         //SmoothLookObjects[CamNum] ;
    67.         }
    68.         else
    69.         { CamNum ++ ;
    70.         //LookObjects[CamNum];
    71.        
    72.         }
    73.        
    74.        
    75.        
    76.     }
    77.    
    78.   void OnGUI() {
    79.        
    80.        
    81.         if (GUI.Button(new Rect(10*x, 70*y, 50*b, 30*c), "Swap"))
    82.            
    83.                 CamSwap();
    84.     }
    85.    
    86.    
    87.    
    88.    
    89.    
    90.    
    91.    
    92.    
    93.    
    94. }
    95.  
     
  2. col000r

    col000r

    Joined:
    Mar 27, 2008
    Posts:
    698
    Rewrote this a bit, here's my version:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [AddComponentMenu( "Camera-Control/Camera Look At" )]
    4. public class CameraLookAt : MonoBehaviour {
    5.     //CONFIG
    6.     [Header("Config")]
    7.     [SerializeField] private Transform[] m_Targets;
    8.     [SerializeField] private float m_Damping = 6f;
    9.     [SerializeField] private bool m_Smooth = true;
    10.     [SerializeField] private bool m_ShowDebugUI = true;
    11.  
    12.     //RUNTIME DATA
    13.     [Header("Runtime Data")]
    14.     [SerializeField] private int m_CurrentTargetId;
    15.     private Transform m_CurrentTarget;
    16.  
    17.  
    18.     void Awake() {
    19.         Rigidbody rb = GetComponent<Rigidbody>();
    20.         if( rb != null ) rb.freezeRotation = true;
    21.         ShiftTargetBy( 0 );
    22.     }
    23.  
    24.     void LateUpdate() {
    25.         if( m_CurrentTarget != null ) {
    26.             if( m_Smooth ) {
    27.                 // Look at and dampen the rotation
    28.                 var rotation = Quaternion.LookRotation( m_CurrentTarget.position - transform.position );
    29.                 transform.rotation = Quaternion.Slerp( transform.rotation, rotation, Time.deltaTime * m_Damping );
    30.             }
    31.             else {
    32.                 // Just lookat
    33.                 transform.LookAt( m_CurrentTarget );
    34.             }
    35.         }
    36.     }
    37.  
    38.     public void ShiftTargetBy( int offset ) {
    39.         if( m_Targets.Length == 0 ) return; //***** nothing to do here
    40.      
    41.         m_CurrentTargetId += offset;
    42.         //unwind from above
    43.         while( m_CurrentTargetId >= m_Targets.Length ) {
    44.             m_CurrentTargetId -= m_Targets.Length;
    45.         }
    46.  
    47.         //unwind from below
    48.         while( m_CurrentTargetId < 0 ) {
    49.             m_CurrentTargetId += m_Targets.Length;
    50.         }
    51.  
    52.         //set new target
    53.         m_CurrentTarget = m_Targets[m_CurrentTargetId];
    54.     }
    55.  
    56.     public void Next() {
    57.         ShiftTargetBy( 1 );
    58.     }
    59.  
    60.     public void Prev() {
    61.         ShiftTargetBy( -1 );
    62.     }
    63.  
    64.     public void OverrideCurrentTarget(Transform target) {
    65.         m_CurrentTarget = target;
    66.     }
    67.  
    68.     void OnGUI() {
    69.         if( m_ShowDebugUI ) {
    70.             GUILayout.BeginHorizontal();
    71.             if( GUILayout.Button( "Prev" ) ) {
    72.                 Prev();
    73.             }
    74.  
    75.             if( GUILayout.Button( "Next" ) ) {
    76.                 Next();
    77.             }
    78.  
    79.             GUILayout.EndHorizontal();
    80.         }
    81.     }
    82. }