Unity 判断画圆(自己写的算法,主要提供一个思路参考)

今天做雷达的时候,突然发现雷达只能左翻翻,右翻翻。。。。。一些很无聊的操作,看到同事的 1加6t 手机画圆就能快速开启摄像头,萌生了一个想法,我要用雷达来识别画圆,今天晚上刚好有时间,写一下自己的小算法,虽然肯定是没人家专业的精确,不过到底还是实现了画圆的手势识别,逻辑都写在注释里了,如果你有更好的想法,Call me!

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

public class JudgeDraw : MonoBehaviour {
    // 画圆的精度点数,值越高要求画的越圆
    public int point = 6;
    // 画圆的精度差值,值越低要求画的越圆
    public float num = 100f;
    float beginTimer= 0;
    float betweenTimer = 0;
    Vector2 beginPos;
    Vector2 endPos;
    Vector2 betweenPos;
    List<float> posFloat;
    List<Vector2> posVector2;
    int maxIndex = 0;
    int i = 0;
    bool isBool = false;
    void Start(){
    	posFloat = new List<float>();
        posVector2 = new List<Vector2>();
    }
	// Update is called once per frame
	void Update () {

        // 开始坐标和结束坐标小于某个值
        // 取得开始点和最远点的半径 以及他们的中间坐标
        // 将取得的点与中间坐标相比小于某个差值
        if (Input.GetMouseButtonDown(0))
        {
            // 记录画圆的开始点坐标
            beginPos = Input.mousePosition;
        }
        if (Input.GetMouseButton(0))
        {
            beginTimer += Time.deltaTime;
            // 1.2秒之内计算点
            if (beginTimer > 1.2f)
            {
                beginTimer = 0f;
            }
            else
            {
                betweenTimer += Time.deltaTime;
                // 每隔0.1秒取点
                if (betweenTimer >= 0.1f)
                {
                    posVector2.Add(Input.mousePosition);
                    float f = Vector2.Distance(beginPos, Input.mousePosition);
                    posFloat.Add(f);
                    betweenTimer = 0f;
                }
            }
        }
        if (Input.GetMouseButtonUp(0))
        {
            // 画圆的结束点
            endPos = Input.mousePosition;
            beginTimer = 0f;
            betweenTimer = 0f;
            // 找到这些点最大值的索引和坐标
            for (int i = 1; i < posFloat.Count; i++)
            {
                if (posFloat[i] > posFloat[maxIndex])
                {
                    maxIndex = i;
                }
            }
            // 开始坐标和最远点的半径,以此作为判断依据
            float radius = posFloat[maxIndex] / 2;
            print("radius" + radius);
            // 开始坐标和最远点的中间坐标 
            betweenPos = (beginPos + posVector2[maxIndex])/2;
            foreach (var item in posVector2)
            {
                print(Vector2.Distance(item, betweenPos));
                // 判断获得的坐标和中间坐标的距离 和半径相减的绝对值 是不是小于一定范围
                if (Mathf.Abs(Vector2.Distance(item, betweenPos)-radius) < num)
                {
                    i++;
                }
            }
            print("当有(默认为)6个点在半径差值范围内");
            if (i > point)
            {
                isBool = true;

            }
            if (Vector2.Distance(beginPos,endPos) <= radius && isBool)
            {
                print("你画了一个圆");
            }
            // 数据初始化
            i = 0;
            maxIndex = 0;
            posFloat.Clear();
            posVector2.Clear();
            isBool = false;
        }
	}
}

 

以下是一个简单的Unity 2D平台跳跃寻路算法的示例: 1. 创建一个空的游戏对象,将其命名为“Player”。 2. 将一个2D刚体组件添加到“Player”游戏对象中,并将重力比例设置为0,以便我们自己控制跳跃。 3. 添加一个Box Collider 2D组件,以便我们可以检测与地面的碰撞。 4. 创建一个地面游戏对象,并将其命名为“Ground”。 5. 将一个Box Collider 2D组件添加到“Ground”游戏对象中,并将其大小设置为适当的大小。 6. 创建一个C#脚本,并将其添加到“Player”游戏对象中。 7. 在脚本中,我们需要定义一些变量,如跳跃力和移动速度: ```csharp public float jumpForce = 7f; // 跳跃力 public float moveSpeed = 5f; // 移动速度 ``` 8. 接下来,我们需要定义一些方法来控制移动和跳跃: ```csharp void Move(float direction) { transform.Translate(Vector2.right * direction * moveSpeed * Time.deltaTime); } void Jump() { GetComponent<Rigidbody2D>().velocity = Vector2.up * jumpForce; } ``` 9. 在Update方法中,我们需要检测玩家是否按下跳跃键,并且检测玩家是否与地面相撞: ```csharp void Update() { float direction = Input.GetAxis("Horizontal"); Move(direction); if (Input.GetKeyDown(KeyCode.Space) && IsGrounded()) { Jump(); } } bool IsGrounded() { Collider2D collider = GetComponent<Collider2D>(); RaycastHit2D hit = Physics2D.Raycast(collider.bounds.center, Vector2.down, collider.bounds.extents.y + 0.1f, LayerMask.GetMask("Ground")); return hit.collider != null; } ``` 10. 最后,我们需要在场景中添加一些地面,并将它们标记为“Ground”层。我们还可以添加一些障碍物,并在“Player”对象和障碍物之间添加一个方法来检测碰撞。 这是一个简单的Unity 2D平台跳跃寻路算法的示例,你可以根据自己的需求进行修改和扩展。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值