本文整理汇总了C#中RigidBody.ApplyImpulse方法的典型用法代码示例。如果您正苦于以下问题:C# RigidBody.ApplyImpulse方法的具体用法?C# RigidBody.ApplyImpulse怎么用?C# RigidBody.ApplyImpulse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RigidBody
的用法示例。
在下文中一共展示了RigidBody.ApplyImpulse方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FixedUpdate
public void FixedUpdate(float timeStep)
{
animCtrl = animCtrl ?? GetComponent<AnimationController>();
body = body ?? GetComponent<RigidBody>();
// Update the in air timer. Reset if grounded
if (!onGround)
inAirTimer += timeStep;
else
inAirTimer = 0.0f;
// When character has been in air less than 1/10 second, it's still interpreted as being on ground
bool softGrounded = inAirTimer < CharacterDemo.InairThresholdTime;
// Update movement & animation
var rot = Node.Rotation;
Vector3 moveDir = Vector3.Zero;
var velocity = body.LinearVelocity;
// Velocity on the XZ plane
Vector3 planeVelocity = new Vector3(velocity.X, 0.0f, velocity.Z);
if (Controls.IsDown(CharacterDemo.CtrlForward))
moveDir += Vector3.UnitZ;
if (Controls.IsDown(CharacterDemo.CtrlBack))
moveDir += new Vector3(0f, 0f, -1f);
if (Controls.IsDown(CharacterDemo.CtrlLeft))
moveDir += new Vector3(-1f, 0f, 0f);
if (Controls.IsDown(CharacterDemo.CtrlRight))
moveDir += Vector3.UnitX;
// Normalize move vector so that diagonal strafing is not faster
if (moveDir.LengthSquared > 0.0f)
moveDir.Normalize();
// If in air, allow control, but slower than when on ground
body.ApplyImpulse(rot * moveDir * (softGrounded ? CharacterDemo.MoveForce : CharacterDemo.InairMoveForce));
if (softGrounded)
{
// When on ground, apply a braking force to limit maximum ground velocity
Vector3 brakeForce = -planeVelocity * CharacterDemo.BrakeForce;
body.ApplyImpulse(brakeForce);
// Jump. Must release jump control inbetween jumps
if (Controls.IsDown(CharacterDemo.CtrlJump))
{
if (okToJump)
{
body.ApplyImpulse(Vector3.UnitY * CharacterDemo.JumpForce);
okToJump = false;
}
}
else
okToJump = true;
}
// Play walk animation if moving on ground, otherwise fade it out
if (softGrounded && !moveDir.Equals(Vector3.Zero))
animCtrl.PlayExclusive("Models/Jack_Walk.ani", 0, true, 0.2f);
else
animCtrl.Stop("Models/Jack_Walk.ani", 0.2f);
// Set walk animation speed proportional to velocity
animCtrl.SetSpeed("Models/Jack_Walk.ani", planeVelocity.Length * 0.3f);
// Reset grounded flag for next frame
onGround = false;
}