Rotate camera around object - convert to touch
I'm trying to convert the code below to work with touch screen. Works perfectly with mouse, but I'd like to have pinch zoom and two finger pan on touch screen. Unfortunately my coding skills aren't up to the challenge (the code I have was pieced together from examples). Any help would be GREATLY appreciated.
Judging from the number of unanswered posts on this topic, I think many of us would benefit from the solution.
Many thanks to the generous soul(s) who help!
public class MouseOrbitTouch : 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;
void Start() {
if (target != null) transform.LookAt(target);
}
void Update() {
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;
}
}
}
}
Your answer
Follow this Question
Related Questions
my mobile button keeps pressing on its own instead of 1 tap 1 Answer
Multitouch second touch interrupts first, BUT is fine if the first touch is the third touch. WHAT!? 0 Answers
How can convert keyboard controller to touchscreen(android .ios ...etc) 1 Answer
TouchInput in Update. Physics in FixedUpdate. Lag and Lost calls? 1 Answer
How to replace KeyCode.Space with a simple tap on Android? 1 Answer