• 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 question was closed Aug 27, 2015 at 11:03 PM by hwale for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by hwale · Aug 28, 2015 at 11:11 AM · c#cameragameobjectblinking

Why are my 2D sprite GameObjects blinking and how do I fix this problem?

Hello. Firstly, I'd like to apologize because my code may be sloppy. I am a beginner. I wanted to have the camera follow the player game object which is a 2d sprite so I found a c# script online to work with. I only wanted the camera to follow the object along the Y-Axis and stay put along the X-Axis so I edited the script a bit. Before I made my changes, the 2d sprite game objects did not blink on play. After I made changes so that the camera did not move along the X-Axis, my 2d sprite game objects started to blink on play. Here is a video: https://youtu.be/0B9kkjXAPDs

Here is my code:

 using UnityEngine;
 using System.Collections;
 
 public class SmoothCamera : MonoBehaviour {
     
     public float dampTime = 0.15f;
     private Vector3 velocity = Vector3.zero;
     public Transform player;
     
     Vector3 currentPos;
     
     private float xCam;
     
     void Start() {
     
     xCam = GetComponent<Camera>().ViewportToWorldPoint(new Vector3(0.5f, 0, 0)).x;
     
     }
     
     void FixedUpdate () 
     {
             Vector3 point = GetComponent<Camera>().WorldToViewportPoint(player.position);
             Vector3 delta = player.position - GetComponent<Camera>().ViewportToWorldPoint(new Vector3(0.5f, 0.5f, point.z));
             Vector3 destination = transform.position + delta;
             currentPos = new Vector3(xCam, transform.position.y, point.z);
             transform.position = Vector3.SmoothDamp(currentPos, destination, ref velocity, dampTime);
     }
 }
Comment
Add comment · Show 1
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 hwale · Aug 27, 2015 at 10:39 PM 0
Share

I fixed it. I made some more changes to the code I found.

 using UnityEngine;
 using System.Collections;
 
 public class SmoothCamera : $$anonymous$$onoBehaviour {
     
     public float dampTime = 0.15f;
     private Vector3 velocity = Vector3.zero;
     public Transform player;
     
     Vector3 currentPos;
     Vector3 destination;
     
     private float xCam, zCam;
     
     void Start() {
     
     xCam = GetComponent<Camera>().ViewportToWorldPoint(new Vector3(0.5f, 0, 0)).x;
     zCam = transform.position.z;
     
     }
     
     void FixedUpdate () 
     {
             destination = new Vector3(xCam, player.position.y, zCam);
             currentPos = new Vector3(xCam, transform.position.y, zCam);
             transform.position = Vector3.SmoothDamp(currentPos, destination, ref velocity, dampTime);
     }
 }

0 Replies

  • Sort: 

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

ARKit: Generate Multiple GameObjects from Script 0 Answers

Move gameobject pivot 6 Answers

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

how are Mathf.SmoothDamp and Mathf.SmoothStep different 1 Answer


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