Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Camera Target

Discussion in 'Scripting' started by ChillWill, Aug 13, 2008.

  1. ChillWill

    ChillWill

    Joined:
    Aug 13, 2008
    Posts:
    13
    I've been busy understanding Unity and its scripting, and I was wondering if there's another way of assigning a camera's Target dynamically, instead of setting it before playing out a scene. I've tried using the Find function, but that just gave me errors. What I want to do is instantiate a player and camera dynamically, and have the camera point to the player in 3rd person view. Thanks in advance for any help!
     
  2. ratamorph

    ratamorph

    Joined:
    Sep 2, 2007
    Posts:
    458
    As a starting point you could just do...

    transform.LookAt(targetPosition);

    if you do this inside the update on a script attached to the camera the camera will always face the target object.
     
  3. ChillWill

    ChillWill

    Joined:
    Aug 13, 2008
    Posts:
    13
    I get an "Unknown identifier" error when I tried what you suggested.

    For a test, I created a Player prefab (with "Player" tag) and camera prefab, and assigned the smoothfollow.js script to the camera. What I want to do is if there's no target assigned, to have the camera search its environment and find an object with the "Player" tag, and then assign that object as the camera's target. I'm wondering if it's possible. Here's what I tried in the smoothfollow.js script:
    Code (csharp):
    1.     if (!target)
    2.     {
    3.    
    4.         target = transform.Find("Player");
    5.         //return;
    6.    
    7.     }
    I'm starting to wonder if this is even possible...
     
  4. burnumd

    burnumd

    Joined:
    May 27, 2008
    Posts:
    367
  5. ChillWill

    ChillWill

    Joined:
    Aug 13, 2008
    Posts:
    13
    Thanks! That did the trick. That, and the LookAt function needs a transform, not a gameObject. So the final code I have in the smoothfollow.js script is:
    Code (csharp):
    1.     if (!target)
    2.     {
    3.         findMe = GameObject.FindWithTag("MaleRagDoll");
    4.         target = findMe.transform;
    5.         distance = 1.0;
    6.         //return;
    7.     }
    So this successfully assigns the target to the new one. Unfortunately, the camera still doesn't follow the object when it's moving. I'm trying to get a camera change targets after hitting the player, from player to ragdoll, and then have the camera follow the ragdoll.

    The target change was successful, but the camera doesn't follow now.
     
  6. burnumd

    burnumd

    Joined:
    May 27, 2008
    Posts:
    367
    Glad it works. It won't alter anything significantly, but you can also request the transform (or any component or function) of the GameObject returned by FindWithTag() on the same line:
    Code (csharp):
    1. target = GameObject.FindWithTag("MaleRagDoll").transform;
    It's pretty much a stylistic decision (and, used in excess, can definitely become difficult for human readers), but it will save a few keystrokes, if you're into that kind of thing.
     
  7. ChillWill

    ChillWill

    Joined:
    Aug 13, 2008
    Posts:
    13
    Oh, I'm always into saving keystrokes when I can, thanks!

    The only problem I'm running into now is having the camera follow the ragdoll. As it stands, it'll just point to where it becomes a ragdoll, but doesn't follow it as it's thrown into the distance. Is this because it's a ragdoll or something?

    And again, thanks for all the tremendous support here in these forums...so friendly...
     
  8. Luke-Houlihan

    Luke-Houlihan

    Joined:
    Jun 26, 2007
    Posts:
    303
    The easiest way to get the camera to follow your ragdoll would be to just parent it, but that might not look very good. You might want to check out the 3rd person platformer tutorial and the wiki, I think there is a script called SmoothFollow.js in either or that would demonstrate what you need.
     
  9. ChillWill

    ChillWill

    Joined:
    Aug 13, 2008
    Posts:
    13
    Hey guys, still pondering on this and I was wondering...the way I'm instantiating the ragdoll is immediately after a collision with a ball. I would destroy the game object (which the main camera was pointing at), and then assign the main camera's target to the newly instantiated rag doll. Does that have any effect in whether or not the script runs the LookAt function properly?
     
  10. ChillWill

    ChillWill

    Joined:
    Aug 13, 2008
    Posts:
    13
    Figured out what was going on...

    The ragdoll parent was just an empty folder, so I just had the camera focus on its children that had the colliders and rigidbody instead. Follows it like it should.