Hey everyone, I’m working on a 2d top down shooter, and I’m currently trying to get the camera script to work. Right now, the player looks at the point that the mouse is on top of, and then the camera shifts slightly to that point. However, whenever the player moves, his sprite “jitters” a bit and it looks bad. I have no idea what’s causing it. Any help is appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
Vector3 screenMousePos;
Vector3 worldMousePos;
Player player;
void Start()
{
player = FindObjectOfType<Player>();
}
// Update is called once per frame
void Update()
{
screenMousePos = Input.mousePosition; //this is the position of the mouse on the screen, measured in resolution pixels
worldMousePos = Camera.main.ScreenToWorldPoint(screenMousePos); //this converts the position of the mouse on the screen to an actual world transform position
worldMousePos.z = 0; //make sure the player is only looking in 2d space
//find distance between these two positions and then add that to the player transform position in order to get the camera position
float midPointPosX = player.transform.position.x - worldMousePos.x;
float midPointPosY = player.transform.position.y - worldMousePos.y;
Vector3 cameraPos = transform.position;
cameraPos.x = (player.transform.position.x) - midPointPosX / 10;
cameraPos.y = (player.transform.position.y) - midPointPosY / 10;
transform.position = cameraPos;
}
}