unity(유니티) vector 함수 전부 알기

2022. 8. 3. 23:05유니티 unity

2022.2 기준

https://docs.unity3d.com/2022.2/Documentation/ScriptReference/Vector3.html

 

Unity - Scripting API: Vector3

This structure is used throughout Unity to pass 3D positions and directions around. It also contains functions for doing common vector operations. Besides the functions listed below, other classes can be used to manipulate vectors and points as well. For e

docs.unity3d.com

 

 

vector3.back

Vector3(0,0,-1)

vector3.down

Vector3(0,-1,0)

vector3.forward

Vector3(0,0,1)

vector3.left

Vector3(-1,0,0)

vector3.negativeInfinity

음수 무한대 vector3(float.negativeInfinity,float.negativeInfinity,float.negativeInfinity)

vector3.one

Vector3(1,1,1)

vector3.positiveInfinity

양수 무한대 vector3(float.positiveInfinity,float.positiveInfinity,float.positiveInfinity)

vector3.right

Vector3(1,0,0)

vector3.up

Vector3(0,1,0)

vector3.zeo

Vector3(0,0,0)

 

 

vector3.magnitude

읽기 전용 길이를 반환합니다 float

vector3.normalized

정규화시킵니다. 크기가 1인 벡터를 반환

vector3.sqrMagnitude

벡터의 길이 제곱 반환

 

 

 

 

 

 

 

vector3.Angle // public static float Angle(Vector3 from, Vector3 to);

두 벡터의 사이의 각도를 반환합니다. (반환되는 각도는 항상 0~180도 사이입니다)

vector3.ClampMagnitude // public static Vector3 ClampMagnitude(Vector3 vector, float maxLength);

maxLength길이를 넘지 않는 벡터를 반환시킵니다.

 

 

public static Vector3 ClampMagnitude(Vector3 vector, float maxLength)
{
  float sqrMagnitude = vector.sqrMagnitude;
  if ((double) sqrMagnitude <= (double) maxLength * (double) maxLength)
    return vector;
  float num1 = (float) Math.Sqrt((double) sqrMagnitude);
  float num2 = vector.x / num1;
  float num3 = vector.y / num1;
  float num4 = vector.z / num1;
  return new Vector3(num2 * maxLength, num3 * maxLength, num4 * maxLength);
}

 

 

void Start()
    {
        Vector3 a = new Vector3(50, 0, 0);
        Vector3 b = new Vector3(15, 0, 0);
        Vector3 c = new Vector3(10, 0, 0);
        Vector3 d = new Vector3(3, 0, 0);
        Debug.Log(Vector3.ClampMagnitude(a, 10));
        //결과 (10,0,0) 
        Debug.Log(Vector3.ClampMagnitude(b, 10));
        //결과 (10,0,0) 
        Debug.Log(Vector3.ClampMagnitude(c, 10));
        //결과 (10,0,0) 
        Debug.Log(Vector3.ClampMagnitude(d, 10));
        //결과 (3,0,0) 
    }

 

vector3.Cross // public static Vector3 Cross(Vector3 lhs, Vector3 rhs);

두 벡터의 수직벡터를 반환합니다.

 

https://docs.unity3d.com/2022.2/Documentation/ScriptReference/Vector3.Cross.html

public static Vector3 Cross(Vector3 lhs, Vector3 rhs) => new Vector3((float) ((double) lhs.y * (double) rhs.z - (double) lhs.z * (double) rhs.y), (float) ((double) lhs.z * (double) rhs.x - (double) lhs.x * (double) rhs.z), (float) ((double) lhs.x * (double) rhs.y - (double) lhs.y * (double) rhs.x));

 

 

 

vector3.Distance // public static float Distance(Vector3 a, Vector3 b);

두 벡터의 사이의 길이를 반환합니다.

public static float Distance(Vector3 a, Vector3 b)
{
  float num1 = a.x - b.x;
  float num2 = a.y - b.y;
  float num3 = a.z - b.z;
  return (float) Math.Sqrt((double) num1 * (double) num1 + (double) num2 * (double) num2 + (double) num3 * (double) num3);
}

vector3.Dot // public static float Dot(Vector3 lhs, Vector3 rhs);

두벡터의 내적을 반환합니다.

 

public static float Dot(Vector3 lhs, Vector3 rhs) => (float) ((double) lhs.x * (double) rhs.x + (double) lhs.y * (double) rhs.y + (double) lhs.z * (double) rhs.z);

 

 

vector3.Lerp // public static Vector3 Lerp(Vector3 a, Vector3 b, float t);

두 벡터의 선형보간한 값을 반환합니다.

 

public static Vector3 Lerp(Vector3 a, Vector3 b, float t)
{
  t = Mathf.Clamp01(t);
  return new Vector3(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t, a.z + (b.z - a.z) * t);
}

vector3.LerpUnclamped

두 벡터의 선형보간한 값을 반환합니다(최소, 최대 제한이 없음)

 

public static Vector3 LerpUnclamped(Vector3 a, Vector3 b, float t) => new Vector3(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t, a.z + (b.z - a.z) * t);

vector3.Max // public static Vector3 Max(Vector3 lhs, Vector3 rhs);

두 벡터 요소중 가장 큰 수를 모아서 하나의 벡터로 만듭니다.

 

public Vector3 a = new Vector3(1, 2, 3);
public Vector3 b = new Vector3(4, 3, 2);

    void Example()
    {
        print(Vector3.Max(a, b)); 
        // 결과 (4.0f, 3.0f, 3.0f)
    }
public static Vector3 Max(Vector3 lhs, Vector3 rhs) => new Vector3(Mathf.Max(lhs.x, rhs.x), Mathf.Max(lhs.y, rhs.y), Mathf.Max(lhs.z, rhs.z));

 

 

 

vector3.Min // public static Vector3 Min(Vector3 lhs, Vector3 rhs);

두벡터 요소중 가장 작은 수를 모아서 하나의 벡터로 만듭니다.

 

public Vector3 a = new Vector3(1, 2, 3);
public Vector3 b = new Vector3(4, 3, 2);

    void Example()
    {
        print(Vector3.Min(a, b));    
        //결과 (1.0f, 2.0f, 2.0f)
    }

 

public static Vector3 Min(Vector3 lhs, Vector3 rhs) => new Vector3(Mathf.Min(lhs.x, rhs.x), Mathf.Min(lhs.y, rhs.y), Mathf.Min(lhs.z, rhs.z));

 

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

asdasd

current 벡터를 target 벡터의 위치로 maxDistanceDelta 속도만큼 이동합니다

(target 거리보다 더 멀리 이동하지 않습니다.)

 

public static Vector3 MoveTowards(
  Vector3 current,
  Vector3 target,
  float maxDistanceDelta)
{
  float num1 = target.x - current.x;
  float num2 = target.y - current.y;
  float num3 = target.z - current.z;
  float d = (float) ((double) num1 * (double) num1 + (double) num2 * (double) num2 + (double) num3 * (double) num3);
  if ((double) d == 0.0 || (double) maxDistanceDelta >= 0.0 && (double) d <= (double) maxDistanceDelta * (double) maxDistanceDelta)
    return target;
  float num4 = (float) Math.Sqrt((double) d);
  return new Vector3(current.x + num1 / num4 * maxDistanceDelta, current.y + num2 / num4 * maxDistanceDelta, current.z + num3 / num4 * maxDistanceDelta);
}

 

vector3.Normalize // public static Vector3 Normalize(Vector3 value);

정규화시킵니다. 벡터의 크기를 1로 만듭니다 

정규화되면 벡터는 같은 방향을 유지하지만 길이는 1.0입니다.

 

public static Vector3 Normalize(Vector3 value)
{
  float num = Vector3.Magnitude(value);
  return (double) num > 9.99999974737875E-06 ? value / num : Vector3.zero;
}

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

벡터들을 정규화하고 서로 수직으로 만듭니다.

 

vector3.Project // public static Vector3 Project(Vector3 vector, Vector3 onNormal);

벡터를 다른 벡터에 투영합니다.

vector3.ProjectOnPlane

https://lalyns.tistory.com/7

 

public static Vector3 Project(Vector3 vector, Vector3 onNormal)
{
  float num1 = Vector3.Dot(onNormal, onNormal);
  if ((double) num1 < (double) Mathf.Epsilon)
    return Vector3.zero;
  float num2 = Vector3.Dot(vector, onNormal);
  return new Vector3(onNormal.x * num2 / num1, onNormal.y * num2 / num1, onNormal.z * num2 / num1);
}

vector3. ProjectOnPlane// public static Vector3 ProjectOnPlane(Vector3 vector, Vector3 planeNormal);

평면에 수직인 법선으로 정의된 평면에 벡터를 투영합니다.

 

public static Vector3 ProjectOnPlane(Vector3 vector, Vector3 planeNormal)
{
  float num1 = Vector3.Dot(planeNormal, planeNormal);
  if ((double) num1 < (double) Mathf.Epsilon)
    return vector;
  float num2 = Vector3.Dot(vector, planeNormal);
  return new Vector3(vector.x - planeNormal.x * num2 / num1, vector.y - planeNormal.y * num2 / num1, vector.z - planeNormal.z * num2 / num1);
}

https://www.youtube.com/watch?v=Q6LC1ubawCo&t=1s 

 

 

vector3.Reflect // public static Vector3 Reflect(Vector3 inDirection, Vector3 inNormal);

 

반사각을 반환합니다.

 

public static Vector3 Reflect(Vector3 inDirection, Vector3 inNormal)
{
  float num = -2f * Vector3.Dot(inNormal, inDirection);
  return new Vector3(num * inNormal.x + inDirection.x, num * inNormal.y + inDirection.y, num * inNormal.z + inDirection.z);
}

 

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

movetowards와 유사합니다 벡터의 위치가 아닌 방향으로 처리됩니다.

표적의 방향을 향해서 오버되지 않고 정확하게 설정됩니다.

vector3.Scale // public static Vector3 Scale(Vector3 a, Vector3 b);

두 벡터를 곱합니다

 

void Example()
    {
        print(Vector3.Scale(new Vector3(1, 2, 3), new Vector3(2, 3, 4)));
        //결과 (2,6,12)
    }
public static Vector3 Scale(Vector3 a, Vector3 b) => new Vector3(a.x * b.x, a.y * b.y, a.z * b.z);

vector3.SignedAngle //public static float SignedAngle(Vector3 from, Vector3 to, Vector3 axis);

각도를 반환합니다 (-180~180도)

from 에서

to 에게

axis 축

보통 두 벡터 사이의 각도를 구할 때 사용합니다.

 

public Transform target;//원형

void Update()
{
    Vector3 targetDir = target.position - transform.position;
    Vector3 forward = transform.forward;
    float angle = Vector3.SignedAngle(forward, targetDir, Vector3.up);
    print(angle);
    //각도

}

 

 

 

 

 

 

public static float SignedAngle(Vector3 from, Vector3 to, Vector3 axis)
{
  float num1 = Vector3.Angle(from, to);
  float num2 = (float) ((double) from.y * (double) to.z - (double) from.z * (double) to.y);
  float num3 = (float) ((double) from.z * (double) to.x - (double) from.x * (double) to.z);
  float num4 = (float) ((double) from.x * (double) to.y - (double) from.y * (double) to.x);
  float num5 = Mathf.Sign((float) ((double) axis.x * (double) num2 + (double) axis.y * (double) num3 + (double) axis.z * (double) num4));
  return num1 * num5;
}

 

vector3.Slerp // public static Vector3 Slerp(Vector3 a, Vector3 b, float t);

구면선형보간

 

https://www.youtube.com/watch?v=AzmVVPWao8U 

 

public static Vector3 Slerp(Vector3 a, Vector3 b, float t)
{
  Vector3 ret;
  Vector3.Slerp_Injected(ref a, ref b, t, out ret);
  return ret;
}

vector3.SlerpUnclamped

구면선형보간 제한치 없음

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

target 벡터를 향해서 이동합니다 (부드러운 이동) 일반적으로 카메라 이동할 때 사용

 

 

 

 

public static Vector3 SmoothDamp(
  Vector3 current,
  Vector3 target,
  ref Vector3 currentVelocity,
  float smoothTime)
{
  float deltaTime = Time.deltaTime;
  float maxSpeed = float.PositiveInfinity;
  return Vector3.SmoothDamp(current, target, ref currentVelocity, smoothTime, maxSpeed, deltaTime);
}