Unity向量的点乘用于判断怪物相对于人物的方向

Unity向量的点乘用于判断怪物相对于人物的方向

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Vector3_Class_methods : MonoBehaviour {

// 用于演示Vector3的函数

Vector3 Apoint;

Vector3 Bpoint;

Vector3 Cpoint;

public Transform oneObj, twoObj;

public Transform Hero, Monster;

public Transform PointA, PointB;

void Start()

{

Apoint = new Vector3(0, 0, 4);

Bpoint = new Vector3(0, 3, 0);

Cpoint = new Vector3(1, 1, 1);

Debug.Log("(0,0,1)和(0,1,0)两个向量夹角:"+Vector3.Angle(Apoint, Bpoint));//求向量夹角

Debug.Log("两个箱子两个向量夹角是(相对点是原点):" + Vector3.Angle(oneObj.position, twoObj.position));//求向量夹角

#region

Debug.Log("(0,0,1)和(0,1,0)两个向量点乘结果:" + Vector3.Dot(Apoint, Bpoint));//求向量点乘结果

Debug.Log("两个箱子坐标向量点乘结果(相对点是原点):" + Vector3.Dot(oneObj.position, twoObj.position));//求向量点乘结果

#endregion

}

// Update is called once per frame

void Update() {

//Debug.Log("(0,0,1)和(0,1,0)两个向量点乘结果:" + Vector3.Dot(Apoint, Bpoint));//求向量点乘结果

Debug.Log("两个箱子坐标向量点乘结果(相对点是原点):" + Vector3.Dot(oneObj.position, twoObj.position));//求向量点乘结果

Debug.DrawLine(Vector3.zero, twoObj.position, Color.red);

Debug.DrawLine(Vector3.zero, oneObj.position, Color.blue);

Relativepos(Hero, Monster);//用于判断物体hero和Monster的前后位置

DrawNormalLine(PointA.position, PointB.position);// 求两个向量的 构成面的法向量

Distance(Apoint, Bpoint);

}

private void DrawNormalLine(Vector3 A, Vector3 B)//求两个向量的 构成面的法向量

{

Vector3 NormalLine= Vector3.Cross(A, B);//求两个向量的 构成面的法向量

Vector3 StartPoint = (A + B) / 2;

Debug.Log(""+NormalLine+ NormalLine.normalized);

Debug.DrawLine(StartPoint, NormalLine, Color.green);

Debug.DrawLine(Vector3.zero, A, Color.blue);

Debug.DrawLine(Vector3.zero, B, Color.blue);

}

private void Relativepos(Transform Hero, Transform Monster)//用DOT【点乘】于判断物体hero和Monster的前后位置

{

Vector3 HeroPos = Hero.TransformDirection(Hero.forward);//从局部坐标系转世界坐标系

Vector3 RelaPosVector = Monster.position - Hero.position;

float Dot = Vector3.Dot(HeroPos, RelaPosVector);

Debug.DrawLine(Hero.position, Monster.position, Color.blue);

if (Dot > 0)

{

Debug.Log("怪物在角色英雄前面");

}

if (Dot == 0)

{

Debug.Log("怪物在角色英雄侧面");

}

if (Dot<0)

{

Debug.Log("怪物在角色英雄后面");

}

}

private void Distance(Vector3 A, Vector3 B)

{

Debug.Log("两个向量的距离"+Vector3.Distance(A,B));

}

}

相关推荐
FAREWELL000751 小时前
Unity学习总结篇(1)关于各种坐标系
学习·unity·c#·游戏引擎
与火星的孩子对话10 小时前
Unity3D开发AI桌面精灵/宠物系列 【六】 人物模型 语音口型同步 LipSync 、梅尔频谱MFCC技术、支持中英文自定义编辑- 基于 C# 语言开发
人工智能·unity·c#·游戏引擎·宠物·lipsync
虾球xz13 小时前
游戏引擎学习第293天:移动Familiars
c++·学习·游戏引擎
敲代码的 蜡笔小新14 小时前
【行为型之访问者模式】游戏开发实战——Unity灵活数据操作与跨系统交互的架构秘诀
unity·设计模式·c#·访问者模式
Magnum Lehar1 天前
3d游戏引擎EngineTest的系统实现3
java·开发语言·游戏引擎
动感光博1 天前
Unity序列化字段、单例模式(Singleton Pattern)
unity·单例模式·c#
虾球xz1 天前
游戏引擎学习第290天:完成分离渲染
c++·人工智能·学习·游戏引擎
虾球xz1 天前
游戏引擎学习第285天:“Traversables 的事务性占用”
c++·学习·游戏引擎
虾球xz1 天前
游戏引擎学习第280天:精简化的流式实体sim
数据库·c++·学习·游戏引擎
FAREWELL000751 天前
Unity基础学习(十五)核心系统——音效系统
学习·unity·c#·游戏引擎