Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input.GetMouseButtonUp is not reliable. (Unity)

I am developing a 2D game in unity. One of the features in the game is to shoot projectiles using the left click of the mouse. Upon releasing the left click the projectile gets fired with certain amount of force that depends on how long the player held the left click.

The problem is that sometimes when I release the left click the game doesnt seem to detect it and the release portion of the code doesnt get executed until I click and release again. I may not sound like a big problem but input reliability will play a fundamental role in this game.

So, is there any way to make mouse input more realiable? I have already tried using Input.GetMouseButtonDown and different kinds of conditionals in order to make it more reliable, but it hasnt worked out. Thank you in advance!

Here is the code I am using:

using UnityEngine;
using System.Collections;

public class ShootPlasma : MonoBehaviour {

    //prefab
    public Transform bulletPrefab;

    //Reloading variables:
    public int numberOfBullets;
    private int numberOfBulletsRecord;

    private bool canShoot=true;

    public float timeToReload;
    private float timeToReloadRecord;

    //direction and bullet Speed variables:
    Transform sightPosition;
    public Vector3 SpawnRiseVector;
    private Vector2 direction;

    public float bulletBoost;
    private float bulletBoostRecord;
    public float MAX_BOOST;


    //Arrow Guide
    public Transform aimingArrow;

    //Helper variables;
    private CharacterController2D charControllerScript;


    void Start () {

        timeToReloadRecord = timeToReload;
        numberOfBulletsRecord = numberOfBullets;
        charControllerScript = transform.parent.GetComponent<CharacterController2D> ();
        bulletBoostRecord = bulletBoost;

        sightPosition = GetComponent<Transform> ();
        aimingArrow.GetComponentInChildren<Renderer>().enabled=false;
    }

    // Update is called once per frame
    void FixedUpdate () {

        if(numberOfBullets<=0){
            canShoot=false;
            if(!canShoot){

                timeToReload-=Time.deltaTime;

                //if the waiting time has ended restart variables.
                if(timeToReload<=0.0f){
                    canShoot=true;
                    timeToReload=timeToReloadRecord;
                    numberOfBullets=numberOfBulletsRecord;
                }
            }
        }


    ////////////////////////////////// SHOOTING CODE ////////////////////////////////////////////
        /// 
        /// MOUSE DOWN
        /////////////////////////////////////////////////////////////////////////////////////////
        else if(Input.GetMouseButton(0)&& canShoot && !Input.GetMouseButtonUp(0)){

            //show the Arrow Guide:

            if(aimingArrow.GetComponentInChildren<Renderer>().enabled!=true){
                aimingArrow.GetComponentInChildren<Renderer>().enabled=true;
            }

            //calculate the distance between the mouse and the sight;
            Vector3 mousePositionRelative=Camera.main.ScreenToWorldPoint(Input.mousePosition);
            direction= new Vector2(mousePositionRelative.x- sightPosition.position.x,
                                   mousePositionRelative.y- sightPosition.position.y).normalized;

            //If Y is less or equal 0:
            if(direction.y<=0.0f && direction.x>=0.0f){
                direction=new Vector2(1.0f,0.0f);
            }
            else if(direction.y<=0.0f && direction.x<0.0f){
                direction=new Vector2(-1.0f,0.0f);
            }


            //Rotate the aiming arrow
            if(charControllerScript.facingFront){
                if(direction.x>=0.0f){
                    aimingArrow.rotation=Quaternion.Euler(new Vector3(0.0f,0.0f,(Mathf.Asin(direction.y/direction.magnitude))*Mathf.Rad2Deg));
                }
                else if(direction.x<0.0f){
                    aimingArrow.rotation=Quaternion.Euler(new Vector3(0.0f,180.0f,(Mathf.Asin(direction.y/direction.magnitude))*Mathf.Rad2Deg));
                }
            }
            else if(!charControllerScript.facingFront){
                if(direction.x>=0.0f){
                    aimingArrow.rotation=Quaternion.Euler(new Vector3(0.0f,180.0f,(Mathf.Asin(direction.y/direction.magnitude))*Mathf.Rad2Deg));
                }
                else if(direction.x<0.0f){
                    aimingArrow.rotation=Quaternion.Euler(new Vector3(0.0f,0.0f,(Mathf.Asin(direction.y/direction.magnitude))*Mathf.Rad2Deg));
                }
            }


            Debug.Log(direction);


            //Charge
            bulletBoost+=Time.deltaTime*bulletBoost;
            if(bulletBoost>=MAX_BOOST){
                bulletBoost=MAX_BOOST;
            }
        }

        ///////////////////////////////////////////////////////////////////////////////////////////////////
        /// MOUSE UP
        /// ///////////////////////////////////////////////////////////////////////////////////////////////
        else if(Input.GetMouseButtonUp(0)&& canShoot && !Input.GetMouseButton(0)){

            //Hide the Arrow Guide:

            if(aimingArrow.GetComponentInChildren<Renderer>().enabled!=false){
                aimingArrow.GetComponentInChildren<Renderer>().enabled=false;
            }


            //Fire
            var shootPrefab= Instantiate(bulletPrefab,sightPosition.position+SpawnRiseVector,Quaternion.identity) as Transform;

            shootPrefab.GetComponent<Rigidbody2D>().AddForce(direction*bulletBoost);
            bulletBoost=bulletBoostRecord;

            //Reduce the Ammo by one:

            numberOfBullets-=1;
        }
    }
}
like image 504
Geronimo Rodriguez Avatar asked Aug 15 '14 22:08

Geronimo Rodriguez


1 Answers

The problem is that you are using FixedUpdate() instead of Update(). FixedUpdate() is called in constant intervals(not necessary each frame). So, the Input.GetMouseButtonUp() may be missed some time between 2 calls of FixedUpdate(). You should better use Update() when you are handling input.

like image 115
jose gabriel Avatar answered Sep 18 '22 16:09

jose gabriel