AddForce给物体添加刚体效果并且脚本增加一个力(按空格实现)
using UnityEngine; using System.Collections; public class CubeAddForce : MonoBehaviour { float hor,ver; //力的方向向量 Vector3 forceDir; //力的大小 public float force = 100; Rigidbody rig; void Start() { rig = GetComponent<Rigidbody> (); } void Update() { hor = Input.GetAxis ("Horizontal"); ver = Input.GetAxis ("Vertical"); forceDir = new Vector3 (hor,0,ver); //施加一个力 rig.AddForce (forceDir * force); if (Input.GetKeyDown (KeyCode.Space)) { rig.AddExplosionForce (1000, Vector3.zero, 10); } } }