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 YagoTheFrood · Jan 13, 2014 at 03:26 PM · cameraplayer

Parenting a camera to different objects with input?

Let's say I have two characters on a scene, and I can switch control between them. Assume that when I press Q, I control Char1, and when I press E, I control Char2. How can I get the camera to check which character is being controlled, so that it pans to and follows that character?

If I was making this game without Unity, I would make an array of playable characters and each character would have a "isBeingControlled" bool, and the camera would cycle through the array of playable characters, checking each one's bool. But in Unity, I don't know how to access another object from a script attached to the camera.

Comment
Add comment · Show 3
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 RChrispy · Jan 13, 2014 at 03:47 PM 0
Share

If your camera has a behaviour script to follow a gameobject you can create a List or Array and then switch between them let the camera behaviour do its job automaticaly.

That way you don't even need to check the bool isBeingControlled (well only if you don't want that others move as well)

ps: to access another object simply call this in start():

anObject = Gameobject.Find("OBJECTNA$$anonymous$$E").GetComponent < TheScriptNA$$anonymous$$E >();

You can also find more of a kind and store them in arrays.

avatar image YagoTheFrood RChrispy · Jan 13, 2014 at 04:42 PM 0
Share

Thank you, that function will also be useful. But I can only choose one correct answer... which is weird, actually?

avatar image RChrispy · Jan 16, 2014 at 09:36 AM 0
Share

No problem m8 I just converted my statement to a comment so others can see it too :)

1 Reply

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

Answer by JoaoOliveira · Jan 13, 2014 at 03:47 PM

Following your idea: You create a "MyCharacterScript" with a public property "isBeingControlled" and attach it to both characters. On the script that is attached to your camera you add a public MyCharacterScript[] field. From this point on you can edit this array in the editor. In your case, you want to set its size to 2. Then you just need to drag and drop your characters to the array positions and you will have access to them.

Keep in mind that other solutions might be reasonable. For instance, having 2 cameras and having a script with a reference to both of them. Depending on the pressed key, you activate and deactivate the correct cameras.

Comment
Add comment · 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

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

20 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Camera watching inbetween the player and cursor 1 Answer

Locking world rotation of child 1 Answer

3d Sound when the player near and not the camera? 1 Answer

Walk animation for camera isn't playing 1 Answer

How you change another script value smoothly on trigger 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