示例#1
0
 public void PhysicsUpdate()
 {
     if (physicsEntity == null)
     {
         Debug.LogError("Character: PhysicsUpdate(): physics entity is null");
         return;
     }
     // Based on input, accelerate in direction that's being pressed
     // Horizontal
     if (isMovingLeft)
     {
         inputVelocityX -= stats.accelerationSpeed;
     }
     else if (isMovingRight)
     {
         inputVelocityX += stats.accelerationSpeed;
     }
     else
     {
         inputVelocityX /= MOVEMENT_INPUT_FRICTION;
         // If inputVelocity is sufficiently close to 0
         if (inputVelocityX < 0.001)
         {
             // snap to 0
             inputVelocityX = 0;
         }
     }
     // Vertical
     if (isMovingDown)
     {
         inputVelocityY -= stats.accelerationSpeed;
     }
     else if (isMovingUp)
     {
         inputVelocityY += stats.accelerationSpeed;
     }
     else
     {
         inputVelocityY /= MOVEMENT_INPUT_FRICTION;
         // If inputVelocity is sufficiently close to 0
         if (inputVelocityY < 0.001)
         {
             // snap to 0
             inputVelocityY = 0;
         }
     }
     // Clamp to maximum input speed
     inputVelocityX = Mathf.Clamp(inputVelocityX, -stats.movementSpeed, stats.movementSpeed);
     inputVelocityY = Mathf.Clamp(inputVelocityY, -stats.movementSpeed, stats.movementSpeed);
     // Pass calculated velocity to physics entity
     physicsEntity.AddInputVelocity(inputVelocityX, inputVelocityY);
     physicsEntity.Update();
     if (HasAuthority())
     {
         // Update the local position based on physics simulation
         transform.position = physicsEntity.transformPosition;
     }
     else
     {
         // Move visual representation a bit closer to the correct position
         transform.position = Vector3.Lerp(transform.position, physicsEntity.transformPosition, GetCurrentLerpFactor());
     }
 }