ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【unity2D】API-学习记录9-射线类Ray2D

2021-05-06 12:35:46  阅读:310  来源: 互联网

标签:API unity2D 碰撞 Raycast 射线 RaycastHit2D Ray2D ray


目标

在游戏开发中,射线的使用非常广泛。今天来学习Ray2D的相关内容以及如何发射2D射线

前言

  1. unity中有应用于2D游戏中的射线,其中,Ray2D射线类和RaycastHit2D射线投射碰撞信息类是最常用的两个射线工具类。前者用来创建射线,后者用来存储发射射线后产生的碰撞信息。今天先来学习射线类Ray2D。
  2. UnityAPI中文文档看上去像是机翻,有些描述很莫名其妙,我这里对其中一些描述进行了改动,改动处会标明有“(改动前)”和“(改动后)”字样

Ray2D是什么

Ray2D是一种类型,正如下图它的构造函数所示:

image

变量 解释
origin 世界空间中的射线起点。
direction 世界空间中的射线方向。

API对Ray2D的解释

Ray2D

A ray in 2D space.
2D 空间中的射线。

A ray is a line segment that extends from a point in space in a specified direction. 
射线 是从空间中的某个点开始,按照指定方向延伸的线段。

Rays have a number of uses in Unity but the most common is probably raycasting. 
射线在 Unity 中具有多种用途,但是最常用的可能是射线投射。

This technique involves tracing along the path of a ray from its origin to determine if it intersects with any objects.
此方法涉及沿着射线路径从其原点进行追踪,以确定它是否与任何对象相交。

This is useful for plotting the paths of projectiles, determining lines of sight and implementing many common game mechanics.
这可用于绘制飞弹的路径、确定视线以及实现许多常见的游戏机制。

公共函数

公共函数 解释
GetPoint 获取射线上位于给定距离处的点。

如何发射2D射线

绘制2D射线用到的是:Physics2D.Raycast()
image

API对Physics2D.Raycast的解释


Physics2D.Raycast

public static RaycastHit2D Raycast(Vector2 origin, Vector2 direction, float distance = Mathf.Infinity, 
int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity);
参数 解释
origin 射线在 2D 空间中的起点。
direction 表示射线方向的矢量。
distance 射线的最大投射距离。
layerMask 过滤器,用于仅在特定层上检测碰撞体。
minDepth 仅包括 Z 坐标(深度)大于或等于该值的对象。
maxDepth 仅包括 Z 坐标(深度)小于或等于该值的对象。

Returns
返回

RaycastHit2D The cast results returned.
RaycastHit2D 返回的投射数量。//(改动前)
返回RaycastHit2D类型的投射结果。//(改动后)

Description
描述

Casts a ray against Colliders in the Scene.
向场景中的碰撞体投射射线。

A raycast is conceptually like a laser beam that is fired from a point in space along a particular direction.
从概念上说,射线投射 类似于从空间中的某个点朝特定方向发射一条光束。

Any object making contact with the beam can be detected and reported.
在该过程中,可以检测并报告与光束接触的任何对象。


This function returns a RaycastHit object with a reference to the Collider that is hit by the ray
(the Collider property of the result will be NULL if nothing was hit). 
函数返回一个 RaycastHit 对象,该对象引用了射线命中的碰撞(如果未命中任何对象,则结果的碰撞体属性将为 NULL)。

The layerMask can be used to detect objects selectively only on certain layers
(this allows you to apply the detection only to enemy characters, for example).
LayerMask 可用于仅在特定层上有选择地检测对象(例如,这让您能够仅将检测应用于敌人角色)。


Overloads of this method that use contactFilter can filter the results by the options available in ContactFilter2D.
使用 contactFilter 的此方法重载可以按 ContactFilter2D 中提供的选项筛选结果。


Raycasts are useful for determining lines of sight, targets hit by gunfire and for many other purposes in gameplay.
对于确定视线、炮火击中的目标以及游戏中的许多其他目的来说,射线投射很有用。

Additionally, this will also detect Collider(s) at the start of the ray. 
此外,这还将检测位于射线起点的碰撞体。

In this case, the ray starts inside the Collider and doesn't intersect the Collider surface. 
在这种情况下,射线从碰撞体内部开始,并且不与碰撞体表面交叠。

This means that the collision normal cannot be calculated, 
in which case the returned collision normal is set to the inverse of the ray vector being tested.
这意味着无法计算碰撞法线,在这种情况下,返回的碰撞法线设置为正在测试的射线向量的倒数。//(改动前)
这意味着无法计算碰撞法线,在这种情况下,返回的碰撞法线会被设置为“与发出的测试射线方向相反”的射线。//(改动后)

This can easily be detected because such results are always at a RaycastHit2D fraction of zero.
这可轻松检测到,因为此类结果始终是 RaycastHit2D 分数为零。//(改动前)
这可以被轻易地发现,因为出了问题的结果在 RaycastHit2D 中的法线部分总为零。//(改动后)

补充

  1. 关于Physics2D.Raycast,其可以返回RaycastHit2D类型的结果,关于RaycastHit2D的内容之后再学。
  2. Physics2D.Raycast还可以返回int类型的结果,具体有何作用,请参考Unity API Physics2D.Raycast

参考资料

Unity API Ray
Unity API Physics2D.Raycast

标签:API,unity2D,碰撞,Raycast,射线,RaycastHit2D,Ray2D,ray
来源: https://www.cnblogs.com/OtusScops/p/14734591.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有