Physics2D.Raycast 光线投射

JavaScript ⇒ static function Raycast(originVector2directionVector2distance: float = Mathf.Infinity, layerMask: int = DefaultRaycastLayers, minDepth: float = -Mathf.Infinity, maxDepth: float = Mathf.Infinity): RaycastHit2D
C# ⇒ static RaycastHit2D Raycast(Vector2 originVector2 direction, float distance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity);

Parameters 参数
origin The point in 2D space where the ray originates.
光线在二维空间中的起点。
direction Vector representing the direction of the ray.
光线的方向。
distance Maximum distance over which to cast the ray.
光线的最大投射距离。
layerMask Filter to detect Colliders only on certain layers.
只在某些层过滤检测碰撞器。
minDepth Only include objects with a Z coordinate (depth) greater than this value.
只包括Z坐标(深度)大于这个值的对象。
maxDepth Only include objects with a Z coordinate (depth) less than this value.
只包括Z坐标(深度)小于这个值的对象。
Returns 返回值

RaycastHit2D The cast results returned. 
RaycastHit2D 类型的投射返回结果。

Description 描述

Casts a ray against colliders in the scene.
对场景内的碰撞器们投射出一条光线。

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). 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).
这个函数返回一个光线投射击中对象,对象内引用了被光线击中的碰撞器(如果没有东西被击中,这个碰撞器属性会是空的)。层遮罩会被用来有选择性地只在指定的层中检测各种对象(比如:这允许您设置检测只适用于敌人角色)

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 is starting inside the Collider and doesn't intersect the Collider surface. This means that the collision normal cannot be calculated in which case the collision normal returned 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中的法线部分是零。

See Also: LayerMask class, RaycastHit2D class, RaycastAllLinecastDefaultRaycastLayersIgnoreRaycastLayerraycastsHitTriggers.

C#:
using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    public float floatHeight;
    public float liftForce;
    public float damping;
    void FixedUpdate() {
        RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up);
        if (hit.collider != null) {
            float distance = Mathf.Abs(hit.point.y - transform.position.y);
            float heightError = floatHeight - distance;
            float force = liftForce * heightError - rigidbody2D.velocity.y * damping;
            rigidbody2D.AddForce(Vector3.up * force);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值