提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言
日常开发中,模型的展示需要使摄像机画面聚焦在模型上,分享下鼠标控制的方法。代码中用到了DoTween插件的功能,大家自行下载导入。
一、控制代码
using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraControlObj : MonoBehaviour
{
public bool isControl=false;
private Vector3 v3init;
public float xMinLimit = -360f;
public float xMaxLimit = 360f;
public float yMinLimit = 0f;
public float yMaxLimit = 80f;
public float xSpeed = 50.0f;
public Transform target;
public float distance = 10.0f;
public float nearLimit = 1.0f;
public float farLimit = 80.0f;
public float movSpeedScroll = 5.0f;
public float movSpeed = 0.005f;
public float x;
public float y;
public float z;
Vector3 v3Pos;
float v3Distance;
Quaternion rotation;
Vector3 position;
// Use this for initialization
void Start()
{
ParamInit();
}
private bool bFstTouch;
private Vector3 vCurPos;
private Vector3 vOrgPos;
private float yDis;
private float xDis;
private Touch touch1;
private Touch touch2;
private Touch touch3;
private float touchDistance;
private float lastTouchDistance;
private float deltaDistance;
public void ParamInit()
{
//v3init = target.position;
//v3Pos = new Vector3(x, y, z);
//v3Distance = distance;
}
// Update is called once per frame
public void Update()
{
if (target==null|| !isControl)
{
return;
}
//if (Input.GetMouseButtonDown(0))
//{
// target.position= v3init;
// distance = v3Distance;
// x = v3Pos.x;
// y = v3Pos.y;
// z = v3Pos.z;
//}
if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WebGLPlayer || Application.platform == RuntimePlatform.WindowsPlayer)
{
if (Input.GetMouseButton(1))
{
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
x = ClampAngle(x, xMinLimit, xMaxLimit);
y -= Input.GetAxis("Mouse Y") * xSpeed * 0.02f;
y = ClampAngle(y, yMinLimit, yMaxLimit);
}
else if (Input.GetMouseButton(0))