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 drunkenoodle · Jan 25, 2012 at 03:11 PM · cameratransformlookatinstantiation

Attaching camera to instantiated object

Hello everyone! I'm absolutely stumped. I spawn a player into the game along with a camera and a few blocks, but I can't fathom at all how to get that camera to follow the character once it's been spawned. I attempt to attach a camera to the spawn using the AddComponent method, which loads the camera into the scene absolutely fine. It's just the tracking I can't figure out. Any insight would be most appreciated!

 var playerPrefab:transform;
 var target:Transform;
 var cam:Camera;

 function Start() {
 target = Instantiate (playerPrefab, transform.position, Quaternion.identity);
 cam = gameObject.AddComponent(Camera);
 }

 function Update() {
 // Presumably camera controls go in here such as lookat(target)?
 }

Thanks very much!

Comment
Add comment · Show 2
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 syclamoth · Jan 25, 2012 at 03:18 PM 0
Share

The simplest $$anonymous$$ouseOrbit can be found in the standard assets- have a look at that. You're on the right track, but you should have a look at the way things work in the example projects as a guide to how you put your own stuff together.

avatar image drunkenoodle · Jan 25, 2012 at 03:26 PM 0
Share

Thanks for pointing me in the right direction, I'll get researching. Cheers!

1 Reply

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

Answer by Owen-Reynolds · Jan 25, 2012 at 03:28 PM

The code in most camera scripts has its own target variable, and is set to track that transform. All you have to do is take a pre-existing camera (you could create one in the code, but why bother?) and change that target var:

 // your spawn line:
 target = Instantiate(plyrPF, transform.position, Quaternion.identity) as Transform;
 // tell camera to track it:
 cam.GetComponenent<camScript>().target = target;
Comment
Add comment · Show 1 · 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 drunkenoodle · Jan 25, 2012 at 03:43 PM 0
Share

Thank's a lot for this, worked a treat!

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

6 People are following this question.

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

Related Questions

make player move in camera direction 1 Answer

Smoothly Move camera while looking at object 0 Answers

LookAt in opposite direction? 2 Answers

LookAt only when camera position changes 2 Answers

An alternative to RotateAround for collisions 0 Answers


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