Legacy Documentation: Version 5.6 (Go to current version)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Rigidbody.ClosestPointOnBounds

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual
public function ClosestPointOnBounds(position: Vector3): Vector3;
public Vector3 ClosestPointOnBounds(Vector3 position);

Description

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

// Subtract damage from a character's hit points when an
// explosion occurs.
var hitPoints : float = 10.0;
var rb: Rigidbody;

function Start() { rb = GetComponent.<Rigidbody>(); }

function ApplyDamage(explosionPos : Vector3, radius : float) { // Find the distance from the explosion position to the surface of the rigidbody. var closestPoint : Vector3 = rb.ClosestPointOnBounds(explosionPos); var distance : float = Vector3.Distance(closestPoint, explosionPos);

// Calculate the damage we apply, decreasing with distance from the strike point. var damage : float = 1.0 - Mathf.Clamp01(distance / radius); damage *= 10;

// Apply the damage hitPoints -= damage; }
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { public float hitPoints = 10.0F; public Rigidbody rb; void Start() { rb = GetComponent<Rigidbody>(); } void ApplyDamage(Vector3 explosionPos, float radius) { Vector3 closestPoint = rb.ClosestPointOnBounds(explosionPos); float distance = Vector3.Distance(closestPoint, explosionPos); float damage = 1.0F - Mathf.Clamp01(distance / radius); damage *= 10; hitPoints -= damage; } }