1,射线
(1)Ray射线
A ray is an infinite line starting at origin and going in some direction.
Ray是具有开始点和方向的无穷线。
构造:
Ray ray = new Ray(transform.position, transform.forward);
transform.position为起点, transform.forward为方向。
(2)Raycast射线投射
C# => public static bool Raycast(Vector3 sourcePosition, Vector3 targetPosition, out NavMeshHit hit, int areaMask);
sourcePosition:射线起点
targetPosition:射线终点
hit:保存射线投射位置的属性
areaMask:绘制路径时,位掩码指定NavMesh区域中可以被通过的地方。
(3)RaycastHit射线投射碰撞信息
| barycentricCoordinate | The barycentric coordinate of the triangle we hit. 碰到的三角形的重心坐标。 |
| collider | The Collider that was hit. 碰到的碰撞器。 |
| distance | The distance from the ray's origin to the impact
point. 从射线的原点到触碰点的距离。 |
| lightmapCoord | The uv lightmap coordinate at the impact
point. 在触碰点的UV光照贴图的坐标。 |
| normal | The normal of the surface the ray hit. 射线触碰表面的法线。 |
| point | The impact point in world space where the ray hit the
collider. 在世界坐标空间,射线碰到碰撞器的接触点。 |
| rigidbody | The Rigidbody of the collider that was hit. If the collider is
not attached to a rigidbody then it is
null. 碰到的该碰撞器上的刚体。如果碰撞器上没有附加刚体,那么返回null。 |
| textureCoord | The uv texture coordinate at the impact point. 在触碰点的UV纹理坐标。 |
| textureCoord2 | The secondary uv texture coordinate at the impact
point. 在接触点处的第二套UV纹理坐标。 |
| transform | The Transform of the rigidbody or collider that was
hit. 碰到的该刚体或碰撞器的变换。 |
| triangleIndex | The index of the triangle that was hit. 碰到的三角形的索引。 |
2,克隆
C# => static Object Instantiate(Object original, Vector3 position, Quaternion rotation);C# => static Object
Instantiate(Object original);
Parameters
| original | An existing object that you want to make a copy of. 你想要拷贝的已有对象 |
| position | Position for the new object. 新对象的位置 |
| rotation | Orientation of the new object. 新对象的方向 |
Clones the object original and returns the clone. cl
克隆原始物体并返回克隆物体。
以上引自Unity圣典。
3,画线
using UnityEngine;
using System.Collections;
public class Drawwarnline : MonoBehaviour {
// Use this for initialization
private Vector3 pos1;//第一个点的位置
private Vector3 pos2;//第二个点的位置
private RaycastHit hit;
private Ray ray;
private int num = 0;
public bool ischoose;//是否开始画线
public GameObject warnline;//线模型
void Update()
{
if (Input.GetMouseButtonDown(0))//按下鼠标左键
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);//从主相机投射一条射线到鼠标位置
if (Physics.Raycast(ray,out hit))//在场景中投下可与所有碰撞器碰撞的一条光线。
{
if (hit.collider != null)//碰撞到碰撞器
{
if (!ischoose)
{
pos1 = new Vector3(hit.point.x, hit.point.y, hit.point.z);//记录第一个点坐标
//Debug.Log(pos1);
ischoose = true;
}
else
{
num++;
pos2 = new Vector3(hit.point.x, hit.point.y, hit.point.z);//第二个点坐标
//Debug.Log(pos2);
var distance = Vector3.Distance(pos2, pos1);//两点之间距离,这里var为float类型,使用var,var会自动检测类型
Vector3 direction = (pos2 - pos1).normalized;//要花的线的方向单位向量
GameObject warnobj = GameObject.Instantiate(warnline, (pos2 - pos1) / 2, Quaternion.identity) as GameObject;//克隆警戒线
warnobj.name = "warnline" + num;
warnobj.transform.localScale = new Vector3(100f, 10f, distance * 70);//根据父对象调整大小
warnobj.transform.position = warnobj.transform.position + pos1;//位置
warnobj.transform.forward = direction;
warnobj.transform.parent = GameObject.Find("pwarnline").transform;
//Debug.Log(direction);
ischoose = false;
}
}
}
}
}
}
浙公网安备 33010602011771号