Unity 四、常用脚本(五)Vector
Vector2
Representation of 2D vectors and points.表示二维向量和点。
属性
public float x;
摘要:X component of the vector.向量的X分量。
public float y;
摘要:Y component of the vector.向量的Y分量。
public Vector2(float x, float y);
摘要: Constructs a new vector with given x, y components.用给定的x, y分量构造一个新的向量。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
void Start ()
{
Vector2 vector2 = new Vector2(10,20);
Debug.Log(vector2.x);
Debug.Log(vector2.y);
}
}
public static Vector2 right { get; }
摘要:Shorthand for writing Vector2(1, 0). Vector2(1, 0)的简写。表示物体右方向。
public static Vector2 left { get; }
摘要:Shorthand for writing Vector2(-1, 0).Vector2(-1, 0)的简写。表示物体左方向。
public static Vector2 down { get; }
摘要:Shorthand for writing Vector2(0, -1).Vector2(0, -1)的简写。表示物体下方向。
public static Vector2 up { get; }
摘要:Shorthand for writing Vector2(0, 1). Vector2(0, 1)的简写。表示物体上方向。
public static Vector2 one { get; }
摘要: Shorthand for writing Vector2(1, 1).Vector2(1, 1)的简写。
public static Vector2 zero { get; }
摘要: Shorthand for writing Vector2(0, 0).Vector2(0, 0)的简写。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
void Start ()
{
Debug.Log(Vector2.right);
Debug.Log(Vector2.left);
Debug.Log(Vector2.down);
Debug.Log(Vector2.up);
Debug.Log(Vector2.one);
Debug.Log(Vector2.zero);
}
}
public Vector2 normalized { get; }
摘要:Returns this vector with a magnitude of 1 (Read Only). 返回大小为1的向量(只读)。规范化
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
void Start ()
{
Vector2 vector2 = new Vector2(0,5);
Debug.Log(vector2.normalized);
}
}
当规格化后,向量保持同样的方向,但是长度变为1.0。
注意,当前向量是不改变的,并且返回一个新的规范化的向量。
如果你想规范当前的向量,使用Normalize函数。
如果这个向量太小而不能被规范化,一个零向量将会被返回。
public float sqrMagnitude { get; }
摘要:Returns the squared length of this vector (Read Only).返回这个向量长度的平方(只读)。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
void Start ()
{
Vector2 vector2 = new Vector2(0,5);
Debug.Log(vector2.sqrMagnitude);
}
}
public float magnitude { get; }
摘要: Returns the length of this vector (Read Only).返回此向量的长度(只读)。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
void Start ()
{
Vector2 vector2 = new Vector2(0,5);
Debug.Log(vector2.magnitude);
}
}
方法
public static float Angle(Vector2 from, Vector2 to);
摘要:Returns the unsigned angle in degrees between from and to.返回两向量的角。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
void Start ()
{
float f = Vector2.Angle(new Vector2(0,0),new Vector2(0,1));
Debug.Log(f);
}
}
public static Vector2 ClampMagnitude(Vector2 vector, float maxLength);
摘要: Returns a copy of vector with its magnitude clamped to maxLength.返回一个向量的副本,最大不超过maxLength所指示的长度。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
void Start ()
{
Vector2 v = Vector2.ClampMagnitude(new Vector2(0,5),3f);
Debug.Log(v);
}
}
public static float Distance(Vector2 a, Vector2 b);
摘要:Returns the distance between a and b.返回两个向量的距离。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
void Start ()
{
float f = Vector2.Distance(new Vector2(0,1), new Vector2(1, 0));
Debug.Log(f);
}
}
public static float Dot(Vector2 lhs, Vector2 rhs);
摘要:Dot Product of two vectors. 两个向量的点积。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
void Start ()
{
float f = Vector2.Dot(new Vector2(0,1), new Vector2(1, 0));
Debug.Log(f);
}
}
public static Vector2 Lerp(Vector2 a, Vector2 b, float t);
摘要:Linearly interpolates between vectors a and b by t. 用t在向量a和b之间做线性插值。
public static Vector2 LerpUnclamped(Vector2 a, Vector2 b, float t);
摘要:Linearly interpolates between vectors a and b by t. 用t在向量a和b之间做线性插值。
public static Vector2 Max(Vector2 lhs, Vector2 rhs);
摘要:Returns a vector that is made from the largest components of two vectors.返回由两个向量的最大分量构成的向量。
public static Vector2 Min(Vector2 lhs, Vector2 rhs);
摘要:Returns a vector that is made from the smallest components of two vectors. 返回由两个向量的最小分量构成的向量。
public static Vector2 MoveTowards(Vector2 current, Vector2 target, float maxDistanceDelta);
摘要:Moves a point current towards target. 从当前位置以一定速度向目标移动。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
void Start ()
{
}
void Update()
{
gameObject.transform.position=Vector2.MoveTowards(gameObject.transform.position, new Vector2(0,10), 0.1f);
}
}
public static Vector2 Reflect(Vector2 inDirection, Vector2 inNormal);
摘要:Reflects a vector off the vector defined by a normal.从法线定义的向量上反射出一个向量。
public static Vector2 Scale(Vector2 a, Vector2 b);
摘要:Multiplies two vectors component-wise.按分量乘两个向量。
public static float SignedAngle(Vector2 from, Vector2 to);
摘要:Returns the signed angle in degrees between from and to.返回带符号的角度(以from和to之间的角度为单位)。
待续。。。