public void AddForce (Vector2 force, ForceMode2D mode= ForceMode2D.Force);

参数

force力在 X 轴和 Y 轴上的分量。
mode用于施加指定力的方法。

描述

对刚体施加力。

力被指定为在 X 和 Y 方向(在 2D 物理中,没有 Z 方向)上的两个独立分量。对象在力的作用下的加速运动遵循力 = 质量 x 加速度 法则 - 即质量越大,加速到给定速度所需的力也越大。

另请参阅:AddForceAtPositionAddTorquemassvelocityAddForceForceMode2D

// The sprite will fall under its weight.  After a short time the
// sprite will start its upwards travel.  This is because
// of the force on the sprite.

using UnityEngine; using System.Collections;

public class Example : MonoBehaviour { public Texture2D tex;

private Rigidbody2D rb2D; private Sprite mySprite; private SpriteRenderer sr; private float thrust = 10.0f;

void Awake() { sr = gameObject.AddComponent<SpriteRenderer>(); rb2D = gameObject.AddComponent<Rigidbody2D>(); }

void Start() { mySprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, 128.0f, 128.0f), new Vector2(0.5f, 0.5f), 100.0f);

sr.color = new Color(0.9f, 0.9f, 0.5f, 1.0f); sr.sprite = mySprite; transform.position = new Vector3(0.0f, -2.0f, 0.0f); }

void Update() { rb2D.AddForce(transform.up * thrust); } }