Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Skip.8 · Aug 19, 2015 at 08:16 AM · c#camerarendering

Camera stops rendering objects whenever this smooth follow 2D Script is applied

Hi guys, im rather new to unity so maybe there is something to the camera behaviour that I just dont understand yet. However, I have written this smoothFollow2D script in C# to make my Camera follow a square player object when its moving:

     public Transform target;
     public float smoothTime = 0.3f;
     private Transform thisTransform;
     private Vector2 velocity;
 
     // Use this for initialization
     void Start () 
     {
         thisTransform = GetComponent<Transform>();
     }
     
     // Update is called once per frame
     void Update () 
     {
         Vector2 smoothMove = new Vector2(Mathf.SmoothDamp(thisTransform.position.x,
         target.position.x, ref velocity.x, smoothTime),
         Mathf.SmoothDamp(thisTransform.position.y, target.position.y, ref
         velocity.y, smoothTime));
         thisTransform.position = smoothMove;
 
     }

This is working very well so far, but when i apply this script to my camera and start the game it simply stops rendering any objects but the background(Skybox). I can see the camera following the player object in the scene view but i see nothing in the game view. I also had this problem before without any sripts and just with 3 objects in my scene, but I cant tell what I did to solve it.

EDIT:

I figured out that the problem has something to do with the initialisation of the targeting Transform, if I leave it empty or just clear the start section there is no rendering issue.

Comment
Add comment · Show 4
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 Nick4 · Aug 19, 2015 at 08:47 AM 0
Share

Why would you create a variable just for your transform component? Just use "transform" ins$$anonymous$$d.

avatar image Skip.8 · Aug 19, 2015 at 10:16 AM 0
Share

I thought it just works that way because you have to acces transform by GetComponent(); So you say I could use it like this.transform?

avatar image Priyanshu · Aug 19, 2015 at 10:31 AM 1
Share

Storing transform in a reference variable saves processing power.

Your script is correct. By assigning vector2 to your camera's position. You set position.z = 0. Does this cause problem?

avatar image Skip.8 · Aug 19, 2015 at 11:03 AM 0
Share

That was my tought too. Thanks, and yes my problem was caused by the z positioning. I've already found that out by myself, but thanks anyway.

2 Replies

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

Answer by Bunny83 · Aug 19, 2015 at 10:30 AM

Most likely because you set the z position of your camera implicitly to "0" with this script. Now it depends on what type of camera you are using and on it's parameters.

  • If you use a Perspective camera you have no choice. You have to place the camera at some negative z value in order to actually see something. Otherwise your 2d stuff (which usually is at z == 0) will be behind the cameras near clipping plane.

  • If you use an orthographic camera the z position doesn't really matter, but you have to use a negative near clipping plane to actually "see" things "behind" the camera position.

Note: A perspective camera can't have a negative near clipping plane it always has to be greater than 0.

An orthographic camera could have the "near" clipping plane at -inf and the far clipping plane at inf which would make the z position completely obsolete.

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 Skip.8 · Aug 19, 2015 at 10:57 AM

I figured it out, it has to be a Vector3 with a fixed value for z Axix, otherwise it will end up in placing the camera behind objects (at z = 0) since they are at z = -10 somehow:

 Vector3 smoothMove = new Vector3(Mathf.SmoothDamp(thisTransform.position.x,
 target.position.x, ref velocity.x, smoothTime),Mathf.SmoothDamp(
 thisTransform.position.y, target.position.y, ref velocity.y, smoothTime),-10f);



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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Trying to reset image settings on main camera smoothly 1 Answer

No Camera Rendering when switching between cameras developing for Android 0 Answers

Checking if GameObject is rendered by specific camera 1 Answer

Rendertype for Unity's non-legacy Image UI 0 Answers

Two Cameras, Want One to Send Data of Both 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