- Home /
Find Camera atuomatically
I need help with this code:
//var thetarget : Camera.main;
var cam : Camera = GameObject.Find("Main Camera");
function Update()
{ transform.LookAt(transform.position + cam.forward); }
The issue that I am having is that on my spawned enemies the camera is lost if I uncomment the first line and set the camera. but i want it to find the main camera automatically...
i get an error with the above in that : BCE0019: 'forward' is not a member of 'UnityEngine.Camera'.
does this make sense?
Answer by Fattie · Jan 15, 2013 at 05:08 PM
put this line up the top of your file
private var theCam:Camera;
Next look at and click the actual camera in your Hierarchy panel.
You must rename the camera to happyCam
Finally, put this line as the first line in your Awake() routine
theCam = GameObject.Find("happyCam").GetComponent(Camera);
Now you're all set. refer to the camera as "theCam"
did that : new code below
//var thetarget : Transform;
private var theCam:Camera;
theCam = GameObject.Find("$$anonymous$$ain Camera").GetComponent(Camera);
function Update()
{ transform.LookAt(transform.position + theCam.forward); }
but i still get this error : BCE0019: 'forward' is not a member of 'UnityEngine.Camera'.
try transform.forward forward doesn't exist with camera component, only as a transform which forward is a vector3 with 1 unit in the blue axis.
that did it.. the solution was.
{ transform.LookAt(transform.position + theCam.transform.forward; }