Unity 模拟水平仪系统

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°

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值