Player Camera Orbit issues...

Hey there. At the moment I’m working on this Camera Control script with the sole purpose being that you can orbit the character by holding down right click, although when I stop clicking I want the camera to return to the normal third person perspective and copy the rotation of the player. I tried using various if statements and I created a “click” boolean as a method of returning if “click” was false, but nothing worked. Any Ideas?

Thanks guys :smile:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraControl : MonoBehaviour
{
private const float Y_ANGLE_MIN = 0.0f;
private const float Y_ANGLE_MAX =70.0f;

private bool click=false;

public Transform lookAt;
public Transform camTransform;
public float distance = 10.0f;

private float currentX = 0.0f;
private float currentY = 45.0f;
private float sensitivityX = 10f;
private float sensitivityY = 5f;

private void Start()
{
}

private void Update()
{
if (Input.GetMouseButton(1))
{
currentX += (Input.GetAxis(“Mouse X”) * sensitivityX);
currentY += (Input.GetAxis(“Mouse Y”) * sensitivityY);
click = true;
}
else
{
click = false;
}

currentY = Mathf.Clamp(currentY, Y_ANGLE_MIN, Y_ANGLE_MAX);
}

private void LateUpdate()
{
Vector3 dir = new Vector3(0, 0, -distance);
Quaternion rotation = Quaternion.Euler(currentY, currentX, 0);
camTransform.position = lookAt.position + rotation * dir;
camTransform.LookAt(lookAt.position);
}
}

You need to store the default X and Y angles so they can be reset when the user releases the mouse button.
Create 2 new constant floats (defaultX and defaultY) and give them values, in the ELSE part of your IF statement, set your currentX = defaultX and currentY = defaultY.

p.s. use the “insert code” button when posting code so it’s easier to read

Thanks so much, and sorry for not using the insert code button. It’s my first thread.

1 Like

@joshgav03 no worries, a lot of people forget to use it. Welcome to the forum :slight_smile: