using UnityEngine;
using System.Collections;
public class LevelControl : MonoBehaviour
{
[Header("水平仪旋钮")]
public Transform level_rotate1;
public Transform level_rotate2;
public Transform level_rotate3;
public int level_rotate1_f;
public float level_rotate2_f;
public float level_rotate3_f;
[Header("水平仪气泡")]
public Transform level_bubble;
public Transform level_bubble_curPos;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
#region 旋钮1
if (GetMouseState.Instance.ClickLeftStay == level_rotate1.gameObject)
{
if (level_rotate1_f < 360)
{
level_rotate1.Rotate(new Vector3(0, 0, 1f));
level_rotate1_f++;
}
level_bubble.localPosition = new Vector3(-0.5f * level_rotate1_f / 18000 - 0.5f * level_rotate2_f / 18000 + level_rotate3_f / 18000, 0.886f * level_rotate1_f / 18000 + (-0.886f * level_rotate2_f / 18000), 0.003662437f);
LimitMoveDis();
}
if (GetMouseState.Instance.ClickRightStay == level_rotate1.gameObject)
{
if (level_rotate1_f > -360)
{
level_rotate1.Rotate(new Vector3(0, 0, -1f));
level_rotate1_f--;
}
level_bubble.localPosition = new Vector3(-0.5f * level_rotate1_f / 18000 - 0.5f * level_rotate2_f / 18000 + level_rotate3_f / 18000, 0.886f * level_rotate1_f / 18000 + (-0.886f * level_rotate2_f / 18000), 0.003662437f);
LimitMoveDis();
}
#endregion
#region 旋钮2
if (GetMouseState.Instance.ClickLeftStay == level_rotate2.gameObject)
{
if (level_rotate2_f < 360)
{
level_rotate2.Rotate(new Vector3(0, 0, 1f));
level_rotate2_f++;
}
level_bubble.localPosition = new Vector3(-0.5f * level_rotate1_f / 18000 - 0.5f * level_rotate2_f / 18000 + level_rotate3_f / 18000, 0.886f * level_rotate1_f / 18000 + (-0.886f * level_rotate2_f / 18000), 0.003662437f);
LimitMoveDis();
}
if (GetMouseState.Instance.ClickRightStay == level_rotate2.gameObject)
{
if (level_rotate2_f > -360)
{
level_rotate2.Rotate(new Vector3(0, 0, -1f));
level_rotate2_f--;
}
level_bubble.localPosition = new Vector3(-0.5f * level_rotate1_f / 18000 - 0.5f * level_rotate2_f / 18000 + level_rotate3_f / 18000, 0.886f * level_rotate1_f / 18000 + (-0.886f * level_rotate2_f / 18000), 0.003662437f);
LimitMoveDis();
}
#endregion
#region 旋钮3
if (GetMouseState.Instance.ClickLeftStay == level_rotate3.gameObject)
{
if (level_rotate3_f < 360)
{
level_rotate3.Rotate(new Vector3(0, 0, 1f));
level_rotate3_f++;
}
level_bubble.localPosition = new Vector3(-0.5f * level_rotate1_f / 18000 - 0.5f * level_rotate2_f / 18000 + level_rotate3_f / 18000, 0.886f * level_rotate1_f / 18000 + (-0.886f * level_rotate2_f / 18000), 0.003662437f);
LimitMoveDis();
}
if (GetMouseState.Instance.ClickRightStay == level_rotate3.gameObject)
{
if (level_rotate3_f > -360)
{
level_rotate3.Rotate(new Vector3(0, 0, -1f));
level_rotate3_f--;
}
level_bubble.localPosition = new Vector3(-0.5f * level_rotate1_f / 18000 - 0.5f * level_rotate2_f / 18000 + level_rotate3_f / 18000, 0.886f * level_rotate1_f / 18000 + (-0.886f * level_rotate2_f / 18000), 0.003662437f);
LimitMoveDis();
}
#endregion
}
/// <summary>
/// 限制范围
/// </summary>
private void LimitMoveDis()
{
//当物体在限制范围内时
if (Vector3.Distance(level_bubble.localPosition, level_bubble_curPos.localPosition) < 0.02f)
{
level_bubble.localPosition = level_bubble.localPosition;
}
//当物体出限制范围时
else
{
level_bubble.localPosition = level_bubble_curPos.localPosition + (level_bubble.localPosition - level_bubble_curPos.localPosition).normalized * 0.02f;
}
}
}
利用力的分解和合成进行模拟了水平仪系统,此处使用的是三点调平的水平仪,相互夹角为120°