- Home /
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?
It is a shame that script code doesn't have more comments in them.
You could use this:
target = transform.parent;
ins$$anonymous$$d of this:
target = transform.parent.gameObject.GetComponent(Transform);
This works, but what happens is that if i dont left click down it will automatically go around slowly... Any fixes?
Answer by MangoDerp · Apr 28, 2012 at 01:53 PM
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;
}
It works great now, I can finally continue working on my game now!
Answer by sovan-k1 · Jan 18, 2016 at 01:38 PM
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);
}
//
}
It works, but if i have my hand off the left click, it automatically slowly rotates...?
Fix??
Hi @Tedeveloper,
omit the following code snippet:
if(!Input.Get$$anonymous$$ouseButton(0)) transform.RotateAround (target.position, Vector3.up, degrees * Time.deltaTime);
Regards, Sovan
Answer by ApplezGaming · Oct 15, 2012 at 08:45 PM
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?
Answer by DeanDeano · Feb 26, 2013 at 03:42 PM
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)
Answer by ModelArt · Jul 07, 2017 at 07:55 PM
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
Your answer
Follow this Question
Related Questions
How to make the camera rotate only when right click is held down on the mouse? 1 Answer
Set Max Rotation On Weapon Sway 0 Answers
Camera Rotation Tips 1 Answer
Move character in direction its facing 1 Answer
Make camera rotate don't stop 1 Answer