I created a scene like the uploaded image file, set it so that the camera moves around the sphere by mouse dragging.
The problem is that if you touch the UI, the overlapping game objects under the UI will also be touched and the camera will move.
I want to avoid touching the game object that overlaps with the UI object. what should i do?
I am a novice script, so please tell me the script to solve this.
Also, please tell me the setting method. Please.
I used this script to rotate the camera around the sphere.
using UnityEngine;
using System.Collections;
public class CameraRotate : MonoBehaviour
{
public Transform targetObject;
public Vector3 targetOffset;
public float averageDistance = 5.0f;
public float maxDistance = 20;
public float minDistance = .6f;
public float xSpeed = 200.0f;
public float ySpeed = 200.0f;
public int yMinLimit = -80;
public int yMaxLimit = 80;
public int zoomSpeed = 40;
public float panSpeed = 0.3f;
public float zoomDampening = 5.0f;
public float rotateOnOff = 1;
private float xDeg = 0.0f;
private float yDeg = 0.0f;
private float currentDistance;
private float desiredDistance;
private Quaternion currentRotation;
private Quaternion desiredRotation;
private Quaternion rotation;
private Vector3 position;
void Start() { Init(); }
void OnEnable() { Init(); }
public void Init()
{
if (!targetObject)
{
GameObject go = new GameObject("Cam Target");
go.transform.position = transform.position + (transform.forward * averageDistance);
targetObject = go.transform;
}
currentDistance = averageDistance;
desiredDistance = averageDistance;
position = transform.position;
rotation = transform.rotation;
currentRotation = transform.rotation;
desiredRotation = transform.rotation;
xDeg = Vector3.Angle(Vector3.right, transform.right );
yDeg = Vector3.Angle(Vector3.up, transform.up );
position = targetObject.position - (rotation * Vector3.forward * currentDistance + targetOffset);
}
void LateUpdate()
{
if (Input.GetMouseButton(2) && Input.GetKey(KeyCode.LeftAlt) && Input.GetKey(KeyCode.LeftControl))
{
desiredDistance -= Input.GetAxis("Mouse Y") * 0.02f * zoomSpeed*0.125f * Mathf.Abs(desiredDistance);
}
else if (Input.GetMouseButton(0) )
{
xDeg += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
yDeg -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit);
desiredRotation = Quaternion.Euler(yDeg, xDeg, 0);
currentRotation = transform.rotation;
rotation = Quaternion.Lerp(currentRotation, desiredRotation, 0.02f * zoomDampening);
transform.rotation = rotation;
}
desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * 0.1f * zoomSpeed * Mathf.Abs
(desiredDistance);
desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);
currentDistance = Mathf.Lerp(currentDistance, desiredDistance, 0.1f * zoomDampening);
position = targetObject.position - (rotation * Vector3.forward * currentDistance +
targetOffset);
transform.position = position;
}
private static float ClampAngle(float angle, float min, float max)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp(angle, min, max);
}
}