当前位置:网站首页>Unity: use ray to detect objects
Unity: use ray to detect objects
2022-06-22 23:26:00 【You still have to work hard!】
utilize ray Ray Detect objects
Unity ray (Ray) It is to detect a collision body or trigger by emitting a ray . Objects that cannot be detected without impactor components , You can cancel the detection trigger in the physical settings (Edit → Project Setting → Physics/Physics2D).
Physics.Raycast(origin, direction, out hitInfo, distance, mask)
origin: Where the rays are emitted ;
direction: The direction in which the rays are emitted .
hitInfo: Information about the object hit by the ray ;
distance: Ray distance , The default is infinite distance ;
mask: Ray mask , Indicates which... Has been detected layer, The default is to detect all layers . The form of this parameter is required :
Method 1 :
int shootableMask = LayerMask.GetMask("Shootable");
Physics2D.Raycast(Ray, out hitInfo, range, shootableMask);
Method 2 :
int layerNumber;
(1 << layerNumber) // Only test No layerNumber The layer
~(1 << layerNumber) // No test number is layerNumber The layer , The rest of the layers are detected
(1 << layerNumber) | (1 << layer2Number) // Only detect 2 Layers
RaycastHit: Store collision information after emitting rays
Common member variables :
collider: A collider that collides with rays ;
transform: Of an impactor that collides with rays Transform;
distance: The distance from the starting point of the ray to the intersection of the ray and the collider ;
normal: The normal vector of a ray entering a plane ;
point: Coordinates of the intersection of the ray and the collider .
Common usage
- Emit a ray from the camera to the mouse ;
- Obtain the collision information of the ray ;
- Get the colliding object , Manipulate the object .
Code instance
Ray myRay = Camera.main.ScreenPointToRay(Input.mousePosition);// A ray from the camera
RaycastHit2D hit = Physics2D.Raycast(new Vector2(myRay.origin.x, myRay.origin.y), Vector2.zero);// The ray starts from the point where the mouse clicks on the screen , Shoot to the coordinate system with the current click position as the origin, perpendicular to (0,0) The location of ,
if (hit.collider)// If you encounter Collider2D Component objects , Find the object
{
Debug.Log(hit.transform.name);// Print out the name of the collision node
GameObject obj = GameObject.Find(hit.transform.name);
}
// adopt obj To manipulate objects
边栏推荐
- OJ每日一练——找第一个只出现一次的字符
- C language -- 17 function introduction
- 剑指 Offer 05. 替换空格
- 2021-01-29
- China Mobile's mobile phone users grow slowly, but strive for high profit 5g package users
- C sqlsugar, hisql, FreeSQL ORM framework all-round performance test vs. sqlserver performance test
- tp5.1解决跨域
- Spark SQL 访问json和jdbc数据源
- 2020-12-04
- flink同步mysql数据到ES
猜你喜欢
随机推荐
Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading
[kubernetes series] overview of kubernetes
14. 最长公共前缀
2020-12-04
2020-12-20
2021-04-05
安装typescript环境并开启VSCode自动监视编译ts文件为js文件
10 Super VIM plug-ins, I can't put them down
flink同步mysql数据到ES
Finding the value of the nth term of Fibonacci sequence by recursion
OJ每日一练——找第一个只出现一次的字符
C language greedy snake
os.Args[1:]中命令行参数为空时,不执行内部语句
Reddit's discussion on lamda model: it is not stateless. It adopts a dual process. Compared with the way it edits Wikipedia, it doesn't matter whether it has feelings or not
2020-12-20
Common operations of sourcetree version management
启牛app下载证券开户,是安全的吗?有风险嘛?
flutter外包,承接flutter项目
剑指 Offer 05. 替换空格
2020-12-20








