Question by
yaswong135 · Jun 26, 2019 at 04:51 AM ·
inputtouchscreenorbitdragging
Touchscreen orbit
I am trying to create a scene where a user can rotate a skull and move it and zoom in and out on a touchscreen tablet. I currently seem to be able to get it to work with my mouse and scroll wheel, but it doesn't work when on a touchscreen, despite other elements of the UI working with touchscreen input. Have I missed something?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
* This is an improved orbit script based on the MouseOrbitImproved script found
* on the unity community wiki. It should run smoother then the original version
*
* */
[AddComponentMenu("Camera-Control/Mouse drag Orbit with zoom")]
public class OrbitSkull : MonoBehaviour
{
public Transform target;
public float distance = 500.0f;
public float xSpeed = 0.1f;
public float ySpeed = 10.0f;
public float yMinLimit = -20f;
public float yMaxLimit = 80f;
public float distanceMin = 200f;
public float distanceMax = 500f;
public float smoothTime = 2f;
public float zoomSpeed = 100.0f;
float rotationYAxis = 0.0f;
float rotationXAxis = 0.0f;
float velocityX = 0.0f;
float velocityY = 0.0f;
public bool detectColliders = false;
private Touch touch;
private float x= 0.0f;
private float y= 0.0f;
// Use this for initialization
void Start()
{
Vector3 angles = transform.eulerAngles;
rotationYAxis = angles.y;
rotationXAxis = angles.x;
// Make the rigid body not change rotation
if (GetComponent<Rigidbody>())
{
GetComponent<Rigidbody>().freezeRotation = true;
}
}
// Late update happens after the normal update, and is used to make sure other
// update events have happened first
void LateUpdate()
{
if (target)
{
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
//One finger touch does orbit
touch = Input.GetTouch(0);
x += touch.deltaPosition.x * xSpeed * 0.02f;
y -= touch.deltaPosition.y * ySpeed * 0.02f;
}
if (Input.touchCount > 1 && (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved))
/* if (Input.GetMouseButton(0))
{
velocityX += xSpeed * Input.GetAxis("Mouse X") * distance * 0.02f;
velocityY += ySpeed * Input.GetAxis("Mouse Y") * 0.02f;
}
*/
rotationYAxis += velocityX;
rotationXAxis -= velocityY;
rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
Quaternion rotation = toRotation;
distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * zoomSpeed, distanceMin, distanceMax);
if (detectColliders) {
RaycastHit hit;
if (Physics.Linecast(target.position, transform.position, out hit))
{
distanceMin = hit.distance;
}
}
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * negDistance + target.position;
transform.rotation = rotation;
transform.position = position;
velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
}
}
// Clamp angle make sure the orbit does not flip the object upside down
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp(angle, min, max);
}
}
Comment