求教Physics2D.Raycast的正确用法关于layerdrawable用法MAskde

Physics Raycast LayerMask problem? - Unity Answers
Navigation
Unity account
You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio.
Physics Raycast LayerMask problem?
Here's the code
public LayerM//I have already set this &cube& in Inspector
void Start () {
void Update () {
RaycastHit rayH
if (!Physics.Raycast(Camera.mainCamera.ScreenPointToRay(Input.mousePosition),100, out rayHit, lm))
Debug.DrawLine(Camera.mainCamera.ScreenPointToRay(Input.mousePosition).origin, rayHit.point);
I have set two different layer: &cube& and &sphere&,and I put two objects which change their layer one is &cube&,the other is &sphere&.When I test in Game , I found that both Objects can be hit by the ray.
So,is there something I set wrong?How can I just set it just hit the layer of &cube&?
Best Answer
I found the problem: 1. The Raycast function have to need a parameter which is &distance& , or it won't
2. The &distance& parameter have to be at the place before &layermask& parameter,like: if (!Physics.Raycast(ray, out rayHit, 100, lm)) but if you set the wrong place like the code in my question,there is nothing wrong to Unity3d,but it just can't select the layermask.
Is it a bug?
ps:it just fine when you use layermask or layermask.value
The Physics.Raycast function does not take a LayerMask variable, it takes an int variable. You can convert the LayerMask value to an integer value like this:
As mentioned here:
Hint: You can notify a user about this post by typing @username
Attachments: Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
4 People are following this question.为什么在Physics.Raycast函数传入了LayerMask参数后检测不到物体【unity吧】_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:21,239贴子:
为什么在Physics.Raycast函数传入了LayerMask参数后检测不到物体收藏
如题,Physics.Raycast(ray, out hit)时可以检测到Default层的物体,写成Physics.Raycast(ray, out hit,-1)或者Physics.Raycast(ray, out hit,0)时就不行了
登录百度帐号推荐应用
为兴趣而生,贴吧更懂你。或Physics2D.Raycast -know what you are hitting - Unity Answers
Navigation
Unity account
You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio.
Physics2D.Raycast -know what you are hitting
Hello, I am trying to find the object that is hit by the ray , but I cannot.
my player is doing a RayCast against the ground layer. the raycast is working as seen in the debug below. but i want to to know the name of that object so I can do CompareTag
function FixedUpdate()
var hit: RaycastHit2D = Physics2D.Raycast(transform.position,-Vector2.up,1 && LayerMask.NameToLayer(&Ground&), -2);
if(hit != null)
Debug.Log(hit.transform); //results: tree_log(Clone) (UnityEngine.Transform)
Debug.Log(hit.collider);
// result: tree_log(Clone) (UnityEngine.BoxCollider2D)
Debug.Log(hit.collider.name); //results: NullReferenceException: Object reference not set to an instance of an object
Debug.Log(hit.transform.name); //results: NullReferenceException: Object reference not set to an instance of an object
Best Answer
hey can i know what does the line 1 && LayerMask.NameToLayer(&Ground&) does,
i think Physics2D.Raycast( vector2 , vector2 , float , int ,float ) is the function parameter list. and also antho function that is overridden Physics2D.Raycast( vector2 , vector2 , float , int ,float , float)
where the layermask.nametolayer returns an integer .
Get the GameObject from the collider, from there you can extract the name:
hit.collider.gameObject.name
The 2D API will apparently return a non-null Raycast2D even when no collider is found in raycasting, so you should also test for that to determine if a collision occurred. Here's an example you can use in testing:
#pragma strict
var target : GameO
var numTargets : int = 8;
var targetDistance : float = 7.0f;
var sweepSpeed = 3.0f;
var lineRender : LineR
var targetLayer :
var lastHit : GameO
function Start () {
lineRender = GetComponent(LineRenderer);
DuplicateTarget();
function Update () {
var radius : Vector2 = Vector2(Mathf.Cos(sweepSpeed * Time.time), Mathf.Sin(sweepSpeed * Time.time)) * targetD
var hit : RaycastHit2D = Physics2D.Raycast(transform.position, radius, targetDistance, 1 && targetLayer);
if (null != hit && null != hit.collider) {
lastHit = hit.collider.gameO
if (null != lineRender) {
lineRender.SetPosition(0, transform.position);
lineRender.SetPosition(1, transform.position + radius);
function OnGUI() {
var r = Rect(10f,10f,200f,100f);
var label : String = &Nothing&;
if (null != lastHit) {
label = lastHit.
GUI.Label(r, label);
function DuplicateTarget() {
if (null != target) {
targetLayer = target.
// Place copies of the target around the space of the sweeper.
var origin : Vector3 = transform.
var angleStep = Mathf.PI * 2.0f / numT
for (var i=0; i&numT ++i) {
var angle : float = angleStep *
var r : Vector2 = Vector2( Mathf.Cos(angle), Mathf.Sin(angle)) * targetD
var newTarget : GameObject = Instantiate(target, origin + r, Quaternion.identity);
newTarget.name = String.Format(&object_{0}&, i);
target.SetActive(false);
Hint: You can notify a user about this post by typing @username
Attachments: Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
21 People are following this question.Physics2D, weird layerMask behavior on Linecast, Raycast and OverlapPoint - Unity Answers
Navigation
Unity account
You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio.
Physics2D, weird layerMask behavior on Linecast, Raycast and OverlapPoint
Whenever I pass a value of 8 (custom LayerMask) to these functions, it collides with an object which uses the Default layer (which value is 0).
If I pass ~8, ~11 or ~13 for instance, they will collide with ~0 (anything that does not use the Default Layer)...
So I tried passing 0 as the layer mask, to check if it would correctly collide with the default layer, and surprisingly it didn't collide with anything.
The weird thing is that it works as expected in other scripts in the same scene. I'm trying to find out if it could be a conflict with some other code, but I haven't found a reasonable answer for this. Anyone has had the same problem?
Best Answer
int layerID = LayerMask.NameToLayer(layerName);
int layerMask = 1 && layerID;
// 2^layerID
Collider2D[] hits = Physics2D.OverlapPointAll(worldPos, layerMask);
We all sometimes forget which out of the 10 kinds of people we are.
I think this might be a bug.
With this code:
int layerToC
Collider2D[] hits = new Collider2D[3];
void Update()
if (Input.GetMouseButtonDown(0))
Vector2 p = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition);
int layerToCheck = LayerMask.NameToLayer(&Default&);
Debug.Log(&Test 1&);
int hitCount = Physics2D.OverlapPointNonAlloc(p, hits, layerToCheck);
while (hitCount-- & 0)
Debug.Log(&
Test 1 HIT & + hits[hitCount].gameObject);
Debug.Log(&Test 2&);
hitCount = Physics2D.OverlapPointNonAlloc(p, hits);
while (hitCount-- & 0)
GameObject go = hits[hitCount].gameO
Debug.Log(&
Test 2 HIT & + go + & Layer was: & + LayerMask.LayerToName(go.layer));
When clicking on the intersecting part of 2 2D colliders, the output is:
Test 2 HIT ColliderObject2 (UnityEngine.GameObject) Layer was: Default
Test 2 HIT ColliderObject1 (UnityEngine.GameObject) Layer was: Default
When checking for hits with the layermask int derived from &Default& we get 0 hits, but all hit objects retrieved with an unfiltered call have a layer of &Default&
Hint: You can notify a user about this post by typing @username
Attachments: Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
23 People are following this question.}

我要回帖

更多关于 jquery layer用法 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信