Search Unity

[SOLVED] Camera Movement script causes Sprites to appear like it's moving back and forth

Discussion in 'Scripting' started by Deleted User, Sep 20, 2014.

  1. Deleted User

    Deleted User

    Guest

    I'm trying to make the camera follow the player and always be a set distance in front of the player. It works, but for some reason causes the sprite to appear like it's slightly moving back and forth even though it's not.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraTemp : MonoBehaviour
    5. {
    6.         public GameObject player;
    7.         public Vector2 camOffset;
    8.         public float camSpeed;
    9.  
    10.         private float x = 0;
    11.         private float h = 0;
    12.         private float g = 0;
    13.         private LayerMask groundLayer = 1 << 10;
    14.         private LayerMask obstacleLayer = 1 << 11;
    15.  
    16.         void Start ()
    17.         {
    18.                 x = camOffset.x - player.transform.position.x;
    19.         }
    20.  
    21.         void Update ()
    22.         {
    23.                 Camera cam = Camera.main;
    24.                 float camY = camOffset.y;
    25.                 camSpeed = h - g;
    26.                 g = player.transform.position.x;
    27.                 Vector3 moveBy = Vector2.right * camSpeed;
    28.                 Vector3 moveTo = transform.position + moveBy;
    29.  
    30.                 if (moveTo.x > player.transform.position.x) {
    31.                         cam.transform.position = new Vector3 (player.transform.position.x, transform.position.y, -1);
    32.                 } else {
    33.                         cam.transform.position = moveTo;
    34.                 }
    35.         }
    36.         void LateUpdate ()
    37.         {
    38.                 h = player.transform.position.x;
    39.         }
    40. }
     
  2. Deleted User

    Deleted User

    Guest

  3. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    239
    move all of your position updating to LateUpdate
     
  4. Deleted User

    Deleted User

    Guest

    This is what I have now but it didn't fix the problem:

    Code (CSharp):
    1. void Start ()
    2.         {
    3.                 x = camOffset.x - player.transform.position.x;
    4.         }
    5.  
    6.         void Update ()
    7.         {
    8.                 Camera cam = Camera.main;
    9.                 float camY = camOffset.y;
    10.                 camSpeed = Mathf.Abs (h - g);
    11.                 g = player.transform.position.x;
    12.  
    13.                 moveBy = Vector2.right * camSpeed;
    14.                 moveTo = transform.position + moveBy;
    15.  
    16.  
    17.         }
    18.         void LateUpdate ()
    19.         {
    20.                 Camera cam = Camera.main;
    21.  
    22.                 h = player.transform.position.x;
    23.                 if (moveTo.x > player.transform.position.x) {
    24.                         cam.transform.position = new Vector3 (player.transform.position.x, transform.position.y, -1);
    25.                 } else {
    26.                         cam.transform.position = moveTo;
    27.                 }
    28.                 Debug.Log (Mathf.Abs (g - h));
    29.  
    30.         }
     
  5. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    239
    I meant more like this lol:

    Code (CSharp):
    1. void Start ()
    2. {
    3.     x = camOffset.x - player.transform.position.x;
    4. }
    5.  
    6. void LateUpdate ()
    7. {
    8.     Camera cam = Camera.main;
    9.     float camY = camOffset.y;
    10.     camSpeed = Mathf.Abs (h - g);
    11.     g = player.transform.position.x;
    12.  
    13.     moveBy = Vector2.right * camSpeed;
    14.     moveTo = transform.position + moveBy;
    15.    
    16.     h = player.transform.position.x;
    17.     if (moveTo.x > player.transform.position.x) {
    18.             cam.transform.position = new Vector3 (player.transform.position.x, transform.position.y, -1);
    19.     } else {
    20.             cam.transform.position = moveTo;
    21.     }
    22.     Debug.Log (Mathf.Abs (g - h));
    23. }
    24.  
    Hope this helps. Late update runs after update has been run in every script, so if you move an object in Update or FixedUpdate, then LateUpdate will have the latest position of your object.
     
  6. Deleted User

    Deleted User

    Guest

    Thanks it works but what if I start updating my player's position in LateUpdate? It will break again, no?
     
  7. Deleted User

    Deleted User

    Guest

  8. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    239
    It will break yes, because the idea is that you either update the camera position at the same time as updating the player position (which is dependent on the players position being updated first before the camera) or you do it in LateUpdate after update has updated your players position. Hope this helps! :)