Legacy Documentation: Version 4.5.0

Script language:

  • JS
  • C#
  • Boo
Script language

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

Rigidbody.AddForceAtPosition

Switch to Manual
AddForceAtPosition(force: Vector3, position: Vector3, mode: ForceMode = ForceMode.Force): void;
void AddForceAtPosition(Vector3 force, Vector3 position, ForceMode mode = ForceMode.Force);
def AddForceAtPosition(force as Vector3, position as Vector3, mode as ForceMode = ForceMode.Force) as void

Description

Applies force at position. As a result this will apply a torque and force on the object.

For realistic effects position should be approximately in the range of the surface of the rigidbody. This is most commonly used for explosions. When applying explosions it is best to apply forces over several frames instead of just one. Note that when position is far away from the center of the rigidbody the applied torque will be unrealistically large.

	function ApplyForce (body : Rigidbody) {
		var direction : Vector3 = body.transform.position - transform.position;
		body.AddForceAtPosition(direction.normalized, transform.position);
	}
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void ApplyForce(Rigidbody body) {
        Vector3 direction = body.transform.position - transform.position;
        body.AddForceAtPosition(direction.normalized, transform.position);
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	def ApplyForce(body as Rigidbody) as void:
		direction as Vector3 = (body.transform.position - transform.position)
		body.AddForceAtPosition(direction.normalized, transform.position)