物体拖动角度,力度检测

力度是假的,算的时间差

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ball : MonoBehaviour
{
    public Transform bowl;
    bool _down = false;
    bool _up = false;

    Vector2 _starPos,_endPos,_bowlPos;
    Vector2 _line1,_line2;

    float _time = 0;

    // Use this for initialization
    void Start ()
    {
        _starPos = Camera.main.WorldToScreenPoint (transform.position);
        _bowlPos = Camera.main.WorldToScreenPoint (bowl.position);
        _line1 = _bowlPos - _starPos;
    }
    
    // Update is called once per frame
    void Update ()
    {
        CheckTime ();
    }

    public void OnPointDown()
    {
        Debug.Log ("按下");
        _down = true;
        _up = false;
        _time = 0;
    }

    public void OnPointUp()
    {
        Debug.Log ("抬起 ");
        _up = true;
        _endPos = Input.mousePosition;
        _line2 = _endPos - _starPos;

        if (CheckAngle())
        {
            switch (CheckTime())
            {
            case 1:
                {
                    Debug.Log ("move");
                    break;
                }
            case 2:
                {
                    Debug.Log ("move Out");
                    break;
                }
            }
            Debug.Log ("Time=====" + _time);
        }

    }

    bool CheckAngle()
    {
        float angle = Vector2.Angle (_line1, _line2);
        Debug.Log ("angle" + angle);
        return angle < 30;
    }

    int CheckTime()
    {
        if (_down)
        {
            _time += 0.1f; 
        }
        if (_up)
        {
            _down = false;
            if (_time > 1 && _time < 2) 
            {
                _time = 0;
                return 1;//OK
            }
            else if (_time <= 1
            ) 
            {
                _time = 0;
                return 2;//Out
            }
        }
            
        return -1;
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值