unity學習筆記(1)

2022-09-19 12:36:15 字數 3837 閱讀 6723

1.想要物體被重力影響則需為其加入剛體元件 rigidbody

2.如果想要兩個類繫結的話,最簡單方便的方法是建立乙個public gameobject類,在unity介面繫結對應的物體。

3.vector3三維向量:表示3d的向量和點。包含位置、方向(朝向)、尤拉角的資訊,也包含做些普通向量運算的函式。

4.transform類

成員變數:

position:在世界空間座標transform的位置。

localposition:相對於父級的變換的位置。如果該變換沒有父級,那麼等同於transform.position。

eulerangles:世界座標系中的旋轉(尤拉角)。

localeulerangles:相對於父級的變換旋轉角度。

right:世界座標系中的右方向。(世界空間座標變換的紅色軸。也就是x軸。)

up:世界座標系中的上方向。(在世界空間座標變換的綠色軸。也就是y軸。)

forward:世界座標系中的前方向。(在世界空間座標變換的藍色軸。也就是z軸。)

rotation:世界座標系中的旋轉(四元數)。

localrotation:相對於父級的變換旋轉角度。

localscale:相對於父級的縮放比例。

parent:父物件transform元件。

worldtolocalmatrix:矩陣變換的點從世界座標轉為自身座標(唯讀)。

localtoworldmatrix:矩陣變換的點從自身座標轉為世界座標(唯讀)。

root:物件層級關係中的根物件的transform元件。

childcount:子物件數量。

lossyscale:全域性縮放比例(唯讀)。

成員方法:

1)translate,用來移動物體的函式,非常常用的乙個函式。

public void translate (translation : vector3, relativeto : space = space.self) : void

把物體向translation方向移動,距離為translation.magnitude。 relativeto表示這個移動的參考座標系。

public void translate (x : float, y : float, z : float, relativeto : space = space.self) : void

同上public void translate (translation : vector3, relativeto : transform) : void

如果relativeto 不是null,則以目標物體relativeto的自身軸向作為參考座標系。

public void translate (x : float, y : float, z : float, relativeto : transform) : void

同上指令碼:

var speed=30;

//將物體以30公尺每秒的速度向前移動。

trasform.translate(vector3.forwardspeedtime.deltatime);

2)rotate,用來旋轉物體的函式,非常常用,在知道需要旋轉的角度的情況下。如果要讓物體旋轉到指定位置,需要搭配quaternion來使用。

public void rotate (eulerangles : vector3, relativeto : space = space.self) : void

旋轉eulerangles度(3個軸向分別旋轉),以relativeto為參考座標系

public void rotate (xangle : float, yangle : float, zangle : float, relativeto : space = space.self) : void

同上public void rotate (axis : vector3, angle : float, relativeto : space = space.self) : void

以axis為軸旋轉angle度,以relativeto為參考座標系

3)rotatearound 讓物體以某一點為軸心成圓周運動。

public void rotatearound (point : vector3, axis : vector3, angle : float) : void

讓物體以point為中心,繞axis為軸向旋轉angle度。保持原來與point的距離。

4)lookat 讓物體的z軸看向目標物體

public void lookat (target : transform, worldup : vector3 = vector3.up) : void

讓物體的z軸看向target的位置,並以worldup為y軸指向方向。

public void lookat (worldposition : vector3, worldup : vector3 = vector3.up) : void

讓物體看向worldposition

5)transformdirection

public vector3 transformdirection (direction : vector3) : vector3

返回以物體自身為座標軸的向量direction在世界座標中的朝向向量。

public vector3 transformdirection (x : float, y : float, z : float) : vector3

同上6)inversetransformdirection

public vector3 inversetransformdirectiontransformdirection (direction : vector3) : vector3

public vector3 inversetransformdirectiontransformdirection (x : float, y : float, z : float) : vector3

與transformdirection相反,從世界座標轉換到自身相對座標。

7)transformpoint

public vector3 transformpoint (position : vector3) : vector3

public vector3 transformpoint (x : float, y : float, z : float) : vector3

把乙個點從自身相對座標轉換到世界座標

8)inversetransformpoint

public vector3 inversetransformpoint (position : vector3) : vector3

public vector3 inversetransformpoint (x : float, y : float, z : float) : vector3

把乙個點從時間座標轉換到自身座標的位置。

9)detachchildren

public void detachchildren () : void

把自身所有的子物體的父物體都設成世界,也就是跟自己的所有子物體接觸父子關係。

10)find

public transform find (name : string) : transform

找到乙個名字是name的物體並返回

如果沒有找到則返回null。如果字串被/隔離,函式則會像檔案路徑一樣逐級下查。

// the magical rotating finger

function update()

11)ischildof

public bool ischildof (parent : transform) : bool

如果物體是parent的父子層級關係下的一員,返回true;

unity學習筆記1

unity後面作為遊戲。什麼我就不說了unity進入之前會有乙個3d和2d視角的選擇,3d就選3d,2d就選2d,這個沒有什麼說的。選單項我們先不管,有四個視窗 他們分別是scene,hierarchy,game,project視窗。game視窗就是你模擬遊戲執行時看到的視窗 scene視窗是你實時...

Unity學習筆記 1

assets 資源 模型,檔案,聲音資源,指令碼 library 庫 shader 內建指令碼 快取檔案 projectsetting 工程設定 各種各樣的設定,例如聲音設定,開啟工程按此 temp 臨時資料夾 臨時檔案存放處,關閉unity刪除其檔案 scene gameobject遊戲物體 元件...

Unity學習筆記(1)

transform transform是gameobject的乙個預設的元件,其包含著該物件的幾種屬性,座標 position 以及旋轉角度 rotation 和尺寸 scale transform的子物件 transform.position 型別為vector3,其值為該物件的世界座標值 tra...