首页 > 代码库 > Unity-Physics.Raycast
Unity-Physics.Raycast
关于API看一下链接
http://game.ceeger.com/Script/Physics/Physics.Raycast.html
? static function Raycast (origin : Vector3, direction : Vector3, out hitInfo : RaycastHit, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool
Parameters参数
- originThe starting point of the ray in world coordinates.
在世界坐标,射线的起始点。 - directionThe direction of the ray.
射线的方向。 - distanceThe length of the ray
射线的长度。 - hitInfoIf true is returned, hitInfo will contain more information about where the collider was hit (See Also: RaycastHit).
如果返回true,hitInfo将包含碰到器碰撞的更多信息。 - layerMaskA Layer mask that is used to selectively ignore colliders when casting a ray.
只选定Layermask层内的碰撞器,其它层内碰撞器忽略。
1.注意:如果从一个球型体的内部到外部用光线投射,返回为假
2.layerMask层的选择和屏蔽
首先 看第一个 如果一个点在碰撞体内部,向外部射,
先写一个脚本
using UnityEngine; using System.Collections; public class Demo2 : MonoBehaviour { public float XStartPos = 0f; public float YStartPos = 0f; public float ZStartPos = 0f; public float XEndPos = 0f; public float YEndPos = 0f; public float ZEndPos = 0f; private Vector3 startPos; private Vector3 endPos; private Vector3 direction; private float distance; // Use this for initialization void Start () { startPos = new Vector3 (XStartPos, YStartPos, ZStartPos); endPos = new Vector3 (XEndPos, YEndPos, ZEndPos); direction = (endPos - startPos).normalized; distance = Vector3.Distance (startPos, endPos); } // Update is called once per frame void Update () { Debug.DrawLine (startPos, endPos, Color.red); RaycastHit hit; if (!Physics.Raycast (startPos, direction, out hit, distance)) { Debug.Log ("false"); } else { Debug.Log ("yes " + hit.collider.name); } } }
建一个cube1,绑定脚本,设置startPos在cube内endPos在cube外,运行结果表明 false
再建一个cube2,startPos依然在cube1内,但endPos在cube2内,运行结果表明 yes cube2
也就是说 射线由内部向外部射 是不识别自己的碰撞体,但是识别其他的碰撞体。
第二个问题关于射线层的选择和屏蔽
修改一下脚本
using UnityEngine; using System.Collections; public class Demo3 : MonoBehaviour { public float XStartPos = 0f; public float YStartPos = 0f; public float ZStartPos = 0f; public float XEndPos = 0f; public float YEndPos = 0f; public float ZEndPos = 0f; private Vector3 startPos; private Vector3 endPos; private Vector3 direction; private float distance; // Use this for initialization void Start () { startPos = new Vector3 (XStartPos, YStartPos, ZStartPos); endPos = new Vector3 (XEndPos, YEndPos, ZEndPos); direction = (endPos - startPos).normalized; distance = Vector3.Distance (startPos, endPos); } // Update is called once per frame void Update () { Debug.DrawLine (startPos, endPos, Color.red); RaycastHit hit; LayerMask l = 1 << LayerMask.NameToLayer("cube1"); if (Physics.Raycast (startPos, direction, out hit, distance, l)) { Debug.Log ("yes " + hit.collider.name); } else { Debug.Log ("false"); } } }
自行比对
只与cube1层碰撞
Physics.Raycast (startPos, direction, out hit, distance, l)与除cube1层以外的层碰撞
Physics.Raycast (startPos, direction, out hit, distance, ~l)
与cube2 cube3层碰撞
Physics.Raycast (startPos, direction, out hit, distance, 1 << LayerMask.NameToLayer("cube2") | 1 << LayerMask.NameToLayer("cube3"))
与除cube2 cube3层碰撞
Physics.Raycast (startPos, direction, out hit, distance,~(1 << LayerMask.NameToLayer("cube2") | 1 << LayerMask.NameToLayer("cube3")))
Unity-Physics.Raycast
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。