Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

inherit position - should be an easy one

Discussion in 'Scripting' started by tattyswad, Aug 2, 2012.

  1. tattyswad

    tattyswad

    Joined:
    Sep 22, 2010
    Posts:
    106
    Hi Guys,

    I am tweening a camera around various positions around a map. At the moment I have the following code:

    Code (csharp):
    1. var mainCam : GameObject;
    2. var vpPosition : Vector3;
    3. var vpRotation : Vector3;
    4.  
    5. function OnMouseUp (){
    6.  
    7. iTween.MoveTo (mainCam, vpPosition, 2);
    8. iTween.RotateTo (mainCam, vpRotation, 2);
    9. }
    I have set up various cameras in my scene where I want them but at the moment I am having to copy and paste their position and rotation into the variables above. I have quite a few cameras and it becomes time consuming. Can anyone suggest the correct line of code so I can declare the positioned cameras as a variable and then take their rotation and position into the iTween without having to copy and paste?

    The reason I am doing this and not just enabling and disabling the various cameras is for a smooth fly-to and also the main camera has quite a few components attached which require it to remain the main camera.

    Thanks
     
  2. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    Just like your main camera, you can declare a GameObject variable (or array of them if your doing some soft of path movement) and get the location, rotation etc from GameObject.transform.

    So for example you might do:
    Code (csharp):
    1.  
    2.     var mainCam : GameObject;
    3.     var targetCam : GameObject;
    4.      
    5.     function OnMouseUp (){
    6.      
    7.     iTween.MoveTo (mainCam, targetCam.transform.position, 2);
    8.     iTween.RotateTo (mainCam, targetCam.transform.eulerAngles, 2);
    9.     }
    10.  
    Not tested the code, but I'm sure you get the idea

    Also, after you've set your target cameras into position, you can remove the camera components from them, as you simply need the empty game objects as a reference for position and rotation :)
     
  3. tattyswad

    tattyswad

    Joined:
    Sep 22, 2010
    Posts:
    106
    That works a treat.

    Spot on thankyou very much.

    Good idea to remove th cam component too.

    Cheers
     
  4. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    Your welcome :)