Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by rainbowparadoxes · Mar 23, 2016 at 06:37 PM · cameraprefabprefabsnewbiecamera follow

How to get the camera to follow a prefab?

Hiya Unity CommUnity!

I'm writing a script to spawn my player prefab whenever I load a new scene. (I may or may not be doing spawning entirely wrong. Would appreciate input if you have any.)

In any case, I'm trying to get the Main Camera to search for a Player prefab through code and follow that, but what I'm writing is not working and I don't quite have enough experience yet to figure out why. Here is the relevant code:

 var player : Transform;
 
 function Update(){
     if (player == null){
         var player : GameObject = GameObject.FindGameObjectsWithTag("Player")[0];
     }
     Debug.Log(player);
 }

The Debug.Log prints out 'null' once (which is odd; I would expect it to print out over and over again, being in the Update function), so I'm sure that everything is being called correctly. Do you know what's wrong with this code?

Any feedback is appreciated. Thanks a lot!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by idontwannabeauser · Oct 26, 2016 at 06:41 AM

@rainbowparadoxes: You might like check this out: https://unity3d.com/es/learn/tutorials/projects/2d-ufo-tutorial/following-player-camera One important thing they said here is that you should use LateUpdate() instead of Update() for camera following behavior since it is called after all other objects are updated so you ensure the right update for the camera.

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

Answer by Brocccoli · Mar 23, 2016 at 06:47 PM

You are only seeing Debug.Log once because Unity nests the same Logs by default. You can expand the single log line to see every line.

For the problem you're having however, I believe there may be a better way to go about such a problem. Below is the code I am currently using on my camera object in my current prototype.

 public Transform target;
 public int distance = -10;
 
 void Update () 
 {
   this.transform.position = new Vector3 (target.transform.position.x, target.transform.position.y,    distance);
 }

In the editor I can set my PlayerPrefab's transform as the target and have it follow that. This reduces some load at Startup. (Be aware, the above is C# and will differ if your implementing in JS or Boo)

If you're set on your previous way, than I would suggest double checking that your Player prefab actually has the tag of "Player". Make sure it's exact as I'm pretty sure the method is case sensitive.

Also, it may be a little cleaner (if you're only looking for one player) to use

GameObject.FindWithTag("Player");

which will return a single GameObject and remove the unnecessary indexing.

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 rainbowparadoxes · Mar 23, 2016 at 10:10 PM 0
Share

I was only showing you the part of the code where I defined the 'player' object. $$anonymous$$y camera follow code looks fine, and is very similar to yours. (It's in the LateUpdate function, though.) I have dragged my prefab into the editor, but it didn't work (it worked well with normal gameobjects). I want this code to work in all of the levels, not just one, so I can't just drag one instance of a prefab into the editor- which is why I wrote my code.

Thanks for showing me the FindWithTag function! I'd been using a workaround because I didn't know a singular version existed. XD

The prefab's tag is definitely 'Player', capitalized (i'm using the default tag).

Thanks for all your help! $$anonymous$$y code is a little prettier now. If I find out my issue, I'll post it to the board.

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

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

Related Questions

So I'm attempting to make a camera at a fixed angle with no rotation,I'm trying to set my camera on a follow path while keeping a fixed angle. 1 Answer

my camera wont stay with my player after assigning the player as a target 0 Answers

How to make a stationary camera into a moving camera that follows the player when entering a new scene? 0 Answers

Screen Space - Camera : Not displaying button size correctly during play 0 Answers

int variable seem to have multiple value at the same time 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