- Home /
Convert Mouse Rotate/Pan/Zoom camera to touch
I have a C# script (not my code) that uses the mouse to rotate/pan/zoom a camera around a scene with a main focal point (PC Build). It works very well for me, but I need to convert it for a kiosk touch screen (windows Surface Pro) and have had no luck modifying the script or finding answers (my coding skills are weak). Would some kind soul please save me from this hell!
Current working mouse script is below.
Any assistance is GREATLY appreciated!
Version 2019.2.5
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MouseOrbit : MonoBehaviour {
public Transform target;
public float maxOffsetDistance = 2000f;
public float orbitSpeed = 15f;
public float panSpeed = .5f;
public float zoomSpeed = 10f;
private Vector3 targetOffset = Vector3.zero;
private Vector3 targetPosition;
// Use this for initialization
void Start() {
if (target != null) transform.LookAt(target);
}
void Update() {
targetPosition = target.position + targetOffset;
if (target != null) {
targetPosition = target.position + targetOffset;
// Left Mouse to Orbit
if (Input.GetMouseButton(0)) {
transform.RotateAround(targetPosition, Vector3.up, Input.GetAxis("Mouse X") * orbitSpeed);
float pitchAngle = Vector3.Angle(Vector3.up, transform.forward);
float pitchDelta = -Input.GetAxis("Mouse Y") * orbitSpeed;
float newAngle = Mathf.Clamp(pitchAngle + pitchDelta, 0f, 180f);
pitchDelta = newAngle - pitchAngle;
transform.RotateAround(targetPosition, transform.right, pitchDelta);
}
// Right Mouse To Pan
if (Input.GetMouseButton(1)) {
Vector3 offset = transform.right * -Input.GetAxis("Mouse X") * panSpeed + transform.up * -Input.GetAxis("Mouse Y") * panSpeed;
Vector3 newTargetOffset = Vector3.ClampMagnitude(targetOffset + offset, maxOffsetDistance);
transform.position += newTargetOffset - targetOffset;
targetOffset = newTargetOffset;
}
// Scroll to Zoom
transform.position += transform.forward * Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;
}
}
}// CLASS
You should get the cross platform input system from Unity standard assets, it will make your life a lot easier.
Also by default, I'm pretty sure $$anonymous$$ouseButton(0) counts and 1 touch, $$anonymous$$ouseButton(1) counts and 2 touches, and you can use Input.touches to iterate over the touches, it wouldn't be hard to implement a scroll by storing touch info, if unity doesn't already handle scrolling as two inputs seperating/closing
Rob, thank you VERY much for your reply - it's the only reply I've gotten on this topic on any forum. Unfortunately, I can't get the 'standard assets' because of my company firewall and ad$$anonymous$$ restrictions (I've tried!). $$anonymous$$y '$$anonymous$$ouseButton()' calls don't do anything for rotate/zoom/drag when I test on laptop. I've looked all over this, and other forums (+ YouTube) but can't find how to do this with touch. I found rotate an object with touch, but not a camera around an object. Posts on this topic are not being answered for some reason (not concisely anyway). So, now I'm trying to tackle one at a time - rotate - zoom - pan. $$anonymous$$y first task is rotate and I'm already stuck! Do you happen to have some sample code on this? Anything is much appreciated.
Your answer
![](https://koobas.hobune.stream/wayback/20220612224515im_/https://answers.unity.com/themes/thub/images/avi.jpg)