Hey guys - when I use the middle mouse button the rotation gets kind of wonky after holding the button down a little too long. Is there a better way to implement the rotate around?
I want to replicate this: Screen capture - cde0185473b7a086391a8b6945fba122 - Gyazo
but this is how it currently works: Screen capture - 81fdf3b1cf94b5c0de6a492d864d6ae0 - Gyazo
void Update()
{
//currentPos = transform.position + offset;
if (Input.GetMouseButtonDown(2))
{
mouseOrigin = Input.mousePosition;
isRotating = true;
}
if (!Input.GetMouseButton(2)) isRotating = false;
if (isRotating)
{
Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin);
transform.RotateAround(PlayerObj.transform.position, Vector3.right, -pos.y * turnSpeed); //changed from PlayerObj.transform.right
transform.RotateAround(PlayerObj.transform.position, Vector3.up, pos.x * turnSpeed);
}
}
==========================================================
for anyone else who wants to reference this, here is a prefect implementation for what I wanted to achieve:
using UnityEngine;
public class CameraOrbit : MonoBehaviour
{
public Transform camTransform;
public Transform pivotTransform;
protected Vector3 localRot;
protected float cameraDist = 10f;
public float MouseSensitivity = 40f;
public float ScrollSensitvity = 2f;
public float orbitSensitivity = 10f;
public float scrollSensitivity = 6f;
public bool pressed = false;
void LateUpdate()
{
if(Input.GetMouseButtonDown(2))
{
pressed = true;
}
if (Input.GetMouseButtonUp(2))
{
pressed = false;
}
if (((Input.GetAxis("Mouse X") != 0) || (Input.GetAxis("Mouse Y") != 0)) && (pressed))
{
localRot.x += Input.GetAxis("Mouse X") * MouseSensitivity;
localRot.y -= Input.GetAxis("Mouse Y") * MouseSensitivity;
//Clamp the y Rotation to horizon and not flipping over at the top
if (localRot.y < 0f)
localRot.y = 0f;
else if (localRot.y > 90f)
localRot.y = 90f;
}
if (Input.GetAxis("Mouse ScrollWheel") != 0f)
{
float ScrollAmount = Input.GetAxis("Mouse ScrollWheel") * ScrollSensitvity;
ScrollAmount *= (cameraDist * .3f);
cameraDist += ScrollAmount * -1f;
cameraDist = Mathf.Clamp(cameraDist, 1.5f, 100f);
}
//Actual Camera Rig Transformations
Quaternion QT = Quaternion.Euler(localRot.y, localRot.x, 0);
pivotTransform.rotation = Quaternion.Lerp(pivotTransform.rotation, QT, Time.deltaTime * orbitSensitivity);
if (camTransform.localPosition.z != cameraDist * -1f)
{
camTransform.localPosition = new Vector3(0f, 0f, Mathf.Lerp(camTransform.localPosition.z, cameraDist * -1f, Time.deltaTime * scrollSensitivity));
}
}
}