2016-07-07 3 views
1

私はキャラクターコントローラースクリプトで作業していますが、すべてうまくいきますが、プレイヤーが落ち始めると突然急激に下がります。Unityの滑らかな落ち - C#

using UnityEngine; 
using System.Collections; 

public class CharacterController : MonoBehaviour { 

    public float inputDelay = 0.1f; 
    public float forwardVel = 12; 
    public float rotateCel = 12; 
    public float JumpHeight = 20; 
    public Vector3 Gravity = new Vector3 (0, -180, 0); 
    public bool CanPress; 
    private float jumpTime; 
    public float _initialJumpTime = 0.4f; 
    //[HideInInspector] 
    public bool isGrounded; 
    Quaternion targetRotation; 
    Rigidbody rBody; 
    Vector3 forwardInput, turnInput; 
    public bool HasJumped; 

    public Quaternion TargetRotation 
    { 
     get {return targetRotation;} 
    } 
    // Use this for initialization 
    void Start() { 
     Physics.gravity = Gravity; 
     targetRotation = transform.rotation; 
     if (GetComponent<Rigidbody>()) 
      rBody = GetComponent<Rigidbody>(); 
     else { 
      Debug.LogError("Character Needs Rigidbody"); 
     } 

    // forwardInput = turnInput = 0; 
     forwardInput = turnInput = Vector3.zero; 
    } 

    // Update is called once per frame 
    void Update() { 

     GetInput(); 
     //Turn(); 

     if (CanPress == true) { 
      if (Input.GetKeyDown (KeyCode.Space)) { 
       HasJumped = true; 
       jumpTime = _initialJumpTime; 
      } 
     } 

     if (HasJumped == true) { 
      rBody.useGravity = false; 
      jumpTime -= 1 * Time.deltaTime; 
      if (jumpTime > 0) { 
      Jump(); 
      } 
      else { 
       HasJumped = false; 
       rBody.useGravity = true; 
      } 
     } 
    } 


    void GetInput() { 
     //forwardInput = Input.GetAxis ("Vertical"); 
     //turnInput = Input.GetAxis ("Horizontal"); 
     forwardInput = new Vector3 (Input.GetAxis ("Horizontal") * rotateCel, 0, Input.GetAxis ("Vertical") * forwardVel); 
     forwardInput = transform.TransformDirection (forwardInput); 
     if (Input.GetKeyUp (KeyCode.Space)) { 
      //HasJumped = false; 
     } 


    } 

    void Jump() { 

      Vector3 up = transform.TransformDirection (Vector3.up); 
     GetComponent<Rigidbody>().AddForce (up * 5, ForceMode.Impulse); 

    } 
    void FixedUpdate() { 

     Run(); 
    } 
    void Run() { 
     if (Mathf.Abs (10) > inputDelay) { 
      //Move 
      //rBody.velocity = transform.forward * forwardInput * forwardVel; 
      rBody.velocity = forwardInput; 
     } else { 
      //zero velocity 
      rBody.velocity = Vector3.zero; 

     } 

    } 
    void Turn() { 
    // targetRotation *= Quaternion.AngleAxis (rotateCel * turnInput * Time.deltaTime, Vector3.up); 
    // transform.rotation = targetRotation; 
    } 

    void OnTriggerEnter(Collider col) { 
     isGrounded = true; 
     CanPress = true; 
    } 

    void OnTriggerExit(Collider col) { 
     isGrounded = false; 
     CanPress = false; 
    } 

} 

私のキャラクターは、重力を利用し、X、Y、回転のためのZ制約を持つ剛体attactchesを持っている - 私は徐々に落下するプレーヤーをしたいと思い、これは私の性格コントローラスクリプトです。

  • プレーヤーはスムーズに上がって滑らかに落ちますが、2人の間の移行は非常に急で突然です。

ありがとうございました。 :)

+0

独自のキャラクターコントローラーを構築していますか?もしあなたがUnity Standard Assetsの一部として組み込みのキャラクターコントローラーを使用していない理由について興味があるなら、これはあなたのためにすべてです:-) – Tom

+0

こんにちは@Tom、私はこのすべてを私は自分のゲームのニーズに合うようにスクリプトを変更するオプションを私に与えてくれます。 –

答えて

1

さて、私はあなたのコードを取って遊んだ。

あなたのRun()機能では、あなたのジャンプに影響していたrigidbodyの速度を変更していたと思います。

私はそのことを取り入れ、スクリプトを少し改善してテストしました。これをrigidbodyのカプセルに貼り付け、床に床下にbox colliderのヒットスペースを付けて、スムーズに飛びます。

新しい(っぽい)スクリプト:ポイントの

using UnityEngine; 
using System.Collections; 

public class CharacterController : MonoBehaviour 
{ 
    public float inputDelay = 0.1f; 
    public float forwardVel = 12; 
    public float rotateCel = 12; 
    public float jumpHeight = 10; 
    private float jumpTime; 
    public float _initialJumpTime = 0.4f; 
    //[HideInInspector] 
    public bool isGrounded; 
    Quaternion targetRotation; 
    Rigidbody rBody; 
    Vector3 forwardInput, turnInput; 
    public bool canJump; 

    public Quaternion TargetRotation 
    { 
     get { return targetRotation; } 
    } 

    void Start() 
    { 
     targetRotation = transform.rotation; 
     if (GetComponent<Rigidbody>()) 
      rBody = GetComponent<Rigidbody>(); 
     else 
     { 
      Debug.LogError("Character Needs Rigidbody"); 
     } 

     // forwardInput = turnInput = 0; 
     forwardInput = turnInput = Vector3.zero; 
    } 

    void Update() 
    { 
     GetInput(); 
     //Turn(); 

     if (Input.GetKeyDown(KeyCode.Space) && canJump) 
     { 
      rBody.AddForce(Vector3.up * jumpHeight, ForceMode.Impulse); 
     } 
    } 


    void GetInput() 
    { 
     //forwardInput = Input.GetAxis ("Vertical"); 
     //turnInput = Input.GetAxis ("Horizontal"); 
     forwardInput = new Vector3(Input.GetAxis("Horizontal") * rotateCel, 0, Input.GetAxis("Vertical") * forwardVel); 
     forwardInput = transform.TransformDirection(forwardInput); 
    } 

    void FixedUpdate() 
    { 
     //Run(); 
    } 

    void Run() 
    { 
     //HERE YOU SET THE RIGIDBODYS VELOCITY, WHICH IS CAUSING YOUR JUMP TO NOT WORK PROPERLY. DO NOT MODIFY THE VELOCITY 
     //OF A RIGIDBODY 
     if (Mathf.Abs(10) > inputDelay) 
     { 
      //Move 
      //rBody.velocity = transform.forward * forwardInput * forwardVel; 
      rBody.velocity = forwardInput; 
     } 
     else 
     { 
      //zero velocity 
      rBody.velocity = Vector3.zero; 
     } 
    } 

    void Turn() 
    { 
     // targetRotation *= Quaternion.AngleAxis (rotateCel * turnInput * Time.deltaTime, Vector3.up); 
     // transform.rotation = targetRotation; 
    } 

    void OnCollisionEnter(Collision col) 
    { 
     isGrounded = true; 
     canJump = true; 
    } 

    void OnCollisionExit(Collision col) 
    { 
     isGrounded = false; 
     canJump = false; 
    } 
} 

カップル:

  • 名前あなたの変数inThisKindOfFashion(jumpHeightisOnGroundcamelCaseExample)、大文字で始まる変数を持ちますGravityJumpHeightのように混乱することがあります。

  • 誰かが一度私の質問に答えると、あなたが何をやっているかわからない限り、剛体の速度を変更しないでください!奇妙だと思うけど、そのアドバイスに続いて以来、私は一度も問題がなかった!

  • スクリプトでは、OnTriggerではなくOnCollisionを使用しました。あなたのcapsuleの下にbox colliderの床を置いてcolliderrigidbodyとこのスクリプトをオンにすると、あなたのキャラクターは地上で止まるでしょう。トリガーを使用すると、彼は落ち着きます(少なくとも私の経験では)

ハッピーコーディング!:-)あなたのコメントに応答する


編集

は、「どのように私はプレーヤーを動かす勧めない」

する運動は、様々な異なる方法で行うことができますしかし、私は少し違ったことを少しずつ行う必要がなければ、いつもこの1つを使います:

void Update() 
{ 
    if (Input.GetKeyDown(KeyCode.LeftArrow)) 
    { 
     transform.position += Vector3.left * speed * Time.deltaTime; //speed could be 5f, for example (f means float); 
    } 

    // then do the same for other directions, RightArrow -.right, UpArrow - .forward, DownArrow - .back 
} 

をあなたがより高いまたはより小さなジャンプのためjumpHeight変数を変更することができ、「どのように私は、プレイヤーがジャンプどのくらいの速を調整します」。より速く落ちることが速くなる場合は、編集>プロジェクト設定>物理>に進み、Yの重力を-20などの小さなものに変更します。

チュートリアル

here彼らはチュートリアルの多種多様を持っている、とあなただけのビデオ、次のそれを一緒に置くことができるように、サンプルプロジェクトが付属していたものを持っていても見つけることができます。

+0

うわー、ありがとう@トム、私は心にそのアドバイスを取る:)私はコードをテストし、それが動作するかどうかを教えてくれます。 :) –

+0

問題ありません! :-) – Tom

+0

ちょっと@Tomジャンプはすぐにうまくいきます。ほんの少しの質問 - Rigidbodyを使用するか、Vector3.Lerpを使用するだけで、プレーヤーを移動することをどのように提案しますか?どのくらい速くプレイヤーがジャンプするかを調整するにはどうすればよいですか?13歳で、コーディングで多くの経験がないので、C#コーディングを詳しく教えるWebサイトやチュートリアルはありますか?あなたの時間と助けをありがとう、ありがとう。あなたにハッピーコーディング! :D –

関連する問題