Platformer Player Jittering When Moving

I’m making a pretty simple 2d platformer, however whenever my player moves (especially when he jumps) he jitters, and a weird effect happens to my sprite. I’ve changed rigidbody to interpolate and that didn’t fix the problem. Any help would be appreciated, thanks!

Player Movement Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour
{
    public Rigidbody2D rbN;
    private Animator anim;
  private SpriteRenderer sprite;
  private BoxCollider2D coll;
    [SerializeField] private LayerMask jumpableGround;
    [SerializeField] private float moveSpeed = 7f;
    [SerializeField] private float jumpHeight = 1000;
    [SerializeField] private Transform player;
    private enum MovementState { idle , running, jumping, falling};
    private MovementState state;
    private float Dirx;
    private double boundryL=7.5-8.235174E-08;

    // Start is called before the first frame update
    private void Start()
    {
        rbN = GetComponent<Rigidbody2D>();
        coll = GetComponent<BoxCollider2D>();
       sprite = GetComponent<SpriteRenderer>();
        anim = GetComponent<Animator>();
        Debug.Log(MovementState.idle);
    }

    // Update is called once per frame
    private void Update()
    {
        jump();
        movement();
        updateAnimations();
        if(player.position.x<=boundryL){
            Debug.Log("homie");
        }
      

    }

    private void movement()
    {
        Dirx = Input.GetAxis("Horizontal");
        if(player.position.x!=boundryL){
        rbN.velocity = new Vector2(Dirx * moveSpeed, rbN.velocity.y);
        }
        else if(player.position.x==boundryL){
            Debug.Log("Hit Boundry");
        }
    }

    private void jump()
    {
        if (Input.GetButtonDown("Jump")&&IsGrounded())
        {
            rbN.velocity = new Vector2(rbN.velocity.x,jumpHeight);
        }
    }
    private void updateAnimations()
    {
        MovementState state=MovementState.idle;
        if (Dirx > 0f)
        {
            sprite.flipX = false;
            if(IsGrounded())
            {
                state = MovementState.running;
                //Debug.Log("Right");
            }
        }
        else if ((Dirx < 0f)&&IsGrounded())
        {
            sprite.flipX = true;
            if(IsGrounded())
            {
            state = MovementState.running;
            //Debug.Log("Left");
            }
        }
        else
        {
            state = MovementState.idle;
        }
      
        if (rbN.velocity.y > 0.1f)
        {
            state = MovementState.jumping;
            //Debug.Log("jump");
            if(Dirx>0f){
                sprite.flipX = false;
            }
            if(Dirx<0f){
                sprite.flipX = true;
            }
        }
        else if(rbN.velocity.y <-0.1f){
            state = MovementState.falling;
            //Debug.Log("fall");
            if(Dirx>0f){
                sprite.flipX = false;
            }
            if(Dirx<0f){
                sprite.flipX = true;
            }
        }
       anim.SetInteger("state", (int)state);
    }
    private bool IsGrounded(){
        //(first 2 make hitbox,then rotation, last 2 move teh box a tiny bit below the players hitbox)
        return Physics2D.BoxCast(coll.bounds.center,coll.bounds.size,0f,Vector2.down, .1f, jumpableGround);
    }  
}

Camera Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class cameraController : MonoBehaviour
{
    [SerializeField] private Transform player;
    [SerializeField] private float timeOfset;
    [SerializeField] private Vector2 posOfset;
    private Vector3 velocity;
  
    // Update is called once per frame
   private void Update()
    {
        if(player.position.x>=-2.235174E-08){
            //camera current position
            Vector3 StartPos = transform.position;

            //player current position
            Vector3 EndPos = player.transform.position;
            EndPos.x +=posOfset.x;
            EndPos.y +=posOfset.y;
            EndPos.z=-10;
          
        transform.position = Vector3.Lerp(StartPos,EndPos,timeOfset*Time.deltaTime);
      
        }
    }
}

You didn’t demonstrate what you mean by “jitter” here but instead dumped a huge block of code as plain text and expected someone to figure out the issue without knowing what the issue is.

Please edit your post above to use code-tags and never use plain text for code, especially when posting so much.

  • What debugging have you done already to narrow the problem down?
  • I see you’ve littered the code with “Debug.Log” (good!) but you didn’t let us know the results of that i.e. what works, what doesn’t. We cannot run the code in our heads easily!
  • Check it’s not your follow camera?
  • Check it’s not how you’re moving your player?
  • You’re modifying physics per-frame but maybe you don’t understand that physics doesn’t run per-frame by default.
  • Why would interpolation fix it if you don’t know what the problem is?
  • Doing this is troubling: “player.position.x>=-2.235174E-08”. Do you know that this value is essentially zero?

The quantity -2.235174E-08 is virtually zero. Why not just say if (player.position.x >= 0) ??

Don’t use Magic Numbers:

https://discussions.unity.com/t/846897/4

Instead, know where the number comes from and compute it from the correct inputs.

If you’re just looking for a 2D platform player controller, there’s plenty of functional ones out there to start from.

Here is one of mine:

https://www.youtube.com/watch?v=1Xs5ncBgf1M

proximity_buttons is presently hosted at these locations:

https://bitbucket.org/kurtdekker/proximity_buttons

https://github.com/kurtdekker/proximity_buttons

https://gitlab.com/kurtdekker/proximity_buttons

https://sourceforge.net/projects/proximity-buttons/

If you prefer to debug what you have going above, here’s how:

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3