Home > Software design >  How to stop an input function after one use?
How to stop an input function after one use?

Time:12-01

Basically I'm creating a game where you drag and launch the player across the screen to collect gems, the player can also jump (very short) and move left to right. I want the player to be able to move the character left and right and jump, just not drag and shoot but once. The problem is with the code I've got so far all a player needs to do is spam the drag and shoot function over and over to make the player fly. I want to disable this. I want the player to have one chance to drag and launch the player. And this will reset after respawning.

So basically I have the following code. I want to enable the lr (Line Renderer) with Get Mouse Button Down once, and disable it, then re-nabling it on respawn and so on.

    LineRenderer lr;
    Rigidbody2D rb;
    public float power = 5f;
    public float speed = 3.0f;
    Vector2 startDragPos;

if (Input.GetMouseButtonDown(0))
            startDragPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        if (Input.GetMouseButton(0))
        {
            lr.enabled = true;

            Vector2 endDragPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Vector2 _velocity = (endDragPos - startDragPos) * power;

            Vector2[] trajectory = Plot(rb, (Vector2)transform.position, _velocity, 500);

            lr.positionCount = trajectory.Length;

            Vector3[] positions = new Vector3[trajectory.Length];

            for(int i = 0; i < trajectory.Length; i  )
            {
                positions[i] = trajectory[i];
            }

            lr.SetPositions(positions);
        }
        else
        {
            lr.enabled = false;
        }
        

         if (Input.GetMouseButtonUp(0))
         {
            anim.SetTrigger ("normanLaunch");
            soundEffect.Play();
            Vector2 endDragPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Vector2 _velocity = (endDragPos - startDragPos) * power;
            rb.velocity = _velocity;

            fallDetector.transform.position = new Vector3(transform.position.x, fallDetector.transform.position.y);
         }
    }

`

thanks for any and all help!

CodePudding user response:

One way to solve this problem would be to use a boolean variable to track whether the player has used their drag and shoot ability or not. You could initialize this variable to false at the start of the game, and then set it to true when the player uses the ability.

Here is an example of how you could modify your code to do this:

LineRenderer lr;
Rigidbody2D rb;
public float power = 5f;
public float speed = 3.0f;
Vector2 startDragPos;
bool hasShot = false; // This variable tracks whether the player has used their drag and shoot ability

if (Input.GetMouseButtonDown(0))
    startDragPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

if (Input.GetMouseButton(0) && !hasShot) // Only enable the line renderer if the player has not used their ability yet
{
    lr.enabled = true;

    Vector2 endDragPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    Vector2 _velocity = (endDragPos - startDragPos) * power;

    Vector2[] trajectory = Plot(rb, (Vector2)transform.position, _velocity, 500);

    lr.positionCount = trajectory.Length;

    Vector3[] positions = new Vector3[trajectory.Length];

    for(int i = 0; i < trajectory.Length; i  )
    {
        positions[i] = trajectory[i];
    }

    lr.SetPositions(positions);
}
else
{
    lr.enabled = false;
}


if (Input.GetMouseButtonUp(0) && !hasShot) // Only launch the player if they have not used their ability yet
{
    anim.SetTrigger ("normanLaunch");
    soundEffect.Play();
    Vector2 endDragPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    Vector2 _velocity = (endDragPos - startDragPos) * power;
    rb.velocity = _velocity;
    hasShot = true; // Set hasShot to true to indicate that the player has used their ability

    fallDetector.transform.position = new Vector3(transform.position.x, fallDetector.transform.position.y);
}

When the player respawns, you can set the hasShot variable back to false to allow them to use the drag and shoot ability again.

  • Related