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

标签 c# unity3d mouseevent

我正在统一开发一款 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() 可能会在 2 次 FixedUpdate() 调用之间的某个时间丢失。处理输入时最好使用 Update()。

关于c# - Input.GetMouseButtonUp 不可靠。 (统一),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25334862/

相关文章:

c# - 在 Windows 中对 'live' 文件使用 grep

c# - 在Unity C#中使用两个麦克风进行录音

c# - 如何在 NetworkManager (Unity C#) 中设置客户端 GameObject 的变量?

c# - Unity中的LoadScene()函数什么时候改变场景?

jquery - fadeIn() 先显示后淡出

c# - 如何将 T 对象动态转换为 Amazon DynamoDB 文档

C# AForge,程序卡在学习阶段

c# - wpf 附加属性不起作用

javascript - 如何获取向上滚动的 div 的更新顶部位置?

javascript - Bootstrap Accordion 并不总是在 Google map 监听器事件上崩溃