Unity ScriptingAPI Vector3學習

來源:互聯網
上載者:User

連結:http://docs.unity3d.com/ScriptReference/Vector3.html Vector3

struct in UnityEngine 描述

Representation of 3D vectors and points.
表示3D向量或點。

This structure is used throughout Unity to pass 3D positions and directions around. It also contains functions for doing common vector operations.
此結構用來貫穿整個Unity3D位置和方向的轉換。也包括一般向量操作方法。

Besides the functions listed below, other classes can be used to manipulate vectors and points as well. For example the Quaternion and the Matrix4x4 classes are useful for rotating or transforming vectors and points.
除了以下列出的方法,其他類也有操作向量和位置的方法。例如Quaternion和Matrix4x4…… 靜態變數 back 代表(0,0,-1) down 代表(0,-1,0) forward 代表(0,0,1) left 代表(-1,0,0) one 代表(1,1,1)單位向量 right 代表(1,0,0) up 代表(0,1,0) zero 代表(0,0,0)
普通變數 magnitude 返迴向量的長度(模),唯讀 normalized 返回該向量對應的單位向量, 不是one,唯讀 sqrManitude 返回長度的平方,唯讀 this[int] 下標[0][1][2]代表x,y,z x y z 建構函式

public Vector3(float x, float y, float z); 普通方法

public void Set(float new_x, float new_y, float new_z);

Set函數

public string ToString();
public string ToString(string format);

返迴向量對應的格式化字串 操作符

operator - 向量相減
operator != 向量判等,返回bool
operator * 數乘
operator / 數除
operator + 向量相加
operator == 向量判等,返回bool 靜態方法(常用)

public static float Angle(Vector3 from, Vector3 to);

返回兩向量間角度,不是弧度,數值不超過180。

public static Vector3 ClampMagnitude(Vector3 vector, float maxLength);

限制向量vector長度為maxLength,若vector.magnitude > maxLegth, 傳回值叫vector縮小

public static float Distance(Vector3 a, Vector3 b);

Distance函數返迴向量的距離,為(a-b).magnitude

public static float Dot(Vector3 lhs, Vector3 rhs);

兩向量點乘,為(|a| * |b| * cos(angle))

public static Vector3 Lerp(Vector3 from, Vector3 to, float t);

線性插值兩個向量,t=0,返回from,t=1,返回to,一般採用Vector3.Lerp(startPoint, endPoint, Time.deltaTime * xx)實現平滑移動

public static Vector3 Max(Vector3 lhs, Vector3 rhs);
public static Vector3 Min(Vector3 lhs, Vector3 rhs);

返回兩向量x,y,z對應座標的最大或者最小值

public static Vector3 MoveTowards(Vector3 current, Vector3 target, float maxDistanceDelta);

current向 target移動,maxDistanceDelta為每秒的距離

public void Normalize();

取單位向量。

public static Vector3 Project(Vector3 vector, Vector3 onNormal);

將vector投影到onNormal上。

public static void OrthoNormalize(ref Vector3 normal, ref Vector3 tangent);

取normal的單位向量,然後取tangent的單位向量,並與normal正交。

public static void OrthoNormalize(ref Vector3 normal, ref Vector3 tangent, ref Vector3 binormal);

去normal的單位向量,然後去tangent的單位向量,並與normal正交;去binormal單位向量,並與normal和tangent分別正交。

public static Vector3 ProjectOnPlane(Vector3 vector, Vector3 planeNormal);

投影vector到以planeNormal為法向量的平面上。

public static Vector3 Reflect(Vector3 inDirection, Vector3 inNormal);

反射indirection,以inNormal為法向量的平面為反射面。

public static Vector3 RotateTowards(Vector3 current, Vector3 target, float maxRadiansDelta, float maxMagnitudeDelta);

與MoveTowards不同之處在於這裡current代表的是方向而非點。

public static Vector3 Scale(Vector3 a, Vector3 b);

(1,2,3)*(2,3,4) = (2,6,12)

public static Vector3 Slerp(Vector3 from, Vector3 to, float t);

球面插值

public static Vector3 SmoothDamp(Vector3 current, Vector3 target, ref Vector3 currentVelocity, float smoothTime, float maxSpeed = Mathf.Infinity, float deltaTime = Time.deltaTime);

平滑阻尼,向量由一些像彈簧阻尼器函數平滑,這將永遠不會超過。最常見的用途是平滑跟隨相機,例如下:

using UnityEngine;using System.Collections;public class example : MonoBehaviour {    public Transform target;    public float smoothTime = 0.3F;    private Vector3 velocity = Vector3.zero;    void Update() {        Vector3 targetPosition = target.TransformPoint(new Vector3(0, 5, -10));        transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.