You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

273 lines
7.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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
}