2017-08-30 18 views
0

私は2dゲームを作っています。私の問題は、プレイ中に、プレイヤーがジャンプしてBoxCollider2Dの下にいる場合、プレイヤーはジャンプをリリースするまで落ちないということです。ユニティ5:剛体2dが天井に張り付き、速度が上に移動しています

私のプレイヤーのGameObjectは、スプライトレンダラー、重力のあるダイナミックな剛体2d、boxcollider2dで構成されています。

ここにある私の動きスクリプト:

1:

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

public class JumpScript : MonoBehaviour { 

    [Range(1, 10)] 
    public float jumpVelocity; 

    [Range(0,10)] 
    public float speed; 
    private bool jumpQueue = false; 
    private bool boolin=false; 
    void Update() 
    { 

     //Friggin fall, loser 

     //Jumping 
     ///* 
     if (Input.GetButton("Jump")&& GetComponent<Rigidbody2D>    ().velocity.y==0) 
     { 
      GetComponent<Rigidbody2D>().velocity = Vector2.up *  jumpVelocity; 
     } 
     //*/ 
     //jumpQueue? 
     /* 
     if(Input.GetButtonDown("Jump")) 
     { 
      jumpQueue = true; 
     }*/ 
     //Right Movement 
     if (Input.GetKey(KeyCode.D)) 
     { 
      GetComponent<Rigidbody2D>().velocity = new Vector2(1*speed,   GetComponent<Rigidbody2D>().velocity.y); 
      boolin = true; 
     } 
     if(Input.GetKeyUp(KeyCode.D)) 
     { 
      GetComponent<Rigidbody2D>().velocity = new Vector2(0,  GetComponent<Rigidbody2D>().velocity.y); 
      boolin = false; 
     } 
     //Left Movement 
     if (Input.GetKey(KeyCode.A)) 
     { 
      GetComponent<Rigidbody2D>().velocity = new Vector2(-1*speed,  GetComponent<Rigidbody2D>().velocity.y); 
      boolin = true; 
     } 
     if (Input.GetKeyUp(KeyCode.A)) 
     { 
      GetComponent<Rigidbody2D>().velocity = new Vector2(0,  GetComponent<Rigidbody2D>().velocity.y); 
      boolin = false; 
     } 
     //No movement? 
     if (Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.A)) 
     { 
      GetComponent<Rigidbody2D>().velocity = new Vector2(0,  GetComponent<Rigidbody2D>().velocity.y); 
      boolin = false; 

     } 

     //Time to handle animation, boios. 
     Rigidbody2D rb = GetComponent<Rigidbody2D>(); 

     bool schwomp = false; 
     bool schwift = false; 
     if(rb.velocity.y>0) 
     { 
      schwomp = true; 
     } 
     if(rb.velocity.y<0) 
     { 
      schwift = true; 
     } 
     Animator anim = GetComponent<Animator>(); 
     if (boolin) 
     { 
      anim.SetInteger("Boolin", 1); 
      /*if (!anim.GetBool("expand")) 
      { 
       anim.SetBool("expand", true); 
       anim.Play("running"); 
      }*/ 
     } 
     else 
     { 
      anim.SetInteger("Boolin", 0); 
      /* 
      if(anim.GetBool("expand")) 
      { 
       anim.SetBool("expand", false); 
       anim.Play("Idle"); 
      }*/ 
     } 
     if(schwomp) 
     { 
      //anim.SetInteger("Boolin", 2); 
     } 
     if(schwift) 
     { 
      //anim.SetInteger("Boolin", 3); 
     } 
    } 
} 

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

パブリッククラスBetterJumper:MonoBehaviour {

public float fallMultiplier = 2.5f; 
public float lowJumpMultiplier = 2f; 

Rigidbody2D rb; 

void Awake() 
{ 
    rb = GetComponent<Rigidbody2D>(); 

} 

void Update() 
{ 
    if(rb.velocity.y<0) 
    { 
     rb.velocity += Vector2.up*Physics2D.gravity.y*(fallMultiplier-1)*Time.deltaTime; 

    } 
    else if(rb.velocity.y>0&&!Input.GetButton("Jump")) 
    { 
     rb.velocity += Vector2.up * Physics2D.gravity.y * (lowJumpMultiplier - 1) * Time.deltaTime; 
    } 
} 
} 

は、事前にどうもありがとうございます!

+0

コードを正しくフォーマットしてください。 – Soviut

+0

あなたは問題が何であるか書きましたが、期待される結果は何も提供していませんでした.... – Programmer

答えて

3

あなたはInput.GetKey()を使用しています。これはすべてのフレームでキーをポーリングします。これは、あなたがジャンプしている時間が長くなればなるほど、速度が増すことを意味します。あなたは効果的にジャンプ力ではなくジェットパックを構築しました。

Input.GetKeyDown()を使用する必要があります。これは、キーを押したときに1回だけ発火し、再度放してから再度押す必要があります。次に、速度に連続的に加算するのではなく、文字を飛ばすために、RigidBody.AddForce()を使用して1つの十分に強い垂直力を適用する必要があります。

さらに、実際にはGetComponent<Rigidbody2D>()コールの結果をキャッシュに入れておく必要があります。これは、スクリプトが起動または起動しても継続的に呼び出されない場合です。これらの呼び出しのそれぞれは、処理時間がかかります。また、物理学にはFixedUpdate()を使用する必要があります。

public class ExampleClass : MonoBehaviour { 
    public float thrust; 
    public Rigidbody rb; // make the rigidbody variable available anywhere in the class 

    void Start() { 
     // cache the rigidbody component to the variable once on start 
     rb = GetComponent<Rigidbody>(); 
    } 

    void FixedUpdate() { 
     // use the variable reference from now on rather than making GetComponent calls 
     rb.AddForce(transform.up * thrust); 
    } 
} 
+0

助けてくれてありがとう!私は可能なときにこれを間違いなく試みます! – TinFellow

0

ソビエトの答えはそれを非常にうまく説明していますが、もっと知る必要があります。ベロシティを直接操作しているので、重力を含めた力の効果を上書きします(手動で適用しているにもかかわらず)。 documentationを参照してください。

ソビエトの答えで示されているように、物理エンジンが速度を決定させるように力と衝動を加えなければなりません。ベロシティを直接設定する必要があるのは、意図的に非現実的な物理(すなわちレトロプラットフォーマー)を持つシミュレーションを構築するときだけです。そのような場合であっても、動きを生み出すすべての小さな事柄を考慮する必要があるので、そうすることはより多くの作業を作り出すことに留意してください。つまり、物理エンジンを本質的に再発明しています。

+0

面白いことに、私はレトロプラットフォーマーを作成しようとしています。 – TinFellow

+0

うわー、どのように皮肉。どのような場合でも、最大速度を制限し、適用している力で船に乗らないと、ほとんどの場合、レトロプラットフォーマーの感触を得ることができます。がんばろう。 –

+0

これは、私が他の状況に適用できる解決策よりも回避策です。しかし、私はレイキャスティングを使用して、プレーヤが接地されているかどうかを判断しました。そうすれば、彼は地面にいればジャンプするだけです。助けてくれてありがとう! – TinFellow

関連する問題