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 Schiel · Oct 07, 2013 at 01:27 AM · cameracamera-movementcamera rotatemouseclickmouse-drag

Sketchup-style camera movement/rotation?.

Hi there, I'm brand-new to Unity - and programming in general, but I'm beginning work on an RTS/RPG game. I've been using Sketchup for a long time, and I really like how the camera movement/rotation works within the program(middle click+drag to rotate, middle+left+drag to pan).

I haven't been able to find any leads on how to replicate that style in Unity , though. I think starting with a mouse position/control script to reference back to is the right way to go, but I'm not sure where to go from there, or if what I have will even work for both unit and camera control in the way that I'd like.

Here's what I have so far, based on Maroe's "DetectClicksAndTouches"(which was just left/right click detection and touch functionality) - with left/right/middle clicking, then shift+click for each, and middle+left hold-down:

 using UnityEngine;
 using System.Collections;
 
 public class DetectClicks : MonoBehaviour
 {    
     public Camera detectionCamera;
     
     //This variable adds a Debug.Log call to show what was touched
     public bool debug = false;
     
     //This is the actual camera we reference in the update loop, set in Start()
     private Camera _camera;
     
     void Start()
     {
         if(detectionCamera != null)
         {
             _camera = detectionCamera;
         }
         else
         {
             _camera = Camera.main;
         }
     }
     
     // Update is called once per frame
     void Update ()
     {
         Ray ray;
         RaycastHit hit;
             
 // LEFT CLICK
         
             //Check to see if we've left-clicked without shift or middle mouse button
             if(Input.GetMouseButtonDown(0) && (!Input.GetKey(KeyCode.LeftShift)) && (!Input.GetMouseButton(2)))  
             {
                 //Set up our ray from screen to scene
                 ray = _camera.ScreenPointToRay(Input.mousePosition); 
                 
                 //If we hit...
                 if(Physics.Raycast (ray, out hit, Mathf.Infinity))
                 {
                         Debug.Log("You left clicked " + hit.collider.gameObject.name,hit.collider.gameObject);
                     
                     
                     //Run the Clicked() function on the clicked object
                     hit.transform.gameObject.SendMessage("LeftClicked", hit.point, SendMessageOptions.DontRequireReceiver);
                         
                 
                 }
             
             }
         
                     //SHIFT-LEFT CLICK
                     else if(Input.GetMouseButtonDown(0) && (Input.GetKey(KeyCode.LeftShift)))
                     {
                         //Set up our ray from screen to scene
                         ray = _camera.ScreenPointToRay(Input.mousePosition); 
                 
                         //If we hit...
                         if(Physics.Raycast (ray, out hit, Mathf.Infinity))
                         {
                                 Debug.Log("You shift-left clicked " + hit.collider.gameObject.name,hit.collider.gameObject);
                     
                             //Run the Clicked() function on the clicked object
                             hit.transform.gameObject.SendMessage("ShiftLeftClicked", hit.point, SendMessageOptions.DontRequireReceiver);
                         }
                     }
         
         
             
 // RIGHT CLICK
             
             //Check to see if we've right-clicked without shift
             if(Input.GetMouseButtonDown(1) && (!Input.GetKey(KeyCode.LeftShift)))  
             {
                 //Set up our ray from screen to scene
                 ray = _camera.ScreenPointToRay(Input.mousePosition); 
                 
                 //If we hit...
                 if(Physics.Raycast (ray, out hit, Mathf.Infinity))
                 {
                         Debug.Log("You right clicked " + hit.collider.gameObject.name,hit.collider.gameObject);
                     
                     
                     //Run the Clicked() function on the clicked object
                     hit.transform.gameObject.SendMessage("RightClicked", hit.point, SendMessageOptions.DontRequireReceiver);
             
                 }            
             }
         
 
             
 // MIDDLE CLICK (HOLD DOWN (camera movement?))
             
             //Check to see if we've middle-clicked and are holding down without shift or left mouse button
             if(Input.GetMouseButton(2) && (!Input.GetKey(KeyCode.LeftShift) && (!Input.GetMouseButton(0))))
             {
                 //Set up our ray from screen to scene
                 ray = _camera.ScreenPointToRay(Input.mousePosition); 
                 
                 //If we hit...
                 if(Physics.Raycast (ray, out hit, Mathf.Infinity))
                 {
                         Debug.Log("You are middle-cicking " + hit.collider.gameObject.name,hit.collider.gameObject);
                     
                     
                     //Run the Clicked() function on the clicked object
                     hit.transform.gameObject.SendMessage("MiddleClicked", hit.point, SendMessageOptions.DontRequireReceiver);
                 
 
                 }
             }
         
     //SHIFT-MIDDLE CLICK
                     else if(Input.GetMouseButton(2) && (Input.GetKey(KeyCode.LeftShift)))
                     {
                         //Set up our ray from screen to scene
                         ray = _camera.ScreenPointToRay(Input.mousePosition); 
                 
                         //If we hit...
                         if(Physics.Raycast (ray, out hit, Mathf.Infinity))
                         {
                                 Debug.Log("You are shift-middle clicking " + hit.collider.gameObject.name,hit.collider.gameObject);
                     
                             //Run the Clicked() function on the clicked object
                             hit.transform.gameObject.SendMessage("ShiftMiddleClicked", hit.point, SendMessageOptions.DontRequireReceiver);
                         }
                     }
         
     //LEFT-MIDDLE CLICK
                     else if(Input.GetMouseButton(2) && (Input.GetMouseButton(0)))
                     {
                         //Set up our ray from screen to scene
                         ray = _camera.ScreenPointToRay(Input.mousePosition); 
                 
                         //If we hit...
                         if(Physics.Raycast (ray, out hit, Mathf.Infinity))
                         {
                                 Debug.Log("You are left-middle clicking " + hit.collider.gameObject.name,hit.collider.gameObject);
                     
                             //Run the Clicked() function on the clicked object
                             hit.transform.gameObject.SendMessage("LeftMiddleClicked", hit.point, SendMessageOptions.DontRequireReceiver);
                         }
                     }
             
             
             
         }
     }


Would this work for all mouse controls in the game(camera/units/etc)? Which direction should I head from here if I want to get camera movement down first?

Thank you, and sorry for the long post.

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

15 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 avatar image avatar image avatar image

Related Questions

How to create a panning, rotating camera with Cinemachine Freelook camera? 0 Answers

Unity - Dancing Ball World camera following and rotating system 1 Answer

Cinemachine input axis camera movement,cinemachine input axis control with script 1 Answer

How to allow camera to complete an upside down rotation while using LookAt() 0 Answers

Would anyone teach me how to create camera movement and look controls? 2 Answers


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