0
\$\begingroup\$

It was working fine last night but it doesn't work today, the zJumpCount stays at zero after I jump now. If the zJumpcount stays at zero my double jump code doesn't work. I even ctrl+z'd back to the beginning of the session with no luck.

public class zPlayerMovement : MonoBehaviour {

  public float zMoveSpeed;
  public float zJumpThrust;          
  public bool IsGrounded; 
  public float zJumpCount;
  private Rigidbody zRB;    

  void Start () {
    zRB = GetComponent<Rigidbody>();
  }

  void Update () {

    if (IsGrounded == true) 
      zJumpCount = 0f;

    if (Input.GetKey(KeyCode.A))
      zRB.transform.Translate(Vector3.left * zMoveSpeed);

    if (Input.GetKey(KeyCode.D))
      zRB.transform.Translate(Vector3.right * zMoveSpeed);

    if(Input.GetButtonDown("Jump") && IsGrounded == true)
    {
      zRB.AddForce(Vector3.up * zJumpThrust, ForceMode.VelocityChange);
      zJumpCount++;
    }

    if (IsGrounded == false && zJumpCount > 0 && zJumpCount < 2)
    {
      if (Input.GetButtonDown("Jump"))
      {
        zRB.AddForce(Vector3.up * zJumpThrust, ForceMode.VelocityChange);
        zJumpCount++;
      }
      if (IsGrounded == false && zJumpCount >= 2)
      {
        if (Input.GetButtonDown("Jump"))
          return;
      }
    }
  }

  void OnCollisionStay (Collision collisionInfo)
  {
    IsGrounded = true;
  }

  void OnCollisionExit (Collision collisionInfo)
  {
    IsGrounded = false;
  }
}
\$\endgroup\$
2
  • \$\begingroup\$ Why is your zJumpCount a float instead of an int? (This could create issues with your comparisons because of floating point errors.) \$\endgroup\$ – Vaillancourt Aug 1 '15 at 19:17
  • \$\begingroup\$ I made it an int but that didn't change anything in regards to the zJumpCount function - still doesn't work correctly. NOTE: If I comment out if (IsGrounded == true) zJumpCount = 0f; the counter works. \$\endgroup\$ – subc0der Aug 2 '15 at 0:46
0
\$\begingroup\$

I never figured out why it stopped working but I found another way to make it work properly. I deleted the first statement in...

void Update ()
if(IsGrouned == true){zJumpCount = 0;}

...then I added this to the double jump

if (IsGrounded == false && zJumpCount >= 2)
{
    **zJumpCount = 0;**
    if (Input.GetButtonDown("Jump"))
        return;
 }

Thanks for the help! :)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.