达成效果:
镜头的是45度的俯视角镜头:
using UnityEngine;
using UnityEngine.EventSystems;
public class Move_Scal : MonoBehaviour, IDragHandler
{
public float dragSpeed;
private Vector3 p1;
private Vector3 camera_right;
private Vector3 camera_up;
private Vector3 camera_forward;
public void Awake()
{
camera_right = Camera.main.transform.right;
camera_up = Camera.main.transform.up;
camera_forward = Camera.main.transform.forward;
}
public void OnDrag(PointerEventData eventData)
{
p1 = Camera.main.transform.position - camera_right * Input.GetAxisRaw("Mouse X") * dragSpeed * Time.timeScale
- (camera_up + camera_forward).normalized * Input.GetAxisRaw("Mouse Y") * dragSpeed * Time.timeScale;
Camera.main.transform.position = p1;
}
}
需要将脚本挂在Canvas上,canvas设置一张image作为鼠标检测的UI,并勾选Raycast Target,否则不会触发OnDrag方法。