下载源码 UnityPackage
1.Ray2D
让小球向右发射射线:
Ray2D ray;
void Start()
{
// Ray2D(起点,终点)
ray = new Ray2D(this.transform.position, Vector2.right);
// Debug.DrawLine(起点,终点,颜色,显示时间)
Debug.DrawLine(ray.origin, ray.direction, Color.green, 1000);
}
2.RaycastHit2D(射线检测)
返回鼠标单击对象的名字:
RaycastHit2D hitInfo;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
hitInfo = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if (hitInfo.collider != null)
{
Debug.Log(hitInfo.collider.gameObject.name);
}
}
}