Search Unity

2D Camera Follow Jitter

Discussion in '2D' started by BossusDev, Sep 23, 2018.

  1. BossusDev

    BossusDev

    Joined:
    May 19, 2018
    Posts:
    2
    Hello, I need assistance with my Unity Project. Normally the jittery camera comes from when the camera tries to update before the player is finished moving, but for some reason in this case the character is the one jittering on their own. I'm pretty stumped as why the character is jittering. They physics code is based off of the 2D custom gravity course.

    PhysicsObject2D:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PhysicsObject2D : MonoBehaviour
    6. {
    7.     //Editable Variables
    8.     public float gravConstant = 1f;                   //Modification to Physics Gravity
    9.  
    10.     //Class Variables
    11.     private Rigidbody2D _rb2d;                        //Reference to RigidBody2D
    12.     private Vector2 velocity;                         //Velocity of the object
    13.  
    14.     private void OnEnable()
    15.     {
    16.         _rb2d = GetComponent<Rigidbody2D>();
    17.     }
    18.  
    19.     private void FixedUpdate()
    20.     {
    21.         //Increase downward velocity while in the air
    22.         velocity += gravConstant * Physics2D.gravity * Time.deltaTime;
    23.  
    24.  
    25.         //Move object
    26.         Vector2 move = Vector2.up * velocity.y * Time.deltaTime;
    27.         _rb2d.position += move;
    28.     }
    29. }
    CameraFollow:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CameraFollow : MonoBehaviour
    4. {
    5.     //Public Variables
    6.     public Transform target;                //Camera Target
    7.     public float smoothSpeed = 0.125f;      //Camera lag speed
    8.     public Vector3 offset;                  //Offset for the camera position
    9.  
    10.     private void LateUpdate()
    11.     {
    12.         //Get Desired camera position - Player's position
    13.         Vector3 desiredPosition = target.position + offset;
    14.         //Smooth the camera's movement using linear interpolation
    15.         Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
    16.         //Apply the lerped position
    17.         transform.position = smoothedPosition;
    18.     }
    19. }
    20.  
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    Why aren't you using Cinemachine for the camera? It's far easier than rolling your own camera script.

    Also, it might be better to use _rb2d.MovePosition().
     
  3. BossusDev

    BossusDev

    Joined:
    May 19, 2018
    Posts:
    2
    I'm still new to Cinemachine, so I thought I could explain my situation better with the script I was originally using.

    I'll give MovePosition a shot and see what happens.