Unity-Physics.Raycast

关于API看一下链接

http://game.ceeger.com/Script/Physics/Physics.Raycast.html

• static function Raycast (origin : Vector3direction : Vector3out hitInfo : RaycastHitdistance : float = Mathf.InfinitylayerMask : int = kDefaultRaycastLayers) : bool

Parameters参数

  • origin
    The starting point of the ray in world coordinates.
    在世界坐标,射线的起始点。
  • direction
    The direction of the ray.
    射线的方向。
  • distance
    The length of the ray
    射线的长度。
  • hitInfo
    If true is returned, hitInfo will contain more information about where the collider was hit (See Also:  RaycastHit).
    如果返回true,hitInfo将包含碰到器碰撞的更多信息。
  • layerMask
    A 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")))







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值