2017-05-21 10 views
0

カメラに対して動きがあります。 Super Mario 64のようなものです。最初はCharacterControllerで作ったのですが、デフォルトの衝突検出が必要なので、Rigidbodyでコライダーを使用する必要があります。キャラクタコントローラコードをリジッドボディコードに変換するUnity

私のコードは次のように見える:

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変数を置き換えるために明白なようです。しかし、来るとき

data.PlayerCharacterController.Move(velocity * Time.deltaTime); 

私はそこに何を置き換えるか分からない。私がCCを持っていなくても、コライダーとリジッドボディーがいたら、私は地面に落ちます。これはコードのために発生する可能性があります..

誰かが私を助けることができますか?

答えて

1

プレーヤーからCharacterControllerコンポーネントを削除し、Rigidbodyコンポーネントを添付してください。その後、私はあなた添付剛体の参照を作成し、GetComponentの代わりにそれを使用することをお勧めします

GetComponent<Rigidbody>().velocity = velocity * Time.deltaTime; 

を使用しています。

+0

これはほとんどうまく動作します。プレーヤーは非常にゆっくりと落ちています。私は彼を動かすことはできません。私の現在のコードはこれを見ています:https://hastebin.com/axexuwakup.cs私はVelocityYが正しくないと信じています。私のリジッドボディは重力を使用しており、私もそれを計算します。あなたはこれを修正する方法を知っていますか? – Question3r

+0

あなたは物理学の世界にいますので、速度の値を微調整する必要があります(現実に近い)。はい剛体にはそれを計算する必要のない独自の重力があります。 –

関連する問題