unity运用点乘和叉乘实现AI自动转向到敌人

unity运用点乘和叉乘实现AI自动转向到敌人

1.点乘判断敌人在AI玩家的前方还是后方,叉乘判断敌人在AI玩家的左方还是右方

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

public class TestCross : MonoBehaviour
{
    public Transform cubeA;//玩家AI
    public Transform cubeB;//敌人
    
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            TestCube(cubeA,cubeB);
        }
        
       
    }
   
    private void OnDrawGizmos()
    {
        Gizmos.color = Color.green;
       
        Gizmos.DrawRay(cubeA.position*-10, cubeA.forward * 20);
    }
    void TestCube(Transform a,Transform b)
    {
        Vector3 enermyVec = b.position - a.position;
        Vector3 forwardVec = a.forward;

        float res = Vector3.Dot(enermyVec,forwardVec);
        Debug.Log("点积"+res);
        if (res>0)
        {
            Debug.Log("敌人在我的前边");
        }
        else if (res==0)
        {
            Debug.Log("两物体垂直");
        }
        else
        {
            Debug.Log("敌人在我的后边");
        }
        Vector3 cross = Vector3.Cross(forwardVec,enermyVec);
        bool isleft = false;
        if (cross.y>=0)
        {
            Debug.Log("敌人在我的右边");
            isleft = false;
        }
        else if (cross.y < 0)
        {
            Debug.Log("敌人在我的左边");
            isleft = true;
        }
        //计算玩家AI向敌人转向的角度
        float angle =  Vector3.Angle(forwardVec,enermyVec);
        StartCoroutine(ContinueRoate(angle,cubeA,isleft));
    }
    //自动转向
    IEnumerator ContinueRoate(float angle,Transform roateObj,bool isLeft)
    {
       
        bool isCom = false;
        while (!isCom)
        {
            Vector3 quaternion = roateObj.rotation.eulerAngles;
            if (isLeft)
            {
                float y = quaternion.y - 0.1f;
                roateObj.rotation = Quaternion.Euler(new Vector3(quaternion.x, y, quaternion.z));
            }
            else
            {
                float y = quaternion.y + 0.1f;
                roateObj.rotation = Quaternion.Euler(new Vector3(quaternion.x, y, quaternion.z));
            }
            
            angle -= 0.1f;
            if (angle-0.1f<=0)
            {
                isCom = true;
            }
            yield return null;
        }
    }
}

2.untiy场景挂物体
在这里插入图片描述
3.运行效果

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Unity 中,点乘(Dot Product)、乘(Cross Product)和投影(Projection)是常用的向量运算。下面我会分别解释它们的含义和用法。 1. 点乘(Dot Product):点乘是两个向量之间的一种运算,结果是一个标量(Scalar)。它可以用来计算两个向量之间的夹角余弦值,还可以判断两个向量是否正交(垂直)或平行。在 Unity 中,可以使用 Vector3.Dot 方法来计算两个三维向量的点乘结果。 示例代码: ```csharp Vector3 a = new Vector3(1, 2, 3); Vector3 b = new Vector3(4, 5, 6); float dotProduct = Vector3.Dot(a, b); ``` 2. 乘(Cross Product):乘是两个向量之间的一种运算,结果是一个新的向量。它的方向垂直于原始两个向量,并符合右手法则。在 Unity 中,可以使用 Vector3.Cross 方法来计算两个三维向量的乘结果。 示例代码: ```csharp Vector3 a = new Vector3(1, 2, 3); Vector3 b = new Vector3(4, 5, 6); Vector3 crossProduct = Vector3.Cross(a, b); ``` 3. 投影(Projection):投影是将一个向量沿着另一个向量的方向进行投影的过程。投影后的结果是一个新的向量,与原始向量垂直。在 Unity 中,可以使用 Vector3.Project 方法来计算一个三维向量在另一个三维向量上的投影结果。 示例代码: ```csharp Vector3 a = new Vector3(1, 2, 3); Vector3 b = new Vector3(4, 0, 0); Vector3 projectedVector = Vector3.Project(a, b); ``` 以上是在 Unity 中使用点乘、乘和投影的基本示例。希望对你有所帮助!如果还有其他问题,请继续提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sunshine落风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值