2016-10-05 6 views
-2

ユニティでは、このスクリプトでテストスプライトを作ったが、速くても無作為にジャンプしても、私のキャラクターは地面に落ちるが、着陸が速い。Unity3Dのバリアから私のキャラクターが落ち続ける

using UnityEngine; 
using System.Collections; 

public class code : MonoBehaviour { 
//void FixedUpdate() { 
     /*if (Input.GetKey (KeyCode.LeftArrow)) 
     { 
      int Left = 1; 
     } 
     if (Input.GetKey (KeyCode.RightArrow)) 
     { 
      int Right = 1; 
     } 


     if (Input.GetKey(KeyCode.UpArrow)) 
     { 

     } 
     */ 

public float speed1 = 6.0F; 
public float jumpSpeed = 8.0F; 
public float gravity = 20.0F; 
private Vector3 moveDirection = Vector3.zero; 

void FixedUpdate() { 
    CharacterController controller = GetComponent<CharacterController>(); 
    // is the controller on the ground? 
    if (controller.isGrounded) { 
     //Feed moveDirection with input. 
     moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0,  Input.GetAxis("Vertical")); 
     moveDirection = transform.TransformDirection(moveDirection); 
     //Multiply it by speed. 
     moveDirection *= speed1; 
     //Jumping 
     if (Input.GetButton("Jump")) 
      moveDirection.y = jumpSpeed; 

     if (Input.GetKey (KeyCode.UpArrow)) 
      moveDirection.y = jumpSpeed; 

    } 
    //Applying gravity to the controller 
    moveDirection.y -= gravity * Time.deltaTime; 
    //Making the character move 
    controller.Move(moveDirection * Time.deltaTime); 
} 
} 
+1

あなたは地面とあなたのキャラクターにコライダーをつけましたか? –

+0

あなたは 'CharacterController'のコードを投稿しませんでした。 'isGrounded'が正しくフラグが設定されていないように見えるため、これは問題です。 – KingDan

+0

@halfer私は彼が中を意味すると思う。 – Alox

答えて

2

これは、すべての物理エンジンの共通の問題です。

物理計算はコストのかかる操作であるため、すべてのフレームで実行することはできません。過熱を減らすために、Unityは物理量を0.02秒ごとに更新します(その数は設定可能です)。

この数値を減らすことはできますが、物理エンジンの過熱が少なくなるほど小さくなりますが、それでも100%の精度は保証されません。 100%の精度を達成するには、物理​​エンジンに頼るのではなく、自分で行うべきです。

以下は、私のペットプロジェクトの1つから取得した直線で飛んでいる弾丸の衝突をチェックするコードです。あなたのケースでは

IEnumerator Translate() 
{ 
    var projectile = Projectile.transform; // Cache the transform 
    while (IsMoving) 
    { 
     // Calculate the next position the transform should be in the next frame. 
     var delta = projectile.forward * ProjectileSpeed * Time.deltaTime; 
     var nextPosition = projectile.position + delta; 
     // Do a raycast from current position to the calculated position to determine if a hit occurs 
     RaycastHit hitInfo; 
     if (Physics.Linecast(projectile.position, nextPosition, out hitInfo, CollisionLayerMask)) 
     { 
      projectile.position = hitInfo.point; 
      OnCollision(hitInfo); // Ok, we hit 
     } 
     else projectile.position = nextPosition; // Nope, haven't hit yet 
     yield return null; 
    } 
} 

あなたのキャラクターは、彼が地面に当たるかどうかを判断するためにジャンプし始めたとき、あなただけの彼がいなければ、あなたを介して落下から彼を防ぐために何かをする、レイキャストを行う必要があります。

+0

もしあなたが私に感謝して落ちるのを止めるコードを教えてもらえますか(私はkina a nubです:/)ありがとう! – epic

+0

'rigidbody'をあなたのキャラクターに(CharacterController'と一緒に)添付しましたか?そうであれば、 'CharacterController'が' rigidbody'なしでUnity 4から単独で動作できるので、それを削除することができます。何らかの理由で削除できない場合は、isKinematicをオンにするか、少なくともuseGravityをオフにします。 https://docs.unity3d.com/Manual/class-Rigidbody.html –

関連する問題