I’ve looked through all the similar questions and I could not find a solution for this. Basically, I wanted to write a script to click and drag the camera around the map that I’ve created. The camera moves inverse to the mouse drag to mimic the map moving when dragging the mouse button. For some reason every time I start to drag the mouse, the camera flickers between 2 points, roughly 30 times a second.
Any help to fix the issue would be greatly appreciated!
Below is the method which is in the Update() function that is supposed to move the camera around. The script is attached to the camera and there is no other camera script in any of my objects.
void MoveCameraMouse()
{
//if left mouse button is clicked, grab starting mouse position
if (Input.GetMouseButtonDown(0))
{
cameraPos = this.transform.position;
RaycastHit hit;
Ray ray = UnityEngine.Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
if (hit.collider != null)
{
hitPointOrigin = hit.point;
Debug.Log("starting point" + hit.point);
}
}
}
//if mouse button is held down but not clicked during this frame
if (Input.GetMouseButton(0))
{
RaycastHit hit;
Ray ray = UnityEngine.Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
if (hit.collider != null)
{
hitPointEnd = hit.point;
Debug.Log(hit.point);
}
}
Vector3 move = new Vector3(cameraPos.x - (hitPointEnd.x - hitPointOrigin.x), cameraPos.y, cameraPos.z - (hitPointEnd.z - hitPointOrigin.z));
transform.position = move;
}
}