Could someone help me with my script (beginner)

I’m very new to programming / scripting and I am trying to go along with the project “Roll-A-Ball” but I’m trying to make it do things that I want so I’m trying to add new things to the playerMovement script but I’m running into issues.

Currently, this is what I have scripted:

using UnityEngine;
using System.Collections;

public class playerMovement : MonoBehaviour
{
    // gloabal floats.
    public GameObject player;
    private Rigidbody rb;
    public float speed;
    private float maxSpeed;
    private float maxJumpHeight;
    private float maxJumps;
    private float jumpNumber;
    private float timer;

    // global booleans
    private bool isGrounded = true;
    private bool jumpIsPos = true;
    //booleans for moving states
    private bool isMovingForward = false;
    private bool isMovingBackward = false;
    private bool isMovingLeft = false;
    private bool isMovingRight = false;

    // Use this for initialization
    void Start()
    {
        rb = GetComponent<Rigidbody>();
        maxSpeed = 10;
        maxJumps = 2;
        maxJumpHeight = 125.0f;
        jumpNumber = 0;
        timer = 2;
    }

    void FixedUpdate()
    {

    }

    void movement()
    {

        if (Input.GetKey(KeyCode.W))
        {
            rb.AddForce(Vector3.left * speed);
            isMovingForward = true;
            //Debug.Log(isMovingForward + " Forwards");
        }
        if (Input.GetKey(KeyCode.A))
        {
            rb.AddForce(Vector3.back * speed);
            isMovingLeft = true;
           //Debug.Log(isMovingLeft + " left");
        }
        if (Input.GetKey(KeyCode.S))
        {
            rb.AddForce(Vector3.right * speed);
            isMovingBackward = true;
            //Debug.Log(isMovingBackward + " backwards");
        }
        if (Input.GetKey(KeyCode.D))
        {
            rb.AddForce(Vector3.forward * speed);
            isMovingRight = true;
            //Debug.Log(isMovingRight + " Right");
        }
    }

    void jumping()
    {
        
        if(Input.GetKeyDown("space"))
        {
            jumpNumber++;
            if(player.transform.position.y <= 0.76 && jumpIsPos && jumpNumber <= 2)
            {
                rb.AddForce(Vector3.up * maxJumpHeight);
                Debug.Log(jumpNumber);

                if (jumpNumber >= 2) // if jumpnumber is greater than 2
                {
                    jumpIsPos = false; // the jump is not possible
                    if(jumpNumber >= 2 && player.transform.position.y <= 0.76f) // however, if the jump number is equal or greater than 2, set a 2 second timer
                    {
                        timer -= Time.deltaTime; // start counting down 
                        if (timer == 0) // and if the timer reaches 0, 
                        {
                            jumpIsPos = true; // make the jump possible again
                            jumpNumber = 0; // and also reset the jumps incremental value.
                            Debug.Log(jumpNumber + "something is wrong here?");
                        }
                    }
                    Debug.Log(jumpNumber);
                }

                if (player.transform.position.y >= 0.76)
                {
                    jumpIsPos = false;
                }
            }
        }
    }

    void Update()
    {
        //movement();
        jumping();
    }
}

I know that I have commented out the “movement();” part but I’ve done that because I’m only trying to get the jump function to work. I hope that you can understand what I am trying to do through the script but in case you do not; I am trying to write it so that the ball can only jump 2 times every 2 seconds. Currently, if I jump it will increment the jump number but the timer doesn’t decrease and the player can still jump straight afterwards. And if they press the spacebar fast enough, they can keep jumping even though the jumpCount > 2?

Any help would be greatly appreciated and if I have made some easy to spot mistakes (to you guys) then I am sorry, I’m still learning :slight_smile:

Kind regards,
Shane.

In this part:

jumpNumber++;
if(player.transform.position.y <= 0.76 && jumpIsPos && jumpNumber <= 2)

You’re calling what’s inside the if statement three times before that if statement evaluates to false because of the end part. You also have jumpNumber >= 2 as an if statement with your timer in it so with the above condition that if statement’s contents will only run once, not enough to make your timer escape out of the top conditional. Right now the top one says this and this and this must be true but if you want this or this then you’re looking for || which is the logical or operator.

Also, one important thing to know is that floating point numbers shouldn’t be compared for equality due to floating point imprecision. If you need to compare them you can use Mathf.Approximatelyor, as in your case, just stick with < and >.