Unity 矩形攻击

Unity 矩形攻击

2D游戏可以通过计算两个物体的距离与与半径比较,判断相交,相切,相离。

  1. pos2-pos1<r2+r1(相交)
  2. pos2-pos1=r2+r1(相切)
  3. pos2-pos1>r2+r1(相离)
    3D游戏物理引擎消耗性能,尤其是在移动端设备上。
    需要用到的数学知识点乘
    A.B=|A|.|B|cos
    如果B是单位向量,那么点乘就是A在B上的投影,相反就是B在A上的投影,如果A,B都为单位向量,那么得到就是cos值,根据这些知识可以判断两个物体是否相撞。
    如图
    在这里插入图片描述
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RectA : MonoBehaviour
{
    public bool AttackRectJudge(Transform attacker,Transform attacked,float forwardDistance,float rightDistanse)
    {
        //计算两个物体之间的距离
        Vector3 deltaA = attacker.position - attacked.position;
        //计算delaA在攻击者上的投影
        float forwardDot = Vector3.Dot(attacker.forward, deltaA);
        //如果点乘大于零,说明被攻击者在攻击者的前方,
        if (forwardDot > 0 && forwardDot<=forwardDistance)
        {
            //delaA在攻击者右方的投影
            float rightDot = Math.Abs(Vector3.Dot(attacker.right, deltaA));

            if (rightDot<=rightDistanse)
            {
                //发生了碰撞
                return true;
            }
            return false;
        }
            return false;
    }
  
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值