码迷,mamicode.com
首页 > 编程语言 > 详细

Unity-Physics.Raycast

时间:2014-11-21 14:29:06      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:unity3d

关于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")))







Unity-Physics.Raycast

标签:unity3d

原文地址:http://blog.csdn.net/lihuozhiling0101/article/details/41347041

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!