返回介绍

Collider.ClosestPointOnBounds 在框上最近的点

发布于 2019-12-18 15:37:32 字数 1942 浏览 1383 评论 0 收藏 0

JavaScript => public function ClosestPointOnBounds(position: Vector3): Vector3;
C# => public Vector3 ClosestPointOnBounds(Vector3 position);

Description 描述

The closest point to the bounding box of the attached collider.

到碰撞器的框最近的点。

This can be used to calculate hit points when applying explosion damage.

当应用于爆炸伤害,这能用于计算伤害点数。

JavaScript:

#pragma strict
public var hitPoints = 100.0F;
public var coll;
function Start() {
	coll = GetComponent.<Collider>();
}
function ApplyHitPoints(explosionPos, radius) {
	// The distance from the explosion position to the surface of the collider.
	var closestPoint = coll.ClosestPointOnBounds(explosionPos);
	var distance = Vector3.Distance(closestPoint, explosionPos);
	// The damage should decrease with distance from the explosion.
	var damage = 1.0F - Mathf.Clamp01(distance / radius);
	hitPoints -= damage * 10.0F;
}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    public float hitPoints = 100.0F;
    public Collider coll;
    void Start() {
        coll = GetComponent<Collider>();
    }
    void ApplyHitPoints(Vector3 explosionPos, float radius) {
        // The distance from the explosion position to the surface of the collider.
        Vector3 closestPoint = coll.ClosestPointOnBounds(explosionPos);
        float distance = Vector3.Distance(closestPoint, explosionPos);
 
        // The damage should decrease with distance from the explosion.
        float damage = 1.0F - Mathf.Clamp01(distance / radius);
        hitPoints -= damage * 10.0F;
    }
}

Collider

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

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

发布评论

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