一、必備知識點 Vector3 基礎
Vector3主要用來標識三維坐標系中的 一個點 或 一個向量
Vector3的本質是一個Unity提供好的結構體
現有:

Lesson6腳本的代碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lesson6 : MonoBehaviour
{
void Start()
{
//Vector3的宣告
//方法一:不new (它是一個值型別,可以不new)
Vector3 v;
v.x = 10;
v.y = 10;
v.z = 10;
//方法二:用無參構造宣告
Vector3 v2 = new Vector3();
v2.x = 10;
v2.y = 10;
v2.z = 10;
//方法三:它還有多載的有參建構式
// 不傳z的值,z會默認為0
Vector3 v3 = new Vector3(10, 10);
// 傳三個值,分別是x、y、z (最常用的宣告方式)
Vector3 v4 = new Vector3(10, 10, 10);
//Vector3的基本計算
//運算后會有一個Vector3型別的回傳值
Vector3 v5 = new Vector3(3, 3, 3);
Vector3 v6 = new Vector3(2, 2, 2);
//1.加/減
//計算結果為v5和v6的x、y、z分別相加/減
print(v5 + v6);
print(v5 - v6);
//2.乘/除
//v5的x、y、z分別 * 10
print(v5 * 10);
//v5的x、y、z分別 / 2
print(v5 / 2);
//常用的預設點/向量
print(Vector3.zero); //對應點(0, 0, 0)
//print(Vector3.one); //對應點(0, 0, 0)
print(Vector3.right); //對應點(1, 0, 0)
print(Vector3.left); //對應點(-1, 0, 0)
print(Vector3.forward); //對應點(0, 0, 1)
print(Vector3.back); //對應點(0, 0, -1)
print(Vector3.up); //對應點(0, 1, 0)
print(Vector3.down); //對應點(0, -1, 0)
//補充:計算兩個點之間的距離的方法
//只需傳入兩個點,就能自動計算出兩點的距離
float f = Vector3.Distance(v5, v6);
}
}
運行:

二、位置相關
現有:

Lesson6腳本的代碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lesson6 : MonoBehaviour
{
void Start()
{
//1.相對世界坐標系的位置(與父GameObject物件無關)
print(this.transform.position);
//2.相對于父GameObject物件的位置
// 如果想要以Inspector面板上的坐標數值為準來進行設定
// 那一定是通過LocalPosition來進行設定
print(this.transform.localPosition);
//補充:他們兩個有兩種情況會出現面板數值一樣
// 1.物件沒有父物件
// 2.父物件的坐標 就是世界坐標系(0, 0, 0)點
//3.位置的賦值
//注意:無法單獨對x、y、z其中一個值進行修改,只能使用Vector3進行整體修改
//this.transform.position.x = 10; // X
this.transform.position = new Vector3(10, 10, 10); // √
// 對然不能直接改transform的x、y、z,但是Vector的是可以直接改的
// 用Vector把位置取出來,
Vector3 v = this.transform.localPosition;
// 然后再修改
v.x = 15;
//4.得到物件當前的朝向相關
// 1.物件當前的面朝向
print(this.transform.forward);
// 2.物件當前頭頂的朝向
print(this.transform.up);
// 3.物件當前的右手邊朝向
print(this.transform.right);
// 4.back、left、down同理...
}
}
三、位移相關
坐標系下的位移計算公式:路程 = 方向 * 速度 * 時間
(因為是在多為坐標系中移動,所以要乘一個方向)
現有:

Lesson6腳本的代碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lesson6 : MonoBehaviour
{
void Update()
{
//方式一:自己手動計算
//用當前的位置 + 我要動多長距離 得出最終所在的位置
//這樣寫 物件會始終朝自身坐標系的正前方移動
this.transform.position += this.transform.forward * 1 * Time.deltaTime; //(方向 * 速度 * 時間)
//這樣寫 物件會朝著世界坐標系的前方(z軸正方向)移動
this.transform.position += Vector3.forward * 1 * Time.deltaTime;
//方法二:使用API計算
//一個專門管位移的方法:Translate
//引數1 表示位移了多少 (方向 * 速度 * 時間)
//引數2 表示相對坐標系 (不寫默認相對本地坐標系)
//1.這樣寫 始終朝著世界坐標系的 Z軸移動
//(因為Vector3.forward得到的僅僅是個方向(0, 0, 1)而已)
this.transform.Translate(Vector3.forward * 1 * Time.deltaTime, Space.World);
//2.這樣寫 始終朝著自己的面朝想移動 (常用)
//(因為this.transform.forward得到的是世界坐標系下物件自己的面朝向)
this.transform.Translate(this.transform.forward * 1 * Time.deltaTime, Space.World);
//3.這樣寫 始終朝著自己的面朝想移動 (常用)
this.transform.Translate(Vector3.forward * 1 * Time.deltaTime, Space.Self);
//4.這樣寫 不對!! X
//(因為this.transform.forward得到的是世界坐標系下物件自己的面朝向,而引數2又是自己的坐標系,兩者重疊后 方向將錯亂)
this.transform.Translate(this.transform.forward * 1 * Time.deltaTime, Space.Self);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/500119.html
標籤:其他
上一篇:【Unity學習筆記】用Transform類控制物體的位置and位移/Vector3基礎
下一篇:15. 三數之和
