[SOLVED] Sprite/Camera shaking when RigidBody 2D component added

Hi all,

I’m new to coding and Unity (it’s been my fourth day of learning, to be precise) and I’m trying to make this simple game where the player can move up and down in Y-axis. The problem is that, when I add the RigidBody 2d component, my player starts shaking and eventually falls off and disappears.

Here’s a video of the “incident”:

Thanks in advance for any suggestions!

And here’s my Player script:

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

public class Player : MonoBehaviour {

            private Vector2 targetPos;
            public float Yincrement;

            public float speed;
            public float maxHeight;
            public float minHeight;


    private void Start()
    {
        targetPos = transform.position;
    }

    private void Update()
            {

                transform.position = Vector2.MoveTowards(transform.position, targetPos, speed * Time.deltaTime);       

                if (Input.GetKeyDown(KeyCode.UpArrow) && transform.position.y < maxHeight) {
                targetPos = new Vector2(transform.position.x, transform.position.y + Yincrement);
               
       
             }  else if (Input.GetKeyDown(KeyCode.DownArrow) && transform.position.y > minHeight) {
              targetPos = new Vector2(transform.position.x, transform.position.y - Yincrement);
             
             }
             }
}

SOLVED by changing body type to kinematic

Glad you were able to solve your problem and thanks for sharing the fix! :slight_smile:

1 Like