I'm hoping someone can clear this up for me, I've literally spent days on this.
I have a need to focus the camera on the front of an object and then allow the player to orbit the object while holding the mouse button. I've managed to get the camera to focus on the front of the object using this:
if (!arrived)
{
//Centre on our object
var newPos = target.transform.position + currentRotation * (distance * target.transform.forward);
transform.position = Vector3.Lerp(transform.position, newPos, 0.25f);
transform.LookAt(target.position);
return;
}
But then I'm looking for the mouse button being held and attempting to rotate around the target object, keeping a consistent distance from it.
I've tried so many different ways but all of them have issues.
I tried:
transform.RotateAround(target.position, Vector3.down, (movementForce * 100000) * Time.deltaTime);
But which works fine horizontally but the same using Vector.left doesn't seem to work vertically.
I've tried:
transform.LookAt(target.position);
Vector3 rotation = new Vector3(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"), 0);
transform.Translate(rotation * Time.deltaTime * 10);
But that moves the camera further away from the target over time.
And finally I'm not trying this:
//Horizontal
Vector3 relativePos = target.position - transform.position; // Vector from camera to player
Vector3 relativePosRight = Vector3.Cross(relativePos, Vector3.left);
transform.RotateAround(target.position, relativePosRight, 100 * Time.deltaTime * Input.GetAxisRaw("Mouse X"));
//Vertical
Vector3 relativePos = target.position - transform.position; // Vector from camera to player
Vector3 relativePosRight = Vector3.Cross(relativePos,Vector3.up);
transform.RotateAround(target.position, relativePosRight, 100 * Time.deltaTime * Input.GetAxisRaw("Mouse Y"));
Which vertically works fine, but when moving the mouse horizontally, that seems to affect it vertically as well and orbits at a weird angle.
Please forgive any obvious mistakes in the above, I've literally resorted to copying and pasting every solution I find online to get the right thing.
Thanks in advance!