- Home /
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.
Your answer
Follow this Question
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