Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
avatar image
0
Question by mariannabell · Feb 15, 2021 at 04:28 PM · newbiecamera followtroubleshootingcamera.main

Camera follow

Hi! It's my first time making a CameraFollow script. It's just a simple one. I used this code:

public class CameraFollow : MonoBehaviour { public GameObject player;

 private void Update()
 {
     Vector2 playerPosition = new Vector2(player.transform.position.x, player.transform.position.y);

     //follow player
     transform.position = playerPosition;
 }

}

But when I enable the script and play the game, I no longer see the game, just a solid color. And if I disable the script, it goes back to normal and I see the game again. I don't get it. I didn't change anything else and it did the same thing on another tutorial project I was working on. Does anyone know why this happens?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by logicandchaos · Feb 15, 2021 at 04:40 PM

It's because you are setting your camera to the exact position of your character. Your camera is probably inside your player, this is more how to do 1st person camera. If you want 3rd person or 2d sidescroll you need to position your camera away from player but pointing at them. There is look at for just this purpose. Alos moving your camera in update will look terrible! You need to move your camera in LateUpdate().

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 DJcrafter5606 · Feb 15, 2021 at 04:56 PM 0
Share

Yes, LateUpdate() is better, to make the camera keep a distance you do a public Vector3(or Vector2 depending or 3d or 2d games) for example call it CameraDistance and now, you need to create a transform called something like player and add your player in the inspector. now you do: transform.postion = player.position - CameraDistance (I think is like this, if not please tell me every mistake i will apreciate it

avatar image
1

Answer by DJcrafter5606 · Feb 15, 2021 at 04:42 PM

Maybe you aren't keeping a distance between the player and 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
1

Answer by mariannabell · Feb 15, 2021 at 04:50 PM

Thank you both so much!! I really appreciate it. That was the exact problem. I had to move the camera back on the z axis so that it wasn't on top of the player. For any newbies like me, if you ever get this problem, this is the corrected code I used:

{ public GameObject player;

 private void LateUpdate()
 {
     Vector3 playerPosition = new Vector3(player.transform.position.x, player.transform.position.y, -10);

     //follow player
     transform.position = playerPosition;
 }

}

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

117 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 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

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

Camera Problem 0 Answers

[Question] Unity 2D Android game : Playspace and objects flickering and disappearing from game view after sometime of playing. 1 Answer

Camera Follow (Not Exact) 1 Answer

How to make a stationary camera into a moving camera that follows the player when entering a new scene?,How do i go from a still camera to a camera that moves when my player moves in another scene? 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