|
|
using UnityEngine;
|
|
|
|
|
|
public class xjxz : MonoBehaviour {
|
|
|
|
|
|
|
|
|
public Transform 目标模型;
|
|
|
|
|
|
public bool mousechange;
|
|
|
[Tooltip("x:最小;y:最大")]
|
|
|
public Vector2 距离范围 = new Vector2(0.1f, 5f);
|
|
|
[Range(0f, 1f)]
|
|
|
public float 距离滑动条值 = 0.5f;
|
|
|
|
|
|
|
|
|
public float 左右旋转速度 = 2f;
|
|
|
public float 上下旋转速度 = 2f;
|
|
|
[Tooltip("x:最小;y:最大")]
|
|
|
public Vector2 上下旋转限制 = new Vector2(-20f, 80f);
|
|
|
|
|
|
|
|
|
public float 移动延迟 = 4f;
|
|
|
public bool slidering;
|
|
|
#region MonoBehaviour
|
|
|
public void Start()
|
|
|
{
|
|
|
if (!目标模型)
|
|
|
{
|
|
|
目标模型 = new GameObject("目标模型").transform;
|
|
|
}
|
|
|
|
|
|
|
|
|
位置初始化();
|
|
|
Now距离滑动条值 = 距离滑动条值;
|
|
|
重置位置();
|
|
|
}
|
|
|
|
|
|
public float mouseX = 40f;
|
|
|
public float mouseY = -30f;
|
|
|
Vector2 oldPosition1;
|
|
|
Vector2 oldPosition2;
|
|
|
public float distance;
|
|
|
public float dragSpeed = 10;
|
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
{
|
|
|
mouseX = -45f;
|
|
|
mouseY = 40f;
|
|
|
|
|
|
}
|
|
|
void Update()
|
|
|
{
|
|
|
distance = Get当前距离 ();
|
|
|
if (Input.GetKey(KeyCode.Y))
|
|
|
{
|
|
|
距离滑动条值 -= Time.deltaTime * 0.5f;
|
|
|
距离滑动条值 = Mathf.Clamp(距离滑动条值, 0, 1);
|
|
|
|
|
|
|
|
|
}
|
|
|
if (Input.GetKey(KeyCode.H))
|
|
|
{
|
|
|
距离滑动条值 += Time.deltaTime * 0.5f;
|
|
|
距离滑动条值 = Mathf.Clamp(距离滑动条值, 0, 1);
|
|
|
}
|
|
|
|
|
|
if (Input.GetMouseButton(1)&&!mousechange|| Input.GetMouseButton(0) && mousechange)
|
|
|
{
|
|
|
// transform.Translate(-Input.GetAxisRaw("Mouse X") * Time.deltaTime * dragSpeed, -Input.GetAxisRaw("Mouse Y") * Time.deltaTime * dragSpeed, 0);
|
|
|
Vector3 current= transform.TransformDirection(-Input.GetAxisRaw("Mouse X") * Time.deltaTime * dragSpeed, -Input.GetAxisRaw("Mouse Y") * Time.deltaTime * dragSpeed, 0);
|
|
|
目标模型.position += current;Debug.Log("U");
|
|
|
}
|
|
|
|
|
|
if (Input.GetKey(KeyCode.A))
|
|
|
{
|
|
|
mouseY += 0.8f;
|
|
|
|
|
|
|
|
|
}
|
|
|
if (Input.GetKey(KeyCode.D))
|
|
|
{
|
|
|
mouseY -= 0.8f;
|
|
|
}
|
|
|
if (Input.GetKey(KeyCode.X))
|
|
|
{
|
|
|
mouseX += 0.8f;
|
|
|
}
|
|
|
if (Input.GetKey(KeyCode.W))
|
|
|
{
|
|
|
mouseX -= 0.8f;
|
|
|
}
|
|
|
|
|
|
|
|
|
#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBPLAYER
|
|
|
if (Input.GetMouseButton(0))
|
|
|
{
|
|
|
mouseX += Input.GetAxis("Mouse X") * 左右旋转速度;
|
|
|
mouseY -= Input.GetAxis("Mouse Y") * 上下旋转速度;
|
|
|
|
|
|
|
|
|
}
|
|
|
else if (Input.GetAxis("Mouse ScrollWheel") != 0)
|
|
|
{
|
|
|
距离滑动条值 += Input.GetAxis("Mouse ScrollWheel") * 0.5f;
|
|
|
距离滑动条值 = Mathf.Clamp(距离滑动条值, 0, 1);
|
|
|
}
|
|
|
|
|
|
|
|
|
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
|
|
|
{
|
|
|
mouseX += Input.GetTouch(0).deltaPosition.x;
|
|
|
mouseY -= Input.GetTouch(0).deltaPosition.y;
|
|
|
}
|
|
|
else if (Input.touchCount > 1)
|
|
|
{
|
|
|
if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)
|
|
|
{
|
|
|
var tempPosition1 = Input.GetTouch(0).position;
|
|
|
var tempPosition2 = Input.GetTouch(1).position;
|
|
|
|
|
|
|
|
|
if (isZoom(oldPosition1, oldPosition2, tempPosition1, tempPosition2))
|
|
|
{
|
|
|
if (距离滑动条值 > 0)
|
|
|
距离滑动条值 -= 0.05f;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
if (距离滑动条值 < 1f)
|
|
|
距离滑动条值 += 0.05f;
|
|
|
}
|
|
|
|
|
|
|
|
|
oldPosition1 = tempPosition1;
|
|
|
oldPosition2 = tempPosition2;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
#else
|
|
|
slidering = false;
|
|
|
if (Input.touchCount == 1&&!slidering)
|
|
|
{
|
|
|
if (Input.GetTouch(0).phase == TouchPhase.Moved)
|
|
|
{
|
|
|
mouseX += Input.GetAxis("Mouse X") * 左右旋转速度;
|
|
|
mouseY -= Input.GetAxis("Mouse Y") * 上下旋转速度;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
else if (Input.touchCount > 1)
|
|
|
{
|
|
|
if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)
|
|
|
{
|
|
|
var tempPosition1 = Input.GetTouch(0).position;
|
|
|
var tempPosition2 = Input.GetTouch(1).position;
|
|
|
|
|
|
|
|
|
if (isZoom(oldPosition1, oldPosition2, tempPosition1, tempPosition2))
|
|
|
{
|
|
|
if (距离滑动条值 > 0)
|
|
|
距离滑动条值 -= 0.05f;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
if (距离滑动条值 < 1f)
|
|
|
距离滑动条值 += 0.05f;
|
|
|
}
|
|
|
|
|
|
|
|
|
oldPosition1 = tempPosition1;
|
|
|
oldPosition2 = tempPosition2;
|
|
|
}
|
|
|
}
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
void LateUpdate()
|
|
|
{
|
|
|
mouseY = ClampAngle(mouseY, 上下旋转限制.x, 上下旋转限制.y);
|
|
|
Quaternion toRotation = Quaternion.Euler(mouseY, mouseX, 0);
|
|
|
transform.rotation = Quaternion.Lerp(transform.rotation, toRotation, Time.deltaTime * 移动延迟);
|
|
|
|
|
|
|
|
|
if (目标模型 != null)
|
|
|
{
|
|
|
Vector3 toPosition = transform.rotation * new Vector3(0.0f, 0.0f, -Get当前距离()) + 目标模型.position;
|
|
|
transform.position = toPosition;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
// void OnGUI()
|
|
|
// {
|
|
|
// if(GUI.Button(new Rect(10, 10, 200, 50), "重置位置"))
|
|
|
// {
|
|
|
// 重置位置();
|
|
|
// }
|
|
|
// }
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
Vector3 StartAngles;
|
|
|
|
|
|
|
|
|
float Start距离滑动条值;
|
|
|
float Now距离滑动条值;
|
|
|
public void 位置初始化()
|
|
|
{
|
|
|
StartAngles = transform.eulerAngles;
|
|
|
Start距离滑动条值 = 距离滑动条值;
|
|
|
}
|
|
|
public void 重置位置()
|
|
|
{
|
|
|
mouseX = StartAngles.y;
|
|
|
mouseY = StartAngles.x;
|
|
|
距离滑动条值 = Start距离滑动条值;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public float Get当前距离()
|
|
|
{
|
|
|
Now距离滑动条值 = Mathf.Lerp(Now距离滑动条值, 距离滑动条值, Time.deltaTime * 移动延迟);
|
|
|
return 距离范围.x + (距离范围.y - 距离范围.x) * Now距离滑动条值;
|
|
|
}
|
|
|
|
|
|
|
|
|
#region Tools
|
|
|
/// <summary>
|
|
|
/// 放大/缩小
|
|
|
/// </summary>
|
|
|
/// <param name="oP1"></param>
|
|
|
/// <param name="oP2"></param>
|
|
|
/// <param name="nP1"></param>
|
|
|
/// <param name="nP2"></param>
|
|
|
/// <returns></returns>
|
|
|
bool isZoom(Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2)
|
|
|
{
|
|
|
float leng1 = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));
|
|
|
float leng2 = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y));
|
|
|
if (leng1 > leng2)
|
|
|
return true;
|
|
|
else
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// 限制旋转最大/最小值
|
|
|
/// </summary>
|
|
|
/// <param name="angle">当前</param>
|
|
|
/// <param name="min">最小</param>
|
|
|
/// <param name="max">最大</param>
|
|
|
/// <returns></returns>
|
|
|
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);
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
} |