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 target and instantiated prefab

Discussion in 'Scripting' started by GusM, Nov 19, 2007.

  1. GusM

    GusM

    Joined:
    Aug 27, 2005
    Posts:
    585
    I have a character I switch to a rigidbody with a key. And I have a camera with stock smoothfollow and smoothlookat scripts. I am trying to switch their targets to the instantiated object, but I just cannot get it.

    I guess what I need is a version of the two mentioned camera scripts with a "find object with tag" function as a target. But I am too noob to figure it out.

    Any help will be really apreciated.
     
  2. Brian-Kehrer

    Brian-Kehrer

    Joined:
    Nov 7, 2006
    Posts:
    411
    Just one object? Or a bunch of instantiated objects?

    In the camera scripts, all of them should be in terms of 'target'
    Here is my easy way to switch--if you put this in the same script it should work.

    Make sure when you instantiate a new object, you set theObject.transform = newInstantiatedObject.transform

    Code (csharp):
    1.  
    2. //call this when you want to switch targets
    3. function ChangeTarget(){
    4.  
    5. if (target == player.transform){
    6.    target = theObject.transform;
    7.    return;
    8.    }
    9. if (target == theObject.transform){
    10.    target = player.transform;'
    11.    return;
    12.   }
    13. //in case the camera is lost
    14.  target = player.transform;
    15. }
    16.