Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Animation Flickering when updating Tranform

Discussion in '2D' started by StevenDStanton, Nov 7, 2020.

  1. StevenDStanton

    StevenDStanton

    Joined:
    Dec 24, 2014
    Posts:
    2
    I have a 2d animation which is 3 sprites long. It works fine on its own.

    As I have moved forward, I added code to update the transform and now the animation is flickering.

    This code is attached to the object.

    I am running this on Windows 10
    Unity version 2019.4.2f1 Personal

    The issue happens both in game preview and in the Scene manager.

    Code (CSharp):
    1.  void Update()
    2.     {
    3.        float mousePos = Input.mousePosition.x / Screen.width * screenWidthInUnits;
    4.        Vector2 paddlePos = new Vector2(transform.position.x, transform.position.y);
    5.        transform.position = paddlePos;
    6.      
    7.     }
    8. }
    If I comment out the last line like this the flickering goes away

    Code (CSharp):
    1. void Update()
    2.     {
    3.        float mousePos = Input.mousePosition.x / Screen.width * screenWidthInUnits;
    4.        Vector2 paddlePos = new Vector2(transform.position.x, transform.position.y);
    5.        //transform.position = paddlePos;
    6.      
    7.     }
    I have attempted to google this with no luck.

    If anyone wishes to try to run the code themselves I have it listed here on github as I am just using this as a way to learn Unity

    https://github.com/StevenDStanton/Block-Breaker
     
  2. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    try doing
    Code (CSharp):
    1.   Vector3 paddlePos = new Vector3(transform.position.x, transform.position.y,transform.position.z);
    2.        transform.position = paddlePos;
     
    StevenDStanton likes this.
  3. StevenDStanton

    StevenDStanton

    Joined:
    Dec 24, 2014
    Posts:
    2
    Thank you,

    Converting it to the Vector3 solves the issue. I appreciate the help!
     
    raarc likes this.