2015-05-30 23 views
1

解決 - 我最終刪除了playerController.SimpleMove並且有playerController.Move控制所有玩家的移動。跳牆高度隨着每次跳躍而降低

所以我一直在試圖讓我的角色牆跳到牆壁之間。然而,我已經能夠做到這一點,每次成功的跳牆之後,下一次跳躍的高度就會下降。這就開始了牆壁向下跳躍的地步。我不知道爲什麼這樣做。在每次按空格鍵之前,我將移動Vector3的值重置爲零,然後重新將正確的值應用於跳轉。我甚至已經通過控制檯查看垂直更改,並且moveDirection和moveAmount都無法獲得足夠大的值更改以實現此目的。

因爲我只是試了一下,下面是影響播放器的所有代碼。

public float rotateSpeed = 3.0f; 
public float walkSpeed = 5.0f; 
public float runSpeed = 15.0f; 
public float jumpSpeed = 10.0f; 
public float acceleration = .05f; 

public bool running = false; 
public bool jumping = false; 
public bool falling = false; 
public bool onWall = false; 
public bool wallJumping = false; 
public bool stay = false; 

private CharacterController playerController; 
private Animator playerAnimator; 
private float speed; 
private Vector3 moveAmount; 
private float animationSpeed; 

private float currentSpeed; 
private float targetSpeed; 
private float moveSpeed; 

float gravity = 10f; 
private Vector3 moveDirection = Vector3.zero; 


// Use this for initialization 
void Start() { 
    playerController = GetComponent<CharacterController>(); 
    playerAnimator = GetComponent<Animator>(); 
} 

// Update is called once per frame 
void Update() { 
    transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0); 
if (onWall) 
    { 
     playerAnimator.SetBool("Wall Holding", true); 
     jumping = false; 
    } 
    else 
    { 
     playerAnimator.SetBool("Wall Holding", false); 
    } 

    if (stay) 
    { 
     moveDirection = Vector3.zero; 
     moveAmount = Vector3.zero; 
    } 

    #region Player Controls 
    if (Input.GetButton("Vertical") && !wallJumping) 
    { 
     moveSpeed = (Input.GetKey(KeyCode.LeftShift)) ? runSpeed : walkSpeed; 
     moveAmount = transform.TransformDirection(Vector3.forward); 
    } 
    else 
    { 
     moveSpeed = 0; 
     moveAmount = Vector3.zero; 
    } 

    if (playerController.isGrounded){ 
     moveDirection = Vector3.zero; 
     jumping = false; 
     wallJumping = false; 
     onWall = false; 
     stay = false; 
    } 
    else 
    { 
     if (!stay) 
      onWall = false; 
    } 

    if (Input.GetKeyDown(KeyCode.Space)) 
    { 
     if (playerController.isGrounded) 
     { 
      moveDirection.y = jumpSpeed; 
      jumping = true; 
     } 
     else if (onWall) 
     { 
      moveDirection = Vector3.zero; 
      moveAmount = Vector3.zero; 
      wallJumping = true; 
      moveDirection += transform.forward * jumpSpeed * 2f; 
      moveDirection += transform.up * jumpSpeed * 2f; 
      stay = false; 
     } 
    } 

    if (Input.GetKeyDown(KeyCode.F)) 
    { 
     if (playerAnimator.GetBool("fight")) 
     { 
      playerAnimator.SetBool("fight", false); 
     } 
     else 
     { 
      playerAnimator.SetBool("fight", true); 
     } 
    } 
    #endregion Player Controls 

    moveDirection.y -= gravity * Time.deltaTime; 
    Debug.Log(moveDirection.x); 
    targetSpeed = Input.GetAxis("Vertical") * moveSpeed; 
    currentSpeed = SpeedFactor(targetSpeed, currentSpeed, acceleration); 

    if (currentSpeed > walkSpeed) 
     running = true; 
    else 
     running = false; 

    playerAnimator.SetFloat("movespeed", currentSpeed); 

    if (!stay) 
    { 
     playerController.SimpleMove(currentSpeed * moveAmount); 
     playerController.Move(moveDirection * Time.deltaTime); 
    } 
} 

private float SpeedFactor (float targetSpeed, float currentSpeed, float dilation) { 
    if (currentSpeed < targetSpeed) { 
     currentSpeed += dilation; 
     if (currentSpeed > targetSpeed) { 
      currentSpeed = targetSpeed; 
     } 
    } 
    else if (currentSpeed > targetSpeed) { 
     currentSpeed -= dilation; 
     if (currentSpeed < targetSpeed) { 
      currentSpeed = targetSpeed; 
     } 
    } 
    return currentSpeed; 
} 

void OnControllerColliderHit (ControllerColliderHit hit) { 
    if (hit.gameObject.tag == "JumpingWall" && !playerController.isGrounded && !onWall && (jumping || wallJumping)) 
    { 
     onWall = true; 
     transform.Rotate(0,180,0); 
     stay = true; 
    } 
} 

回答

0

最簡單的解釋是,transform.forward和/或transform.up載體開始與你的期望隨着時間的推移出現分歧。這可以解釋在你應用下面的幾行時,它最終會如何「倒退」。你確定這些載體的方向是否與跳牆碰壁後(在下一次跳躍之前)一樣?

moveDirection += transform.forward * jumpSpeed * 2f; 
moveDirection += transform.up * jumpSpeed * 2f; 
+0

感謝您的回覆。至於你的問題,是的,我確信他們是。我甚至將兩行設置爲=而不是+ =,並且發生了相同的結果。我已經做了一些進一步的測試,似乎高度似乎是向後跳躍的一個因素,但我不知道爲什麼它會是一件事情。 – Kyther

+0

'+ ='vs'='不應該有所作爲,因爲您在這兩個賦值之前將「moveDirection」置零。你可以嘗試用'moveDirection.y = jumpSpeed * 2f;'(最後一個賦值語句)替換'moveDirection + = transform.up * jumpSpeed * 2f;'來看看它有什麼不同。 '.y'向量應該是不變的,而'transform.up'向量將取決於跳線的方向。在這兩個賦值語句之後,我會記錄/檢查** moveDirection的所有3 **組件。然後你會看到這些力如何分配給每個不變的3D矢量方向(x,y,z)。 –

+0

感謝您提供的幫助。但我設法通過刪除playerController.SimpleMove來解決它。我發現它影響了身高。我真的應該早些時候刪除它,但是我在這樣做時遇到了麻煩。哦,你的換線也不會對問題產生影響。不過謝謝。 – Kyther