0
votes

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!

1

1 Answers

0
votes

What does your scene hierarchy look like? Are you able to make the camera a child of the object you want to orbit, have the camera always looking at the object and then rotate the object with the mouse?

Similar to this answer.

So something like this:

// Move the camera to the correct position/distance from the target.
Camera.main.transform.position = target.position + new Vector3(0,1,0); // 1m above the target.
Camera.main.transform.LookAt(target.position);
Camera.main.transform.SetParent(target);

// Rotate the target with the mouse input.
Vector3 rotation = new Vector3(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"), 0);
target.Translate(rotation * Time.deltaTime * 10);

It might not be exactly what you might want but it visually looks like it is orbiting the object.

Hope that helps!