【发布时间】:2017-05-22 02:29:52
【问题描述】:
我得到了一个相对于相机的运动。像超级马里奥 64 等。首先我使用 CharacterController 制作它,但我想要默认的碰撞检测,所以我需要使用带有 Rigidbody 的碰撞器。
我的代码如下所示:
public class PlayerMovementController : PlayerCommonController
{
PlayerMovementData data; // data class
PlayerMovementView view; // animation, etc. ... class
float turnSmoothVelocity;
float speedSmoothVelocity;
private void Start()
{
data = new PlayerMovementData();
view = new PlayerMovementView();
}
private void Update()
{
Vector2 inputDirection = (new Vector2(data.InputHorizontal, data.InputVertical)).normalized; // get the inputs
if (Input.GetButtonDown("Jump"))
{
if (data.PlayerCharacterController.isGrounded) // player is on ground?
data.VelocityY = Mathf.Sqrt(-2 * data.PlayerGravity * data.JumpPower);
}
if (inputDirection != Vector2.zero) // Rotate the player
{
transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(
transform.eulerAngles.y,
Mathf.Atan2(inputDirection.x, inputDirection.y) * Mathf.Rad2Deg + data.CameraTransform.eulerAngles.y,
ref turnSmoothVelocity,
GetModifiedSmoothTime(data.TurnSmoothTime));
}
data.CurrentMovementSpeed = Mathf.SmoothDamp( /* Set the movementspeed */
data.CurrentMovementSpeed,
data.MovementSpeed * inputDirection.magnitude,
ref speedSmoothVelocity,
GetModifiedSmoothTime(data.SpeedSmoothTime));
data.VelocityY += data.PlayerGravity * Time.deltaTime; // vertical velocity
Vector3 velocity = transform.forward * data.CurrentMovementSpeed + Vector3.up * data.VelocityY; // set the players velocity
data.PlayerCharacterController.Move(velocity * Time.deltaTime); // Move the player
data.CurrentMovementSpeed = (new Vector2(data.PlayerCharacterController.velocity.x, data.PlayerCharacterController.velocity.z)).magnitude; // Calc movementspeed
if (data.PlayerCharacterController.isGrounded) // Set the vertical vel. to 0 when grounded
data.VelocityY = 0;
}
float GetModifiedSmoothTime(float smoothTime) // Handle the movement while in air
{
if (data.PlayerCharacterController.isGrounded)
return smoothTime;
if (data.AirControlPercentage == 0)
return float.MaxValue;
return smoothTime / data.AirControlPercentage;
}
}
因此,将所有 CharacterController 变量替换为 Rigidbody 关键字似乎是显而易见的。但是说到
data.PlayerCharacterController.Move(velocity * Time.deltaTime);
我不知道在那里替换什么。当我没有CC,只有一个碰撞器和一个刚体时,我从地上掉了下来。这可能是因为代码..
有人可以帮我吗?
【问题讨论】: