2016-07-07 33 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。

  • 玩家順利升高並順利跌倒,但兩者之間的過渡非常突然和突然。

感謝您的幫助。 :)

+0

你在建立自己的角色控制器嗎?如果你對我爲什麼沒有使用內置的字符控制器作爲Unity標準資產的一部分感興趣,那麼你可以這樣做:-) – Tom

+0

嘿@Tom,我將所有這些作爲一部分因爲我不想一直使用統一的內置腳本,並且創建自己的腳本使我可以選擇更改腳本以適合我的遊戲需求。 –

回答

1

好的,所以我把你的代碼,併發揮了作用。

我認爲問題在於你的Run()函數中,你正在改變影響跳躍的rigidbody的速度。

我已經把那些東西拿出來,稍微改進了你的腳本並測試了它。將它附加到上面的rigidbody膠囊上,下面的地板上有box collider,並且撞上空間,您應該順利跳下。

你的新的(ISH)腳本:

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可能會引起混淆。

  • 正如有人回答了我的問題之一,不修改剛體的速度,除非你知道你在做什麼!似乎很奇怪,但在遵循這個建議之後,我從來沒有遇到過問題!

  • 在您的腳本中,我使用了OnCollision而不是OnTrigger。如果你把地板上box collidercapsule下方有colliderrigidbody並在此腳本,你的角色將停止在地面上。如果你使用觸發器,他會跌倒(至少以我的經驗!)

快樂的編碼!:-)


編輯 要回複意見:

「你怎麼建議我移動玩家」

運動可以以多種不同的方式完成,但我通常會一直使用這一個,除非我需要做一些有點不同的事情:

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

哇,謝謝@Tom,我會把這個建議記在心裏:)我會測試代碼並告訴你它是否工作。 :) –

+0

沒問題! :-) – Tom

+0

嘿@Tom跳躍現在效果很好。只是幾個問題 - 你如何建議我將玩家轉移到Rigidbody或者只是使用Vector3.Lerp?我該如何調整玩家跳躍的速度,因爲我只有13歲,而且沒有太多編碼經驗,所以有詳細教導C#編碼的網站或教程?非常感謝您的時間和幫助。快樂編碼給你! :D –