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);
}
}
}