Rotate camera when right mouse button is down

Hi everybody

I want to write a script which rotates the camera around the player when I hold the right mouse button down. I tried using this script I found somewhere on this site:

var target : Transform;
var distance = 5.0;
var xSpeed = 125.0;
var rightclicked : boolean = false;

private var x = 0.0;

@script AddComponentMenu("Camera-Control/Mouse Orbit")


function Start () {
    var angles = transform.eulerAngles;
    x = angles.y;
    target = transform.parent.gameObject.GetComponent(Transform);
}

function Update (){
	if (Input.GetMouseButtonDown(1)){
		rightclicked = true;
		}
	else{
		rightclicked = false;
	}
}

function LateUpdate () {
    if (target && rightclicked == true) {
      	x += Input.GetAxis("Mouse X") * xSpeed * distance* 0.02;
       	var rotation = Quaternion.Euler(0, x, 0);
       	var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
       	transform.rotation = rotation;
       	transform.position = position;
    }	
}

But this script doesn’t work. I want the camera to rotate around a GameObject, its parent when I hold down the right mouse button.

Does anyone know what’s wrong with this script?

if (Input.GetMouseButtonDown(1)){
rightclicked = true;
}
else{
rightclicked = false;
}

try changing it to

 if (Input.GetMouseButtonDown(1)){
       rightclicked = true;
       }
     if (Input.GetMouseButtonUp(1)){
       rightclicked = false;
       }

i am also making a game which requires a script like that but how i get to rotate around an object when clicked not just stay in one spot and look left and right?

I have added this but for some reason it wont keep the transform on my player it resets every time I click play any ideas why ?

(Shows it as being attached before play but when I click play it deselects it)

using UnityEngine;
using System.Collections;

public class CameraRotateExploreScene : MonoBehaviour 
{

	public Transform target;
	int degrees = 10;

	// Update is called once per frame
	void Update () 
	{
		if (Input.GetMouseButton (0)) 
		{	
		
		transform.RotateAround (target.position, Vector3.up, Input.GetAxis ("Mouse X")* degrees);
			//			transform.RotateAround (target.position, Vector3.left, Input.GetAxis ("Mouse Y")* dragSpeed);

		}

		if(!Input.GetMouseButton(0))
		transform.RotateAround (target.position, Vector3.up, degrees * Time.deltaTime);	
	}

//
}

using UnityEngine;
using System.Collections;
public class CameraRotate : MonoBehaviour
{
public Transform target;
int degrees = 0;
// Update is called once per frame

void Update () 
{ 
	
	if (Input.GetMouseButton (1)) 
	{    

		degrees = 10;
		transform.RotateAround (target.position, Vector3.up, Input.GetAxis ("Mouse X")* degrees);
		//            transform.RotateAround (target.position, Vector3.left, Input.GetAxis ("Mouse Y")* dragSpeed);
	}
	if (!Input.GetMouseButton (1))
		transform.RotateAround (target.position, Vector3.up, degrees * Time.deltaTime);
	else {
	
		degrees = 0;
	}
}

}

This version of Sovan’s Script will set the rotate speed to 10, then if not mouse button down, it will return it to 0, this should fix your problem you were having.

working in Unity 5.5.1