publicclasscontroller:MonoBehaviour{
publicRigidbodyball_1rig;
publicRigidbodyball_2rig;
voidStart(){
}
voidUpdate(){
if(Input.GetKeyDown(KeyCode.A)){
//添加相对力
//ball_1rig.AddForce(Vector3.right*100);
//ball_2rig.AddForce(Vector3.right*100*-1);
//在特定位置添加力
//位置是自身相对位置
ball_2rig.AddForceAtPosition(Vector3.right*300,newVector3(transform.position.x-0.5f,transform.position.y,transform.position.z));
}
//添加力矩
//ball_1rig.AddRelativeTorque(Vector3.up*5);
}
}
publicclassCapsuleScript:MonoBehaviour{
privateRigidbodyrig;
voidStart(){
rig=GetComponent
}
voidUpdate(){
if(Input.GetKeyDown(KeyCode.Space)){
//添加力,添加在中心点
//rig.AddForce(newVector3(0,200f,0));
rig.AddRelativeForce(newVector3(0,200,0));//添加力矩
//刚体质量为1,力的向量为(10.0f,0,0)
//t=0.02f;
//f*t=m*v(动量定理);
//10*0.02=1*v;
//v=0.2m/frame;
//v=10m/s;
//ForceMode.Acceleration f*t=1*v;
//ForceMode.Impulse瞬间力 f*1=m*v;
//ForceMode.VelocityChange f*1=1*v;
}
}
给物体添加一个爆炸力
publicclassboom:MonoBehaviour{
publicGameObjectsphere;
//Usethisforinitialization
voidStart(){
}
//Updateiscalledonceperframe
voidUpdate(){
if(Input.GetKeyDown(KeyCode.F)){
GetComponent
//这个方法返回球形半径之内(包括半径)的所有的碰撞体
Collider[]coll=Physics.OverlapSphere(transform.position,3);
//遍历所有的碰撞体,如果有刚体,则给刚体添加爆炸的力
foreach(Collider hit in coll){
//Rigidbodybody=hit.gameObject.GetComponent();
if(hit.attachedRigidbody){
hit.attachedRigidbody.AddExplosionForce(1000,transform.position,3);
}
}
GameObject.Destroy(sphere,0.01f);
}
}
}