While smoothly moving camera towards player, player is shaking. I tried many solutions some of them helped a little bit and some none.
What I tried:
– changing Player Rigibody2D Interpolate to Interpolate
– changing Player movement to FixedUpdate
– changing Camera movement to FixedUpdate and LateUpdate
– adding and removing Time.deltatime to Camera movement
The code that I currently have that works the best:
Player movement with Rigibody2D (with Interpolate) in FixedUpdate:
void Movement() { float velX = Input.GetAxisRaw(“Horizontal”); float velY = Input.GetAxisRaw(“Vertical”); if (velX != 0) sprite.flipX = velX > 0 ? false : true; rb.MovePosition((Vector2)transform.position + new Vector2(velX, velY) * speed * Time.deltaTime); }
Camera movement with Pixel Perfect Camera in LateUpdate:
void Camera() { Vector3 pos = new Vector2(Mathf.Clamp(-sticToPosition.x + player.transform.position.x, -sizeOfRoom.x, sizeOfRoom.x), Mathf.Clamp(-sticToPosition.y + player.transform.position.y, -sizeOfRoom.y, sizeOfRoom.y)) + (Vector2)sticToPosition; cam.transform.position = Vector3.Lerp(cam.transform.position, new Vector3(0, 0, -10) + pos, camSpeed * Time.deltaTime); }
Thanks for the answears!
submitted by /u/ErdenArt
[link] [comments]
Unity2D – Develop 2D games using Unity