本文介绍了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,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;
        }
    }
}

推荐答案

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

这篇关于Input.GetMouseButtonUp不可靠。(统一)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-17 17:16