0
\$\begingroup\$

I have a Unity C# script 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. Would some kind soul please save me from this hell!

My current working mouse script is below.

Any assistance is GREATLY appreciated!

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 ```
\$\endgroup\$
3
  • \$\begingroup\$ What specific touch gesture do you want to implement first? How have you to detect that gesture so far? What went wrong? The more precisely you can narrow down the problem, the better the answers you'll get, faster. \$\endgroup\$ – DMGregory Mar 6 '20 at 23:24
  • \$\begingroup\$ The only thing I have gotten to work is to rotate an object with touch (borrowed code). Works great, but I need to rotate the camera around an object. I can't find any good samples on this anywhere. I found lots of people asking, but no clear answers on how to do it (hard I'm guessing?). Unfortunately, my coding skills aren't sufficient to write this from scratch; I'm hoping to find an example and then tweak for my needs. Any examples out there? Many people would benefit. Many thanks for the reply! \$\endgroup\$ – Alex Mar 10 '20 at 21:03
  • \$\begingroup\$ Not hard. You just need to explain precisely what outcome you want. Taking time to complain that you can't find anything or that you don't know how to code it adds zero useful information to your question. (If you'd found the answer already, you wouldn't be posting here after all!) Instead, spend that effort explaining the intended behaviour. What patterns of touch inputs should map to what responses? How have you tried detecting those input patterns? How have you tried implementing the resulting camera change? \$\endgroup\$ – DMGregory Mar 10 '20 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.