Results 1 to 7 of 7
  1. #1
    Join Date
    May 2010
    Posts
    1

    Question TP_Camera does not focus on targetLookAt

    Hello, I've been following the Unity3D third person character controller series and have come across a snag after videos 14 and 15 (Secondary Functionality Implementation) while implementing the method UseExistingOrCreateNewMainCamera() of the TP_Camera class: the Main Camera (created by the script or manually by me) does not focus itself onto the targetLookAt gameobject (created by me).

    What the script does do is successfully compiles and creates the Main Camera and targetLookAt game objects if not already existent with proper tags and script attachments.

    Source code snippets:

    My TP_Controller class' Awake() method:
    Code:
    void Awake() 
    	{
    		CharacterController = GetComponent("CharacterController") as CharacterController;
    		Instance = this;
    		TP_Camera.UseExistingOrCreateNewMainCamera();
    	}
    My TP_Camera class' UseExistingOrCreateNewMainCamera() method:
    Code:
    public static void UseExistingOrCreateNewMainCamera()
    	{
    		GameObject tempCamera;
    		GameObject targetLookAt;
    		TP_Camera myCamera;
    		
    		if (Camera.mainCamera != null)
    		{
    			tempCamera = Camera.mainCamera.gameObject;
    		}
    		else
    		{
    			tempCamera = new GameObject ("Main Camera");
    			tempCamera.AddComponent("Camera"); // adds camera functionality
    			tempCamera.tag = "MainCamera"; // actually makes tempCamera the main camera
    		}
    		
    		tempCamera.AddComponent("TP_Camera"); // adds TP_Camera script to the camera object
    		
    		// set tempCamera+TP_Camera script to myCamera
    		myCamera = tempCamera.GetComponent("TP_Camera") as TP_Camera;
    		
    		targetLookAt = GameObject.Find("targetLookAt") as GameObject;
    		
    		if (targetLookAt == null)
    		{
    			targetLookAt = new GameObject("targetLookAt");
    			targetLookAt.transform.position = Vector3.zero;
    		}
    		
    		// take local targetLookAt (either found or created) and setting
    		// to the public camera.TargetLookAt
    		myCamera.TargetLookAt = targetLookAt.transform;
    	}
    Things I've done so far:
    -I've made sure to watch/rewatch the Theory(14) and Implementation(15) videos to ensure that my code matches what is shown in the video, and that it follows the logic.
    -When I make the targetLookAt gameobject in Unity I parent it under the character stand in object.
    -When the camera is made by me in Unity I make sure that it's named "Main Camera" and is tagged under the MainCamera tag.
    -I searched this forum for similar problems, the only one I found was this post: , but setting my targetLookAt gameobject tag to player and the layer to ignoreRayCast (I couldn't find 'ignorecast') didn't seem to affect anything, nor does the script really look like it relies on it.

    Because of these results I'm guessing I have some gameobject or component property in the Unity editor incorrectly set, but I have been unable to find anything. Any and all help is greatly appreciated.

    Thank you!

  2. #2
    Join Date
    Dec 2011
    Location
    CA, USA
    Posts
    27
    taeseong,
    Did you ever resolve this issue?

  3. #3
    Join Date
    Jan 2011
    Location
    Essex, Vermont
    Posts
    103
    I'm just now doing the videos, and I found the issue with the camera not following the player.

    If you have manually created the TargetLookAt GameObject and made it a child of Player, the camera will follow the player.

    If you let the code create the TargetLookAt GameObject, the camera will not follow the player.

    Why?

    Because the code, as written in the video, does not make targetLookAt a child of the Player. The code simply places targetLookAt at the center of the world with:

    targetLookAt.transform.position = Vector3.zero;

    The result is the camera is looking at targetLookAt on the ground just fine, but it's not part of the Player. The Player is standing on it when the game starts. When you move the Player, the Player runs off and the camera stays right where it is since targetLookAt is still right there on the ground.

    When you press Play, you can see that TargetLookAt was created, but not as child of Player. Drag it onto Player while still in Play mode and you will now follow the Player.

    Now I need to figure out the changes in the code to make targetLookAt a child of the Player when Play is pressed.

  4. #4
    Join Date
    Jan 2011
    Location
    Essex, Vermont
    Posts
    103
    Did a lot of messing around trying to figure out how to make TargetLookAt a child of Player in code. Just not smart enough to figure that out yet. Browsed through the scripting reference and just didn't find what I needed.

    Now I did make the camera follow the Player, but I didn't use the TargetLookAt GameObject to do it. I just had the camera follow the Player itself.

    Please note: I'm just starting these lessons, so perhaps the TargetLookAt GameObject has other uses that I don't know about yet.

    Anyway, here's the changes I made to TP_Camera.cs.

    I commented out all lines of code that contained targetLookAt, that's the lower case one, not the uppercase one. targetLookAt is a GameObject, TargetLookAt is a Transform.

    I had to get a reference to the Player GameObject, so I created a variable to hold it:


    private static GameObject player;


    Then, in the method UseExistingOrCreateNewMainCamera()

    I retrieved the Player:


    player = GameObject.Find("Player") as GameObject;


    Finally, I told the camera to look at the Player instead of targetLookAt:



    myCamera.TargetLookAt = player.transform;


    I commented out:


    // myCamera.TargetLookAt = targetLookAt.transform;



    Last edited by TerryNorton; 08-18-2012 at 10:15 PM. Reason: text all dark

  5. #5
    Join Date
    Jan 2011
    Location
    Essex, Vermont
    Posts
    103
    Why is my post all dark?

  6. #6
    Join Date
    Apr 2010
    Location
    Alabama
    Posts
    100
    I believe you can set a GameObject's parent like this...

    Code:
    player = GameObject.Find("Player") as GameObject;
    
    transform.parent = player.transform;
    Now, its been awhile since I messed with the 3rd person controller videos here, but if I'm not mistaken, the code was designed around the targetLookAt being there already, and the code to build one from scratch was built as a placeholder of sorts.

    So, if you can find your player in code, you can parent the targetLookAt to it, but really that just means you're trading one requirement for another. Instead of requiring that your player have a GameObject named "targetLookAt" parented to it, you now require that you have your player named "Player". The difference is, now you have less control of where exactly the camera is facing. If your player transform is located at the player's feet, that's where your camera will look. And there's no telling where the camera might look when animation comes into play.

    I guess it might be a good fail-safe for the chance that you forget to put in a targetLookAt, but I prefer to have my code put the targetLookAt on the world origin. That way, if I forget to put it on my character, it's immediately obvious that something isn't right in the world.

    Hope this helps =)
    Get your facts first, and then you can distort them as much as you please. - Mark Twain

  7. #7
    Join Date
    Jan 2011
    Location
    Essex, Vermont
    Posts
    103
    I like your logic. I wasn't too comfortable bypassing the intended design and functionality this early in the series.

    It was still fun getting some code to work, but I am reverting back to using tatgetLookAt and manually making it a child.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •