返回介绍

Physics2D.Raycast 光线投射

发布于 2019-12-18 15:38:17 字数 6254 浏览 1230 评论 0 收藏 0

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

Parameters 参数

originThe point in 2D space where the ray originates.
光线在二维空间中的起点。
directionVector representing the direction of the ray.
光线的方向。
distanceMaximum distance over which to cast the ray.
光线的最大投射距离。
layerMaskFilter to detect Colliders only on certain layers.
只在某些层过滤检测碰撞器。
minDepthOnly include objects with a Z coordinate (depth) greater than this value.
只包括Z坐标(深度)大于这个值的对象。
maxDepthOnly 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.
对场景内的碰撞器们投射出一条光线。

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). 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, RaycastAll, Linecast, DefaultRaycastLayers, IgnoreRaycastLayer, raycastsHitTriggers.

JavaScript:

// Float a rigidbody object a set distance above a surface.
var floatHeight: float; // Desired floating height.
var liftForce: float; // Force to apply when lifting the rigidbody.
var damping: float; // Force reduction proportional to speed (reduces bouncing).
 
function FixedUpdate() {
    // Cast a ray straight down.
    var hit: RaycastHit2D = Physics2D.Raycast(transform.position, -Vector2.up);
 
    // If it hits something...
    if (hit.collider != null) {
        // Calculate the distance from the surface and the "error" relative
        // to the floating height.
        var distance = Mathf.Abs(hit.point.y - transform.position.y);
        var heightError: float = floatHeight - distance;
 
        // The force is proportional to the height error, but we remove a part of it
        // according to the object's speed.
        var force = liftForce * heightError - rigidbody2D.velocity.y * damping;
 
        // Apply the force to the rigidbody.
        rigidbody2D.AddForce(Vector3.up * force);
        }
    } 

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);
        }
    }
}

Physics2D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文