c# – Input.GetMouseButtonUp不可靠. (统一)

前端之家收集整理的这篇文章主要介绍了c# – Input.GetMouseButtonUp不可靠. (统一)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发一个统一的2D游戏.游戏中的一个功能是使用鼠标左键单击射击弹丸.释放左击后,射弹会被一定量的力量击打,这取决于玩家持续左击的时间.

问题是,有时当我发布左键单击时,游戏似乎没有检测到它,并且代码的发布部分在我再次单击并释放之前不会被执行.我可能听起来不是一个大问题,但输入可靠性将在这个游戏中发挥重要作用.

那么,有没有办法让鼠标输入更加可靠?我已经尝试过使用Input.GetMouseButtonDown和不同类型的条件以使其更可靠,但它还没有成功.先感谢您!

这是我正在使用的代码

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,(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;
        }
    }
}

解决方法

问题是您使用的是FixedUpdate()而不是Update(). FixedUpdate()以固定间隔(每帧不必要)调用.因此,在两次调用FixedUpdate()之间的某个时间可能会错过Input.GetMouseButtonUp().在处理输入时,最好使用Update().

猜你在找的C#相关文章