2017-05-12 4 views
2

私はRigidbodyでメインFPSキャラクターの動きをプログラミングしようとしています。Unity3D Rigidbodyプレーヤーのジャンプが実行中に制限されています

カメラとZQSD変位うまく機能するが、移動が非常に制限されている間ジャンプ:

  • 不動ジャンプ:Y(分)= 1。 Y(max)= 2.7;
  • モバイルジャンプ:Y(最小)= 1; Y(max)= 1.9;

特にプラットフォームゲームでは非常にイライラします。 Personnaly、私は両方のアクションで同じ結果を得たいと思います。

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 

public class FPSMovement : MonoBehaviour { 

    public float walkAcceleration = 150f; 
    public float maxWalkSpeed = 10f; 
    public float jumpVelocity = 500f; 
    public float maxSlope = 45f; 

    private Rigidbody rb; 
    private Vector2 horizontalMovement; 
    private bool isGrounded = false; 
    private bool doubleJumped = false; 

    // Use this for initialization 
    void Start() { 
     rb = GetComponent<Rigidbody>(); 
    } 

    // Update is called once per frame 
    void Update() { 
     if(rb.velocity.magnitude > maxWalkSpeed){ 
      rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxWalkSpeed); 
     } 

     transform.rotation = Quaternion.Euler (0, GetComponentInChildren<FPSCamera>().currentYRotation, 0); 

     // Entrées clavier ZQSD déplaçant le joueur 
     float x = 0, y = 0, z = 0; 
     if (Input.GetKey (KeyCode.Z)) { 
      x += 1f; 
     } 
     if (Input.GetKey (KeyCode.S)) { 
      x -= 1f; 
     } 
     if (Input.GetKey (KeyCode.Q)) { 
      z -= 1f; 
     } 
     if (Input.GetKey (KeyCode.D)) { 
      z += 1f; 
     } 
     // Arrêt prompt du glissement 
     if ((!Input.GetKey (KeyCode.Z) && !Input.GetKey (KeyCode.S) && !Input.GetKey (KeyCode.Q) && !Input.GetKey (KeyCode.D)) 
     && isGrounded) { 
      rb.velocity /= 1.1f; 
     } 
     // Saut du joueur 
     if (Input.GetKey (KeyCode.Space) && isGrounded) { 
      rb.AddForce (0, jumpVelocity, 0); 
     // Deuxième saut 
     } 
     if (Input.GetKey (KeyCode.Space) && !doubleJumped) { 
      rb.AddForce (0, jumpVelocity, 0); 
      doubleJumped = true; 
     } 

     rb.AddRelativeForce (z * walkAcceleration, 0, x * walkAcceleration); 
    } 

    void OnCollisionStay(Collision other) { 
     foreach (ContactPoint contact in other.contacts) { 
      if (Vector3.Angle (contact.normal, Vector3.up) < maxSlope) { 
       isGrounded = true; 
       doubleJumped = false; 
      } 
     } 
    } 

    void OnCollisionExit() { 
     isGrounded = false; 
    } 


} 

は私も気取っに移動する際にそのプレイヤーが置いた印象を持っているが、これは主な問題ではなく、すべてのちょうど印象以上:

は、ここに私のC#コードです。

説明する単語が少ないという単純な問題ですが、このタイプのゲームでは解決しなければならない問題があります。

私は静かに私を助けるために、Stackoverflowの人々、事前の仲間によって1000のおかげで頼むことがあります。

PS:私の母国語がそう、文法や他の英語微妙にあまりにも多くの時間を失うことはありませんしてください、フランス語です:)

答えて

1

あなたがプレイヤーの合計速度をクランプするので、それがあります水平速度のみをクランプする必要があります。最高速度で歩いてジャンプすると、あなたのジャンプ速度が合計速度に加算され、あなたのキャラクターは実際に最大歩行速度よりも高い速度で移動するはずです。これを修正するには、次のようにします:

// Isolate the horizontal component 
Vector3 horizontalVelocity = rb.velocity; 
horizontalVelocity.y = 0; 

if (horizontalVelocity.magnitude > maxWalkSpeed) { 
    // Clamp the horizontal component 
    Vector3 newVelocity = Vector3.ClampMagnitude(horizontalVelocity, maxWalkSpeed); 
    // Keep the original vertical velocity (jump speed) 
    newVelocity.y = rb.velocity.y; 
    rb.velocity = newVelocity; 
} 
関連する問題