Vector3

来源:互联网 发布:匡恩网络 待遇 编辑:程序博客网 时间:2024/05/16 05:14

1.Transform.forward:

非静态属性;

返回类型为Vector3;

长度(magnitude)总为1;

总与物体的正前方方向保持一致。

 

2.Transform.TransformDirection

 Vector3 TransformDirection(Vector3 direction);

将向量从局部空间转换到世界空间。不受物体的position和scale影响

返回的向量长度与原向量保持一致。

transform.TransformDirection(Vector3.forward)与transform.forward相同。但前者不仅能将上下左右的方向转换到世界空间,还能将任意方向转换到世界空间,比前者用法宽泛的多。

 Transform.InverseTransformDirection
 Vector3 InverseTransformDirection(Vector3 direction);

与TransformDirection作用相反,从世界方向转换为局部方向,同样不受物体的scale影响

 Transform.TransformPoint
 Vector3 TransformPoint(Vector3 position)

将点的坐标从局部空间转换到世界空间,受物体的scale影响。如何受影响?

 Transform.InverseTransformPoint
 Vector3 InverseTransformPoint(Vector3 position)

与TransformPoint作用相反,将点的坐标从世界空间转换到局部空间,同样受到物体的scale的影响。

 

3.Vector3.Angle

静态函数;

返回两个向量之间的角度,类型为float;

这个角度大于等于0度,小于等于180度;即总是取向量相交后形成的角中较小的那一个;

 void OnTriggerStay (Collider other){  // If the player has entered the trigger sphere...        if(other.gameObject == player)        {   // By default the player is not in sight.   playerInSight = false;      // Create a vector from the enemy to the player and store the angle between it and forward.   Vector3 direction = other.transform.position - transform.position;   float angle = Vector3.Angle(direction, transform.forward);      // If the angle between forward and where the player is, is less than half the angle of view...   if(angle < fieldOfViewAngle * 0.5f)   {    RaycastHit hit;        // ... and if a raycast towards the player hits something...    if(Physics.Raycast(transform.position + transform.up, direction.normalized, out hit, col.radius))    {     // ... and if the raycast hits the player...     if(hit.collider.gameObject == player)     {      // ... the player is in sight.      playerInSight = true;            // Set the last global sighting is the players current position.      lastPlayerSighting.position = player.transform.position;     }    }   }

}

这个脚本的用途:由于enemy有一定的视野范围(角度),当player进入enemy的碰撞区域时,检测player是否在enemy的视野范围内。

 

4.Vector3.normalized

非静态属性;

返回类型为Vector3;

返回的向量与原向量的方向相同,长度为1;原向量不变;

若原向量太小,则返回零向量;

Vector3.Normalized()

非静态函数;

返回类型为void;

使原向量改变,长度变为1,方向与原向量相同;

若原向量太小,则使原向量为零向量;

 

5.Transform.Lookat

 void LookAt(Transform target, Vector3 worldUp = Vector3.up);
使物体的Z轴正方向(正前方)指向目标,至于其他两个轴,由于z轴正方向已定,所以这两个轴可在与z轴平面上任意旋转。当具体怎么旋转,由第二个参数worldUp来指定。


Then it rotates the transform to point its up direction vector in the direction hinted at by theworldUp vector. If you leave out theworldUp parameter, the function will use the world y axis.worldUp is only a hint vector. The up vector of the rotation will only match theworldUp vector if the forward direction is perpendicular toworldUp

 

6.Vector3.magnitude

返回向量的长度。也就是x*x+y*y+z*z的平方根,也就是Vector3.Dot((x,y,z))

Vector3.sqrMagnitude

返回x*x+y*y+z*z,只是比magnitude少了一次开平方,也就是Mathf.Sqrt运算

由于Mathf.Sqrt开平方运算较复杂,执行耗时,所以,如果只是想比较向量长度或距离的话,请使用sqrMagnitude

 

0 0
原创粉丝点击