[FIXED]Best way to move a RigidBody2D and a camera Script to follow him?

Hi guys, i have to post a new thread cause i just cant fix this problem.

So the thing is, i am bulding a 2D platformer game, like Geometry dash, where you can only control the Jump. So i have created a script for moving the player non stop on the X and adding a velocity to rigidbody2d on the Y when the player taps the screen. Its all working fine but here is the problem.

When i use a camera that follows him just on the X axis, its fine, everything works as it should. Now when i use the same camera to follow him on the Y it works too, but it moves with him everytime i jump, and that is just weird, it makes you dizzy from looking at it.

Now i have used a bunch of different scripts, one with Smoothing and one with deadzones and the last one is great, when you hit a certain height it justs moves the camera to follow you on Y and when you go down it reverts to normal, so the camera doesnt jump everytime you jump so to speak.

Now the issue is when i use any camera that has Smoothing or Deadzones or any camera that doesnt follow the exact movement of the player, i get lags on my movement. Its like he is teleporting back and forth a little, it looks real bad, and i tried everything.

Using FixedUpdate, LateUpdate, Smooth time for Delta time etc

I even tried animating the camera movement, i even buyed the Camera Path hoping it would fix it, but no, its still the same. Any Damping or Smoothing or anything when the camera is not the same speed as the player he starts lagging.

Well thats the best explanation i can give you without a video.

So can it be that my movement script is wrong? This is the part of the script for moving the player on the X.

            Vector3 temp = transform.position;
            temp.x += forwardSpeed * Time.smoothDeltaTime;
            transform.position = temp;

I just cant find a fix for this lagging. So can you guys tell me, if there is a better way to move the player, and a way to stop the lagging.

Keep in mind that i am new to programming and Unity. Just over a month and a half has passed since i started and this is a real problem and a first one i can not fix.

Thanks guys in advance! :slight_smile:

EDIT: Fixed it. Using AddForce to move player, calling it in FixedUpdate, turning on Interpolate and using a standard assets camera did the trick. Jittering is not the issue anymore, everything is working as it should! :slight_smile:

With an infinite runner it might be easier to keep the player still on the x axis (let them move on the y for jumping) & scroll the background & the platforms/obstacles etc.

If not check this site, I use it to get a start with my cameras.

1 Like

Hmmm, never thought of that, to move everything and keep the player in the same spot. Interesting. Any way you can quickly explain how that would work, and its pros and cons?

Thanks for the link, but i managed to half fix it. Setting the RigidBody of my Player to Interpolate almost did the trick. now there is no more jittering except a little one that occurs every couple of seconds, but that one is barely noticable so its ok.

Now i am going to re learn everything about the camera and create a new script to work for it. Any links on that, maybe you will share something i did not find and read?

Just the issue now is that my player moves back and forward, like he slows down a bit and then accelerates but never out of bounds of the camera, movement is really minor but noticable so i think that issue is related to the camera it self hence the relearning and rewriting

what’s the script for your camera?

This is the script. I found it on the internet, pieced a couple of stuff together and this is what i got. Now i think its not working as it should, because it wont follow my player even though tags are set correctly. Anyway i will redo everything, start the whole project again. Refresh is what i need, too much clutter has accumulated.

using System;
using UnityEngine;


namespace UnityStandardAssets._2D
{
    public class CameraFollow : MonoBehaviour
    {
        public float xMargin = 1f; // Distance in the x axis the player can move before the camera follows.
        public float yMargin = 1f; // Distance in the y axis the player can move before the camera follows.
        public float xSmooth = 8f; // How smoothly the camera catches up with it's target movement in the x axis.
        public float ySmooth = 8f; // How smoothly the camera catches up with it's target movement in the y axis.
        public Vector2 maxXAndY; // The maximum x and y coordinates the camera can have.
        public Vector2 minXAndY; // The minimum x and y coordinates the camera can have.

        private Transform m_Player; // Reference to the player's transform.


        private void Awake()
        {
            // Setting up the reference.
            m_Player = GameObject.FindGameObjectWithTag("Player").transform;
        }


        private bool CheckXMargin()
        {
            // Returns true if the distance between the camera and the player in the x axis is greater than the x margin.
            return Mathf.Abs(transform.position.x - m_Player.position.x) > xMargin;
        }


        private bool CheckYMargin()
        {
            // Returns true if the distance between the camera and the player in the y axis is greater than the y margin.
            return Mathf.Abs(transform.position.y - m_Player.position.y) > yMargin;
        }


        private void Update () {

        TrackPlayer();
        }

    private void TrackPlayer()
        {
            // By default the target x and y coordinates of the camera are it's current x and y coordinates.
            float targetX = transform.position.x;
            float targetY = transform.position.y;

            // If the player has moved beyond the x margin...
            if (CheckXMargin())
            {
                // ... the target x coordinate should be a Lerp between the camera's current x position and the player's current x position.
                targetX = Mathf.Lerp(transform.position.x, m_Player.position.x, xSmooth*Time.deltaTime);
            }

            // If the player has moved beyond the y margin...
            if (CheckYMargin())
            {
                // ... the target y coordinate should be a Lerp between the camera's current y position and the player's current y position.
                targetY = Mathf.Lerp(transform.position.y, m_Player.position.y, ySmooth*Time.deltaTime);
            }
            // The target x and y coordinates should not be larger than the maximum or smaller than the minimum.
            targetX = Mathf.Clamp(targetX, minXAndY.x, maxXAndY.x);
            targetY = Mathf.Clamp(targetY, minXAndY.y, maxXAndY.y);

            // Set the camera's position to the target position with the same z component.
            transform.position = new Vector3(targetX, targetY, transform.position.z);
        }
    }
}
1 Like

Check the link I gave you as I’ve never had an issue with jittery or laggy cameras when I’ve used them.

Here’s a link to a unity tut that may give ideas for endless runners

https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/infinite-runner

1 Like

I fixed it! Using AddForce to move my player, calling it in FixedUpdate and turning Interpolate did the trick! Camera i was using is from standard assets and it works like a charm. Thank you all for your inputs, the links were really usefull! :slight_smile:

Hi,hey man can you please help me out here. I am stuck on the same problem you were. Can you please explain what you did? and what camera follow script you are using? did you use the standard one in unity? smooth camera follow 2d? I’ve been stuck on this forever … more than 5 months… please help me out! thanks in advance!