Need help with a 3rd person Camera
I'm trying to recreate TP_Camera from https://www.3dbuzz.com/training/view/3rd-person-character-system/simple-character-system/14-tpcamera-sec-func-implementation
This is the script as written in the Tutorial.
public static void UseExisitingOrCreateNewMainCamera()
{
GameObject tempCamera;
GameObject targetLookAt;
TP_Camera myCamera;
if (Camera.main != null)
{
tempCamera = Camera.mainCamera.gameObject;
}
else
{
tempCamera = new GameObject("MainCamera");
tempCamera.AddComponent("Camera");
tempCamera.tag = "MainCamera";
}
tempCamera.AddComponent("TP_Camera");
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;
}
myCamera.TargetLookAt = targetLookAt.transform;
}
This is the script after automatically converting it to the new API.
public static void UseExisitingOrCreateNewMainCamera()
{
GameObject tempCamera;
GameObject targetLookAt;
TP_Camera myCamera;
if (Camera.main != null)
{
tempCamera = Camera.main.gameObject;
}
else
{
tempCamera = new GameObject("MainCamera");
tempCamera.AddComponent<Camera>();
tempCamera.tag = "MainCamera";
}
tempCamera.AddComponent<TP_Camera>();
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;
}
myCamera.TargetLookAt = targetLookAt.transform;
}
This method no longer works. It does not find a camera to tag as "MainCamera", nor does it create a camera to tag. It neither finds, nor creates, targetLookAt When I start the scene with a gameobject called targetLookAt it does not find the gameobject and is unable to be instanced by dragging it to the TP_Camera in the inspector without it crashing.
UseExisitingOrCreateNewMainCamera() is also called in TP_Controller and is vital to the controller working. As is, the scene fails to initiate after clicking play. It just sits and tries to start indefinitely.
The scene will only play if I comment out UseExisitingOrCreateNewMainCamera() in TP_Camera and removing it from the awake method of TP_Controller.
Well, what I really want to be able to do is to attach my TP_Camera script to a gameobject and start the scene without a camera in it. The script should look for a camera, not find one, then create a camera named and tagged "MainCamera". I also want it to do the same thing with the object "targetLookAt". As is, when I attach TP_Camera to a capsule with a character controller and hit play in a scene with no camera it just plays the scene without a camera in it. The Game tab just says "Display 1 No Camera Rendering". I'm using the latest version of unity. Sorry if I don't quite understand, I'm new.
Here are the scripts that I am using, applied to a capsule with a character controller:
https://drive.google.com/drive/folders/0Bzywz7Vlm7NlRktndl90NG5FLUU?usp=sharing
Thank you for your time.