Unity3D中的unity raycastt

Physics.Raycast 光线投射_unity3d游戏脚本制作教程-游戏蛮牛出品
Unity 脚本手册
Physics.Raycast 光线投射
static function Raycast (origin : , distance : float = , layerMask : int = kDefaultRaycastLayers) : bool
Parameters参数
The starting point of the ray in world coordinates.
在世界坐标,射线的起始点。
The direction of the ray.射线的方向。
The length of the ray
射线的长度。
A Layer mask that is used to selectively ignore colliders when casting a ray. 只选定Layermask层内的碰撞器,其它层内碰撞器忽略。
bool - True when the ray intersects any collider, otherwise false.
当光线投射与任何碰撞器交叉时为真,否则为假。
Description描述
Casts a ray against all colliders in the scene.
在场景中投下可与所有碰撞器碰撞的一条光线。
using UnityE
using System.C
public class example :
void Update() {
if (Physics.Raycast(transform.position, fwd, 10))
print("There is something in front of the object!");
function Update () {
var fwd = transform.TransformDirection ();
if (Physics.Raycast (transform.position, fwd, 10)) {
print ("There is something in front of the object!");
Note: This function will return false if you cast a ray from
inside a s this in an intended behaviour.
注意:如果从一个球型体的内部到外部用光线投射,返回为假。Unity3D UI 事件穿透_百度经验
&&&&&&互联网Unity3D UI 事件穿透听语音123
百度经验:Unity4.6新UI系统,当我们需要在按钮上面放上图片时,点击图片下面的按钮按钮就无法响应了百度经验:Unity3D4.6及以上版本百度经验:1添加一个新的脚本文件,这里以C#为例,添加如下代码using UnityEusing System.Cpublic class DUIRayIgnore : MonoBehaviour, ICanvasRaycastFilter{ public bool IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera) {
}}2将次脚本绑定到Image上面3运行,这时,图片下面的UI元素就能响应事件了,Image就会穿透事件同时,这个脚本可以绑定到任何需要穿透的地方END经验内容仅供参考,如果您需解决具体问题(尤其法律、医学等领域),建议您详细咨询相关领域专业人士。作者声明:本篇经验系本人依照真实经历原创,未经许可,谢绝转载。投票(2)已投票(2)有得(0)我有疑问(0)◆◆说说为什么给这篇经验投票吧!我为什么投票...你还可以输入500字◆◆非回享用户暂时不能发布经验“有得”&你还可以输入1000字◆◆如对这篇经验有疑问,可反馈给作者,经验作者会尽力为您解决!你还可以输入500字相关经验0246217热门杂志第1期你不知道的iPad技巧3488次分享第1期win7电脑那些事6117次分享第2期新人玩转百度经验1173次分享第1期Win8.1实用小技巧2521次分享第1期小白装大神1673次分享◆请扫描分享到朋友圈程序写累了,就来玩玩酷跑小游戏吧,嘿嘿。
雨松MOMO送你一首歌曲,嘿嘿。
UGUI研究院之忽略UI被拦截事件(十三)
UGUI研究院之忽略UI被拦截事件(十三)
围观23060次
编辑日期: 字体:
如果对UGUI事件系统还不清楚的朋友可以看看我之前的文章
如果一个按钮有一半的区域被Image挡住,那么被挡住的按钮区域的点击事件就会被拦截掉。解决这个问题有两个方法。
1.修改Hierarchy视图中的树状结构。如下图所示,把Image2放到Image1的下面,这样渲染上Image2在Image1前面,这样点击被挡住区域就会被响应了, Text的原理一样。
2.通过重写Image类,让Image不参与射线检测。
继承Image 重写 IsRaycastLocationValid 直接返回 false就行了。
12345678910
using UnityEngine;using System.Collections;using UnityEngine.UI;public class ImagePlus : Image {& override public bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera) {
return false; }}
感谢写朋友在下面的留言的 另一个方法
加個UGUI內建的CanvasGroup組件, 把Interactable和Blocks Raycasts選項取消。
我试过了没问题, 不会增加多余dc。赞!
补充一下,如果你想整体的关闭某个父节点下的所有UI事件。把如下脚本绑定在父节点上即可。
<div class="crayon-num" data-line="crayon-590ccb<div class="crayon-num crayon-striped-num" data-line="crayon-590ccb<div class="crayon-num" data-line="crayon-590ccb<div class="crayon-num crayon-striped-num" data-line="crayon-590ccb<div class="crayon-num" data-line="crayon-590ccb<div class="crayon-num crayon-striped-num" data-line="crayon-590ccb<div class="crayon-num" data-line="crayon-590ccb<div class="crayon-num crayon-striped-num" data-line="crayon-590ccb<div class="crayon-num" data-line="crayon-590ccb<div class="crayon-num crayon-striped-num" data-line="crayon-590ccb<div class="crayon-num" data-line="crayon-590ccb
using UnityEngine;using System.Collections;&public class UIFocus : MonoBehaviour ,ICanvasRaycastFilter{ public bool IsFocus= false; public bool IsRaycastLocationValid (Vector2 sp, Camera eventCamera) {
return IsFocus; }}
本文固定链接:
转载请注明:
雨松MOMO提醒您:亲,如果您觉得本文不错,快快将这篇文章分享出去吧 。另外请点击网站顶部彩色广告或者捐赠支持本站发展,谢谢!
作者:雨松MOMO
专注移动互联网,Unity3D游戏开发
如果您愿意花10块钱请我喝一杯咖啡的话,请用手机扫描二维码即可通过支付宝直接向我捐款哦。
您可能还会对这些文章感兴趣!c# - Unity3D Physics.Raycast() never true - Stack Overflow
Join the Stack Overflow Community
Stack Overflow is a community of 7.1 million programmers, just like you, helping each other.
J it only takes a minute:
I have a scene where I have an game object with sprite renderer and image attached to it. The game object has a 2D collider to. I'm trying to get a true reading when I'm clicking the image but its always false, I
have no idea what em i doing wrong.
TapPos = GetComponent&Camera& ().ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 0));
bool SomethingTapped(){
Vector3 CanvasTapPoint = new Vector3 (TapPos.x, TapPos.y, 10);
Debug.DrawLine (TapPos, CanvasTapPoint, Color.green, 10f);
if (Physics.Raycast (TapPos,CanvasTapPoint, 20f)) {
The Debug.DrawLine goes right through my sprite.
The script is on the camera and the camera Z position is -10
After Andrew Griffin's response changed my code to this if anyone want to do the same thing:
public Transform ObjectTapped(){
RaycastHit2D FoundObj = Physics2D.Raycast(TapPos, new Vector2(TapPos.x, TapPos.y + 1),0f);
if(FoundObj.collider
Transform obj = FoundObj.
Look into . As far as I am aware, Physics.Raycast will not work with 2D colliders. You must use Physics2D.Raycast instead.
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you&#39;re looking for?
Browse other questions tagged
rev .25837
Stack Overflow works best with JavaScript enabled使用Unity3d的Physics.Raycast()的用法做子弹射击 - Tonge(2)【站长博客网】
使用Unity3d的Physics.Raycast()的用法做子弹射击 - Tonge(2)
作者:冰河 来源:博客园-所有随笔区 时间: 23:17
static function RaycastAll (origin : Vector3 , direction : Vector3 , distance : float = Mathf.Infinity , layermask : int = kDefaultRaycastLayers) : RaycastHit [] 与所有在ray这条射线上的物体碰
相关推荐:
& function
&RaycastAll (origin :&
, direction :&
, distance :& float
, layermask :& int
&= kDefaultRaycastLayers) :&
与所有在ray这条射线上的物体碰撞并返回所有被碰撞体,RaycastHit[]。 其他同RaycastAll一样。
& function
&Linecast ( start
, layerMask :& int
&= kDefaultRaycastLayers) :& bool
& function
&Linecast ( start
, out hitInfo :&
, layerMask :& int
&= kDefaultRaycastLayers) :& bool
从start到end画一条直线,其他与Raycast和RaycastAll类似。 这个通常可以用于检测AI是否能看到目标物体等功能。
& function
&OverlapSphere (position :&
, radius :& float
, layerMask :& int
&= kAllLayers) :&
检测在position位置的半径radius的球形范围内是否存在可碰撞体并返回所有检测到的collider
& function
&CheckSphere (position :&
, radius :& float
, layerMask :& int
&= kDefaultRaycastLayers) :& bool
当任何物体碰到position位置的半径为radius的球形范围时返回true;
& function
&CheckCapsule ( start
, radius :& float
, layermask :& int
&= kDefaultRaycastLayers) :& bool
大家感兴趣的内容
最近更新的内容

我要回帖

更多关于 unity3d raycasthit 的文章

 

随机推荐