Dual-Joystick Controlled 3rd-Person Orbit Camera

I am brand-new to Unity. :slight_smile:

I am trying to make a dual-joystick controlled 3rd-person “orbit” camera (arcball camera? / trackball camera?). The camera follows the player and can rotate around the player in a sphere or bubble centered on the player. This is the type of camera used in games like Dark Souls and Naughty Bear. The left joystick moves and rotates the player, and the right joystick rotates the camera.

Dark Souls’ camera is the best third-person camera I have ever used and is the ideal I eventually want: it is “intelligent”, automatically reorients itself after a few seconds, and never gets obscured by walls or other objects. However, I know that it is probably quite complex, even in Unity. On the other hand, Naughty Bear’s camera is quite adequate and much simpler to implement. It doesn’t reorient itself and can get obscured but it follows the player exactly the way I want to. For now, I’m quite satisfied just getting a Naughty Bear camera to work! :slight_smile:

I searched for awhile but never found quite what I wanted. I looked through some of the demo projects’ code and saw some good cameras but I’m not familiar enough with Unity yet to extract only the parts I need without having to undo lots of other stuff.

The code below is as simple as I could make it. It is working fairly well but isn’t quite what I want and I’m sure it can be improved. At this point, I’m not really concerned about player movement but just want to get the camera working like I want. Note that I’ve mapped my joystick axes to LeftX, LeftY, RightX and RightY. I’ve also set the Player as the target of the Camera.

Thanks for any insights! :slight_smile:

public class PlayerScript : MonoBehaviour
{
    public float RotateSpeed = 150,
        MoveSpeed = 50;
    float DeltaTime;

    void Update()
    {
        DeltaTime = Time.deltaTime;
        transform.Rotate(0, Input.GetAxis("LeftX") * RotateSpeed * DeltaTime, 0);
        transform.Translate(0, 0, -Input.GetAxis("LeftY") * MoveSpeed * DeltaTime);
    }
}

public class CameraScript : MonoBehaviour
{
    public GameObject Target;
    public float RotateSpeed = 170,
        FollowDistance = 20,
        FollowHeight = 10;
    float RotateSpeedPerTime,
        DesiredRotationAngle,
        DesiredHeight,
        CurrentRotationAngle,
        CurrentHeight,
        Yaw,
        Pitch;
    Quaternion CurrentRotation;

    void LateUpdate()
    {
        RotateSpeedPerTime = RotateSpeed * Time.deltaTime;

        DesiredRotationAngle = Target.transform.eulerAngles.y;
        DesiredHeight = Target.transform.position.y + FollowHeight;
        CurrentRotationAngle = transform.eulerAngles.y;
        CurrentHeight = transform.position.y;

        CurrentRotationAngle = Mathf.LerpAngle(CurrentRotationAngle, DesiredRotationAngle, 0);
        CurrentHeight = Mathf.Lerp(CurrentHeight, DesiredHeight, 0);

        CurrentRotation = Quaternion.Euler(0, CurrentRotationAngle, 0);
        transform.position = Target.transform.position;
        transform.position -= CurrentRotation * Vector3.forward * FollowDistance;
        transform.position = new Vector3(transform.position.x, CurrentHeight, transform.position.z);

        Yaw = Input.GetAxis("RightX") * RotateSpeedPerTime;
        Pitch = Input.GetAxis("RightY") * RotateSpeedPerTime;
        transform.Translate(new Vector3(Yaw, -Pitch, 0));
        transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z);

        transform.LookAt(Target.transform);
    }
}

Below is the code I used currently for my camera, minus the collision handling:

x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;


y += Input.GetAxis ("VerticalJoyR") * ySpeed * 0.02f;
x += Input.GetAxis ("HorizontalJoyR") * xSpeed * 0.02f;

y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0f);
transform.rotation = rotation;
HandleCollision();
Vector3 position = rotation * new Vector3(currXOffset, height, -_currDistance) + target.position;
_standardPosition = rotation * new Vector3(currXOffset, height, -distance) + target.position;
				
transform.position = position;

If you modified it like this, it may work for what you need:

x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;


y += Input.GetAxis ("RightY") *  RotateSpeedPerTime;
x += Input.GetAxis ("RightX") *  RotateSpeedPerTime;

y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0f);
transform.rotation = rotation;
Vector3 position = rotation * new Vector3(0f, height, FollowDistance) + target.position;
				
transform.position = position;