Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by trinitysj96 · Jan 15, 2013 at 05:05 PM · cameracamera.main

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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"

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image trinitysj96 · Jan 15, 2013 at 11:01 PM 0
Share

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'.

avatar image cdrandin · Jan 15, 2013 at 11:16 PM 1
Share

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.

avatar image trinitysj96 · Jan 15, 2013 at 11:26 PM 0
Share

that did it.. the solution was.

{ transform.LookAt(transform.position + theCam.transform.forward; }

avatar image trinitysj96 · Jan 16, 2013 at 04:42 PM 0
Share

thanks again it worked great.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I can not find Cinemachine in package manager. There is no option of 'All Assets in package manager.' 1 Answer

How to make camera position relative to a specific target. 1 Answer

Switching from a cinematic cycle to the player camera 0 Answers

NullReferenceException in game build 1 Answer

I'm having some trouble understanding how to switch cameras 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges