2016-10-31 6 views
0

の自然代わりに剛体を遅くするためにどのように私はこれを使用しています:ユニティ5:急停止

void Update(){ 
    if(leftJoystick){ 
     rb.AddRelativeForce(0, 0, 400f, ForceMode.Impulse); 
    } 
} 

は(ジェットパックを使用して)空気中で前方に私のキャラクターを移動させること。私がジョイスティックを手放すと、私のキャラクターはすぐに止まります。私が望むのは、ジョイスティックを放して時間が遅くなって徐々に止まってからキャラクターが前進し続けるということです。私のキャラクターが壁に当たったように止まるのは馬鹿に見えます。ジョイスティックを離した後に物理学が私の性格を減速させるように、どのような力/速度/変換を使用するべきですか?

マイ剛体の設定:

  • 質量:100
  • ドラッグ:0
  • 角度ドラッグ:
  • 衝突を補間:0.05
  • は重力
  • 動未補間を使用します検出:ディスクリート
  • フリーズ位置:なし
  • フリーズローテーション:すべての

文字も引き金と全く物理学材料を持っていないされていないカプセルコライダーを持っています。

編集:ここでは、単純な線形が遅く行うことができます私のコードのいずれかの紛争

using UnityEngine; 
using System.Collections; 
using UnityStandardAssets.CrossPlatformInput; 

public class JetpackController : MonoBehaviour{ 

    Rigidbody rb; 

    // controller axes 
    [SerializeField] 
    public string speedAxis = "Vertical"; 
    public string rotateAxis = "HorizontalRotate"; 

    [SerializeField] 
    public float forwardSpeed; 
    public float upSpeed; 
    public float rotateSpeed; 
    public float rotationIgnoreZone; 
    public float MAX_HEIGHT; 

    float speedInput; 
    float rotateInput; 
    public bool leftJoystick; 
    public bool rightJoystick; 

    ParticleSystem jetpackFire1; 

    Vector3 originalGravity; 

    // access Joystick script in Standard assets to detect when right joystick is held down 
    Joystick joystick_left; 
    Joystick joystick_right; 

    void Start() { 
     if (GetComponent<Rigidbody>()) { 
      rb = GetComponent<Rigidbody>(); 
     } else { 
      Debug.LogError ("Player needs to have a rigidbody."); 
     } 

     // get Joysticks 
     if (GameObject.Find ("MobileJoystick_left").GetComponent<Joystick>()) { 
      joystick_left = GameObject.Find ("MobileJoystick_left").GetComponent<Joystick>(); 
     } else { 
      Debug.LogError ("Either gameobject with name 'MobileJoystick_right' does not exist in the scene or it does not have script called Joystick.cs attached to it."); 
     } 
     if (GameObject.Find ("MobileJoystick_right").GetComponent<Joystick>()) { 
      joystick_right = GameObject.Find ("MobileJoystick_right").GetComponent<Joystick>(); 
     } else { 
      Debug.LogError ("Either gameobject with name 'MobileJoystick_right' does not exist in the scene or it does not have script called Joystick.cs attached to it."); 
     } 

     jetpackFire1 = GameObject.Find("JetpackFire1").GetComponent<ParticleSystem>(); 
     jetpackFire1.playbackSpeed = 5.0f; 

     originalGravity = Physics.gravity; 
    } 

    void FixedUpdate() { 

     GetInput(); 

     // move forward and backward according to left joysticks input 
     if(leftJoystick){ 
      // left joystick held down 
      rb.AddRelativeForce(0, 0, forwardSpeed*speedInput, ForceMode.Impulse); 
     } 

     // if right joystick is used 
     if (rightJoystick) { 
      jetpackFire1.Play(); 

     if (transform.position.y < MAX_HEIGHT) { 
      // allow going up 
      //rb.AddRelativeForce (0, upSpeed, 0, ForceMode.Impulse); 
      rb.AddForce(transform.forward * forwardSpeed); 
     } else if (transform.position.y >= MAX_HEIGHT) { 
      // prevent player to go any further up and also falling 
      Physics.gravity = Vector3.zero; // no gravity to prevent player from falling 
      rb.velocity = Vector3.zero; 
     } 
      // rotate 
      // always keep rotating (player can go past look at joystick dir) 
      if (rotateInput >= rotationIgnoreZone || rotateInput <= -rotationIgnoreZone) { 
       transform.Rotate(Vector3.up * rotateInput, Time.deltaTime * rotateSpeed); 
      } 
    } else { 
      jetpackFire1.Stop(); 
      // if right joystick is released 
      Physics.gravity = originalGravity; // normal gravity -> player can fall 
     } 

    } 

public void GetInput(){ 
    speedInput = CrossPlatformInputManager.GetAxis (speedAxis); 
    rotateInput = CrossPlatformInputManager.GetAxis (rotateAxis); 
    // access Joystick.cs script to grab wether right joystick is pressed or not 
    leftJoystick = joystick_left.leftJoystickPressed; 
    rightJoystick = joystick_right.rightJoystickPressed; 
} 

}

+1

剛体を止めることができるのは、 'transform.position.y> = MAX_HEIGHT'のときだけです。 if文が何らかの形で入力されたかどうか確認できますか? – Serlite

+0

私はDebug.Log( "あなたは私を見ることができますか?")を、if文がトリガしたときにチェックするために追加しました。予想通り、プレーヤーの位置が15(MAX_HEIGHT = 15f)を超えた場合にのみ起動します。 y位置15を超えてしまう前にジョイスティックを離すと、問題は残っています。 – Jonathan

+2

MAX_HEIGHT以上になると速度を止めます。おそらく、最大高さに近づくとY方向の旋回を行い、水平方向の速度を正常に続けることができます。 Lerpはパックのための "酸素の欠乏"をシミュレートし、最大の高さに達すると同じ "ヒットウォール"のような感覚を防ぎます。 –

答えて

0

があるかどうかを確認することができますので、完全なコードです。

void Update(){ 
    if(leftJoystick){ 
     rb.AddRelativeForce(0, 0, 400f, ForceMode.Impulse); 
    } 

    if(noJoystick){ 
     rb.velocity = rb.velocity * 0.9; 
    } 
} 

更新機能は、フレームの更新時に呼び出されることに注意してくださいので、私はゆっくりとした速度は、フレームレートに依存しないであろう道FixedUpdate()関数でこれを置くことを示唆しています。 J.Smallはあなたがゆっくりと剛体を遅くするために0.95の周りの数と速度を掛けることができ、言ったように

+0

これを試してみましたが、残念ながらそれは動作しません。ジョイスティックを放した後、速さはすぐに0になるので、乗算するものは何もないようです。 – Jonathan

1

はまだ

をコメントすることはできません。乗算をUpdateメソッドに入れた場合、〜0.95の数値にTime.deltaTimeを乗算することをお勧めします。したがって、速度の低下は渡された時間に比例します。

void Update(){ 
    if(leftJoystick){ 
     rb.AddRelativeForce(0, 0, 400f, ForceMode.Impulse); 
    } 

    if(noJoystick){ 
     rb.velocity = rb.velocity * 0.95 * Time.deltaTime; 
     //Or: rb.velocity.x 
    } 
} 

あなたは複数の構成方法で剛体を遅くしたい場合は、あなたが更新にそれを置く場合(金額はTime.deltaTimeに基づくべきで小さな力の量の代わりに、速度を乗算をも適用することができ - 一度フレームで呼び出される)が、上記の方法は簡単です。