Unity2D有關角色跳躍不靈敏和跳躍時移動y軸速度特別小問題的注意事項和解決方法

*本文原創(chuàng),表明來源可隨意轉載,主要是記錄自己在學習過程中遇到的問題,故不作為教程。如果有大佬愿意指導糾正,區(qū)區(qū)拜謝。
SCDN:車部擬幻
一 .角色跳躍不靈敏:
void Player1Jump()
? ? {
? ? ? ? if(groundDetect&&Input.GetKeyDown(KeyCode.Space))
? ? ? ? {
? ? ? ? ? ? rb.velocity = new Vector2(rb.velocity.x, jumpForce);
? ? ? ? ? ? Debug.Log(111);
? ? ? ? }
? ? }
這部分代碼是沒有問題的,問題出在我把Player1Jump()放在了FixedUpdate(),而FixedUpdate()是游戲時間的0.02s,Input.GetKeyDown(KeyCode.Space)的檢測會出現類似于掉幀的情況,所以會經常狂按空格人物卻不跳躍,這時候只要把Player1Jump()放回Update()就好。
二.跳躍時移動y軸速度特別小:
問題代碼:
void Player1Move()
? ? {
? ? ? ? float x = Input.GetAxis("Horizontal");
? ? ? ?// float y = Input.GetAxis("Vertical");
? ? ? ? //Debug.Log(x);
? ? ? ? if (x == 0)
? ? ? ? {
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? xLast = x;
? ? ? ? }
? ? ? ? rb.velocity = new Vector2(player1Speed * xLast*Time.fixedDeltaTime*50, 0.0f);?
? ? ? ? //transform.Translate(Vector3.right * x * player1Speed * Time.fixedDeltaTime, Space.World);
? ? }
改正后的代碼:
void Player1Move()
? ? {
? ? ? ? float x = Input.GetAxis("Horizontal");
? ? ? ?// float y = Input.GetAxis("Vertical");
? ? ? ? //Debug.Log(x);
? ? ? ? if (x == 0)
? ? ? ? {
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? xLast = x;
? ? ? ? }
? ? ? ? rb.velocity = new Vector2(player1Speed * xLast*Time.fixedDeltaTime*50, rb.velocity.y);?
? ? ? ? //transform.Translate(Vector3.right * x * player1Speed * Time.fixedDeltaTime, Space.World);
? ? }
不難發(fā)現
rb.velocity = new Vector2(player1Speed * xLast*Time.fixedDeltaTime*50, rb.velocity.y);?
這里的參數二被我丟掉了,設置為了0.0f,因此在跳躍時移動自然會丟失y軸上的速度,我們把第二參數設置為rb.velocity.y就沒有問題了。
