using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lesson4 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
#region 知識點一 向量
//三維向量 - Vector3
//Vector3有兩種幾何意義
//1.位置 —— 代表一個點
print(this.transform.position);
//2.方向 —— 代表一個方向
print(this.transform.forward);
print(this.transform.up);
Vector3 v = new Vector3(1, 2, 3);
Vector2 v2 = new Vector2(1, 2);
#endregion
#region 知識點二 兩點決定一向量
//A和B此時 幾何意義 是兩個點
Vector3 A = new Vector3(1, 2, 3);
Vector3 B = new Vector3(5, 1, 5);
//求向量
//此時 AB和 BA 他們的幾何意義 是兩個向量
Vector3 AB = B - A;
Vector3 BA = A - B;
#endregion
#region 知識點三 零向量和負向量
print(Vector3.zero);
print(Vector3.forward);
print(-Vector3.forward);
#endregion
#region 知識點四 向量的模長
//Vector3中提供了獲取向量模長的成員屬性
//magnitude
print(AB.magnitude);
Vector3 C = new Vector3(5, 6, 7);
print(C.magnitude);
print(Vector3.Distance(A, B));
#endregion
#region 知識點五 單位向量
//Vector3中提供了獲取單位向量的成員屬性
//normalized
print(AB.normalized);
print(AB / AB.magnitude);
#endregion
}
//總結
//1.Vector3這邊變數 可以表示一個點 也可以表示一個向量 具體表示什么 是根據我們的具體需求和邏輯決定
//2.如何在Unity里面得到向量 重點減起點 就可以得到向量 點C也可以代表向量 代表的就是 OC向量 O是坐標系原點
//3.得到了向量 就可以利用 Vector3中提供的 成員屬性 得到模長和單位向量
//4.模長相當于可以得到 兩點之間的距離 單位向量 主要是用來進行移動計算的 它不會影響我們想要的移動效果
// Update is called once per frame
void Update()
{
}
}
點乘

關于求A與B的相對位置,前后
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lesson6 : MonoBehaviour
{
public Transform target;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
#region 補充知識 除錯畫線
//畫線段
//前兩個引數 分別是 起點 終點
//Debug.DrawLine(this.transform.position, this.transform.position + this.transform.forward, Color.red);
//畫射線
//前兩個引數 分別是 起點 方向
//Debug.DrawRay(this.transform.position, this.transform.forward, Color.white);
#endregion
#region 知識點一 通過點乘判斷物件方位
//Vector3 提供了計算點乘的方法
Debug.DrawRay(this.transform.position, this.transform.forward, Color.red);
Debug.DrawRay(this.transform.position, target.position - this.transform.position, Color.red);
//得到兩個向量的點乘結果
//向量 a 點乘 AB 的結果
float dotResult = Vector3.Dot(this.transform.forward, target.position - this.transform.position);
if( dotResult >= 0 )
{
print("它在我前方");
}
else
{
print("它在我后方");
}
#endregion
#region 知識點二 通過點乘推導公式算出夾角
//步驟
//1.用單位向量算出點乘結果
dotResult = Vector3.Dot(this.transform.forward, (target.position - this.transform.position).normalized);
//2.用反三角函式得出角度
print("角度-" + Mathf.Acos(dotResult) * Mathf.Rad2Deg);
//Vector3中提供了 得到兩個向量之間夾角的方法
print("角度2-" + Vector3.Angle(this.transform.forward, target.position - this.transform.position));
#endregion
}
}
上面只能解決出B在不在A的前面或者后面,如果有一個主角,一個boss,boss只能掃描自己扇形位置的區域,怎么搞?

直角邊AB從上面點乘概念知道,就是B在A上的投影
用反三角函式ACos就可以推出兩個向量的角度,然后通過角度就可以跟boss的檢測范圍對比,看是不是在檢測范圍角度內
//1.用單位向量算出點乘結果
dotResult = Vector3.Dot(this.transform.forward, (target.position - this.transform.position).normalized);
//2.用反三角函式得出角度
print("角度-" + Mathf.Acos(dotResult) * Mathf.Rad2Deg);//弧度轉成角度
//Vector3中提供了 得到兩個向量之間夾角的方法
print("角度2-" + Vector3.Angle(this.transform.forward, target.position - this.transform.position));
#endregion
注意這個角度范圍是0到180,怎么分左側右側,看后面講解
//Vector3中提供了 得到兩個向量之間夾角的方法
print("角度2-" + Vector3.Angle(this.transform.forward, target.position - this.transform.position));

分析:
就是A與B的夾角只有小于等于22.5度,才說明B在A的視線范圍內,如果長度是5米,才會發現
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FindEnemy : MonoBehaviour
{
public Transform B;
// Update is called once per frame
void Update()
{
//if(Vector3.Distance(this.transform.position, B.transform.position) <= 5)
//{
// //第一步 算出點乘結果(方向向量)
// float dotResult = Vector3.Dot(this.transform.forward, (B.transform.position - this.transform.position).normalized);
// //第二步 通過反余弦函式 算出 夾角
// if(Mathf.Acos(dotResult) * Mathf.Rad2Deg <= 22.5f)
// {
// print("發現入侵者");
// }
//}
if( Vector3.Distance(this.transform.position, B.transform.position) <= 5 &&
Vector3.Angle(this.transform.forward, B.transform.position - this.transform.position) <= 22.5f)
{
print("發現入侵者");
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/295272.html
標籤:其他
