Unity's rigidbody (reproduced)

Rigidbody

Rigidbody components are added to GameObjects with physical properties, resulting in more realistic behavior when they are affected by forces and torques. When manipulating rigid body parameters, you should use it in the FixedUpdate function, where the physics simulation is performed at discrete time steps. The FixedUpdate function is called immediately before each step. When working with rigid bodies, care should be taken that both parent and child objects should not have rigid bodies at the same time, nor should the parent of a rigid body be scaled.

 

variable:

var velocity : Vector3 The velocity vector of the rigid body

var angularVelocity : Vector3 The angular velocity vector of the rigid body.

var drag : float object's drag

var angularDrag : float The angular drag of the object

var mass : float The mass of the rigid body

var useGravity : bool whether gravity is affected

var isKinematic : bool                           

       Whether it is a kinematic rigid body. Kinematic rigid bodies are not controlled by forces, collisions or joints, and are completely driven by animation or code (Transform), but kinematic rigid bodies can affect the motion of other rigid bodies through collisions or joints. Non-kinematic rigid bodies are controlled by forces, collisions, or joints.

var freezeRotation : bool Freeze the rotation so that physics cannot change the rotation of the object

var collisionDetectionMode: CollisionDetectionMode

       Collision detection mode for rigid bodies. For best results, for fast-moving objects, you should set the dynamic continuous collision detection mode (ContinuousDynamic) to prevent the object from crossing the collided object before detecting the collision; for the collided object, you should set the continuous collision detection mode ( Continuous, continuous collision detection is based on static collision of mesh geometry). Since the choice of collision detection mode has a great impact on physical performance, by default, the discontinuous collision detection mode (Discrete) should be used.

var centerOfMass : Vector3                     

       center of gravity. If you don't set the center of gravity in the script, it will be calculated automatically from all colliders attached to the rigidbody. The setting of the center of gravity is related to the authenticity of the physical movement.

var worldCenterOfMass : Vector3 The center of mass of the rigid body in world coordinates. (read only)

var detectCollisions : bool Whether collision detection is enabled (always enabled by default).

var position : Vector3                             

       The position of the rigid body, which is only applied to the transformation at the end of the physics step, cannot be used for continuous movement.

var rotation : The rotation of the Quaternion rigid body, which is only applied to the transformation at the end of the physics step, not for continuous rotation.

var interpolation : RigidbodyInterpolation

Rigid body interpolation, used to smooth the display of physical motion, is off by default (None). Physical motion runs in discrete time steps, while the graphics card renders at a variable frame rate, which can cause objects to shake. Interpolation is recommended for protagonists or objects that the camera follows.

 

function:

 

move

function AddForce (force : Vector3, mode :ForceMode = ForceMode.Force) : void

       Add a force to the rigid body. As a result the rigid body will start to move.

       Enumeration type ForceMode

       值:       Force                      添加一个可持续力到刚体,使用它的质量。

                     Acceleration          添加一个可持续加速度到刚体,忽略它的质量。

                     Impulse                  添加一个瞬间冲击力到刚体,使用它的质量。

                     VelocityChange     添加一个瞬间速度变化给刚体,忽略它的质量。

function AddForce (x : float, y : float, z: float, mode : ForceMode = ForceMode.Force) : void

       添加一个力到刚体。作为结果刚体将开始移动。

function AddRelativeForce (force : Vector3,mode : ForceMode = ForceMode.Force) : void

       添加一个力到刚体。相对于本地坐标。

function AddRelativeForce (x : float, y :float, z : float, mode : ForceMode = ForceMode.Force) : void

       添加一个力到刚体。相对于本地坐标。

function MovePosition (position : Vector3): void 

       移动刚体到position。

 

旋转

function AddTorque (torque : Vector3, mode: ForceMode = ForceMode.Force) : void

       添加一个转矩到刚体。作为结果刚体将绕着torque轴旋转,torque的长度决定转速。

function AddTorque (x : float, y : float, z: float, mode : ForceMode = ForceMode.Force) : void

       添加一个转矩到刚体。作为结果刚体将绕着torque轴旋转,torque的长度决定转速。

function AddRelativeTorque (torque :Vector3, mode : ForceMode = ForceMode.Force) : void 

       添加一个转矩到刚体,相对于本地坐标。

function AddRelativeTorque (x : float, y :float, z : float, mode : ForceMode = ForceMode.Force) : void

       添加一个转矩到刚体,相对于本地坐标。

function MoveRotation (rot : Quaternion) :void 

       旋转刚体到rot。

 

向物体表面施加力

function AddForceAtPosition (force :Vector3, position : Vector3, mode : ForceMode = ForceMode.Force) : void 

       在position位置应用force力。作为结果这个将在这个物体上应用一个力矩和力。为了效果的真实性,position的位置应在刚体的表面。

 

爆炸

function AddExplosionForce (explosionForce: float, explosionPosition : Vector3, explosionRadius : float, upwardsModifier: float = 0.0F, mode :ForceMode = ForceMode.Force) : void 

       应用一个力到刚体来模拟爆炸效果。爆炸力将随着到刚体的距离线形衰减。

       explosionForce                             爆破力

       explosionPosition                        爆炸位置

       explosionRadius                           爆破半径,为0时表示无穷大

       upwardsModifier                         可以理解为爆炸产生的向上冲击波的衰减值。值越大,威力越小。

function ClosestPointOnBounds (position :Vector3) : Vector3

       计算刚体表面离position最近的点。可用于计算作用到刚体表面上一个点的爆炸力。

 

获取速度

function GetRelativePointVelocity(relativePoint : Vector3) : Vector3

       获取刚体上的点相对本地坐标系的速度。获取的速度有考虑刚体的角速度。

function GetPointVelocity (worldPoint :Vector3) : Vector3

       获取刚体上的点相对世界坐标系的速度。获取的速度有考虑刚体的角速度。

 

休眠

function Sleep () : void 

       强制一个刚体休眠至少一帧。刚体休眠有利于性能优化

function IsSleeping () : bool 

       判断刚体是否在休眠

function WakeUp () : void

       强制唤醒一个刚体

 

碰撞扫描

function SweepTest (direction : Vector3,out hitInfo : RaycastHit, distance : float = Mathf.Infinity) : bool 

       碰撞扫描,返回是否将要发生碰撞。

       direction                       扫描方向

       hitInfo                           如果将要发生碰撞(即返回true),hitInfo将包含更多碰撞信息

       distance                        扫描长度

function SweepTestAll (direction : Vector3,distance : float = Mathf.Infinity) : RaycastHit[] 

       碰撞扫描,返回所有碰撞点。


犯错不要紧,要紧的是发现错误的眼睛。


在5.3版本里面直接调用rigidbody.AddForce(movement);已经过期的方法改用下面的方法可以编译成功

GetComponent<Rigidbody>().AddForce(movement);


[csharp]  view plain  copy
  1. <pre name="code" class="csharp">using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class PlayerController : MonoBehaviour {  
  5. <span style="white-space:pre">    </span>//定义变量控制刚体速度  
[csharp]  view plain  copy
  1. <span style="white-space:pre">    </span> public float speed;  
  2.   
  3. oid FixedUpdate() {  
  4.       float moveHorizontal = Input.GetAxis("Horizontal");  
  5.       float moveVertical = Input.GetAxis("Vertical");  
  6.   
  7.       Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);  
  8.   
  9.       GetComponent<Rigidbody>().AddForce(movement * speed * Time.deltaTime);  
  10.   
  11.   }  


 
 


unity3d里面直接填写speed,挺简单的。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326431040&siteId=291194637