Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Romano185 · Apr 28, 2012 at 01:11 PM · camerarotate

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?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kolban · Apr 28, 2012 at 01:16 PM 0
Share

It is a shame that script code doesn't have more comments in them.

avatar image aldonaletto · Apr 28, 2012 at 05:18 PM 1
Share

You could use this:

 target = transform.parent;

ins$$anonymous$$d of this:

 target = transform.parent.gameObject.GetComponent(Transform);
avatar image Tedeveloper · Apr 30, 2016 at 11:01 PM 0
Share

This works, but what happens is that if i dont left click down it will automatically go around slowly... Any fixes?

5 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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;
        }


Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aldonaletto · Apr 28, 2012 at 02:25 PM 0
Share

Better yet:

   rightclicked = Input.GetmouseButton(1);
avatar image Romano185 · Apr 29, 2012 at 08:51 AM 0
Share

It works great now, I can finally continue working on my game now!

avatar image
1

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);    
     }


//
}

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tedeveloper · Apr 30, 2016 at 12:19 PM 0
Share

It works, but if i have my hand off the left click, it automatically slowly rotates...?

Fix??

avatar image sovan-k1 Tedeveloper · May 03, 2016 at 08:46 AM 1
Share

Hi @Tedeveloper,

omit the following code snippet:

if(!Input.Get$$anonymous$$ouseButton(0)) transform.RotateAround (target.position, Vector3.up, degrees * Time.deltaTime);

Regards, Sovan

avatar image
0

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges