1. 最初のタイプの光線: オブジェクト自体から発生する光線 (このメソッドは、光線コンポーネント Line Renderer をオブジェクトに追加する必要があります)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CubeLine : MonoBehaviour {
LineRenderer line;
// Use this for initialization
void Start () {
line=GetComponent<LineRenderer>();
line.widthMultiplier = 0.08f;//设置射线的宽度
}
// Update is called once per frame
void Update () {
line.SetPosition(0, transform.position);//设置射线的起点
line.SetPosition(1, transform.forward*10.0f);//设置射线的终点
}
}
2. 2 番目のタイプの光線: カメラの光線 (カメラを光線の開始点とする)
注: このレイはタグ値 MainCamera に依存します。タグ値 MainCamera を持つカメラがなければ、このレイは無効です。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraLine : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);//定义射线,以鼠标的位置为终点
Debug.DrawLine(mouseRay.origin, mouseRay.direction*10.0f,Color.red);//描绘射线,只在Scene视图中看得见
RaycastHit hitInfo;
if (Physics.Raycast(mouseRay, out hitInfo))//检测带有碰撞体的物体,其射线终点是与带有碰撞体的物体的交汇处
{
print(hitInfo.collider.name);//输出射线碰撞到的物体的名字
}
}
}
結論: 抱きしめる木は髪の毛の先に生まれ、9 階建てのプラットフォームは土の山から始まり、千里の道も一歩から始まります。