Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 不必要地调用OnTiggerExit2D_C#_Unity3d - Fatal编程技术网

C# 不必要地调用OnTiggerExit2D

C# 不必要地调用OnTiggerExit2D,c#,unity3d,C#,Unity3d,我希望你们都做得很好。我一直在遵循一个节奏游戏的统一教程,我发现了这个我无法克服的错误。本质上,我的OnTriggerExit2D被调用得太早了。我会在这篇文章的结尾加上一张照片。我尝试记录游戏对象,似乎我所有的按钮对象都遭受相同的命运。我已经包括了一个我在结论中一直遵循的教程链接。任何有助于解决这个问题的方法都会很有帮助 教程链接: 我的比赛是什么样子的,当我击球时,错过的球就会出现。 调试输出 游戏经理 using System.Collections; using System.Col

我希望你们都做得很好。我一直在遵循一个节奏游戏的统一教程,我发现了这个我无法克服的错误。本质上,我的OnTriggerExit2D被调用得太早了。我会在这篇文章的结尾加上一张照片。我尝试记录游戏对象,似乎我所有的按钮对象都遭受相同的命运。我已经包括了一个我在结论中一直遵循的教程链接。任何有助于解决这个问题的方法都会很有帮助

教程链接:

我的比赛是什么样子的,当我击球时,错过的球就会出现。

调试输出

游戏经理

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameManager : MonoBehaviour
{
    public AudioSource theMusic;
    public bool startPlaying;
    public BeatScroller theBS;
    public static GameManager instance;

    public int currentScore;
    public int scorePerNote = 100;
    public int scorePerGoodNote = 125;
    public int scorePerPerfectNote = 150;
    
    public int currentMultiplier;
    public int multiplierTracker;
    public int [] multiplierTresholds;

    public Text scoreText;
    public Text multiText;

    // Start is called before the first frame update
    void Start()
    {
        instance = this;

        scoreText.text = "Score: 0";
        multiText.text = "Multiplier: x1";
        currentMultiplier = 1;

    }

    // Update is called once per frame
    void Update()
    {
     if(!startPlaying){
         if(Input.anyKeyDown){
             startPlaying = true;
             theBS.hasStarted = true;
             theMusic.Play();
         }
     }
    }

    public void NoteHit(){
        Debug.Log("Note Hit On Time");
        if(currentMultiplier-1 < multiplierTresholds.Length){

        multiplierTracker++;

            if(multiplierTresholds[currentMultiplier-1] <= multiplierTracker){
                multiplierTracker = 0;
                currentMultiplier++;
            }
        }

        multiText.text = "Multiplier: x"+currentMultiplier;

        //currentScore += scorePerNote * currentMultiplier;
        scoreText.text = "Score: "+currentScore;
    }

    public void NormalHit(){
        currentScore += scorePerNote * currentMultiplier;
        NoteHit();
    }

    public void GoodHit(){
        currentScore += scorePerGoodNote * currentMultiplier;
        NoteHit();

    }

    public void PerfectHit(){
        currentScore += scorePerPerfectNote * currentMultiplier;
        NoteHit();
 
    }

    public void NoteMissed(){
        Debug.Log("MISSED!");
        multiText.text = "Multiplier: x1";
    }
}

按钮控制器

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ButtonController : MonoBehaviour
{
    // Start is called before the first frame update
    private SpriteRenderer theSR;
    public Sprite defaultImage;
    public Sprite pressedImage;

    public KeyCode keyToPress;
    void Start()
    {
        theSR = GetComponent<SpriteRenderer>();
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(keyToPress))
        {
            theSR.sprite = pressedImage;
        }

         if(Input.GetKeyUp(keyToPress))
        {
            theSR.sprite = defaultImage;
        }
    }
}

效应对象

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EffectObject : MonoBehaviour
{
    public float lifeTime = 1f;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
       Destroy(gameObject, lifeTime);
    }
}


嗯。。。您是否说调用了
OnTriggerExit2D
?我想当元素仍在彼此内部时,它会被调用?如果是这样的话,我猜你的边界框没有合适的大小或形状。我看到箭头,它们是有一个矩形的边界框还是一个多边形的边界框跟随它们的形状?你所有的边界框大小都正确吗?

多亏了阿德拉斯的评论,我才知道是哪里出了问题

原来我必须检查我的箭头和碰撞器的y位置是否低于我的按钮盒碰撞器的高度。除此之外,我还改造了我的对撞机。我发现这段代码帮了我的忙。感谢大家朝着正确的方向努力

noteObject->new OnTriggerExit2D


 private void OnTriggerExit2D(Collider2D other)
    {
        if(other.tag == "Activator" && transform.position.y < -0.32)
        {
            Debug.Log("Exited collider on game object: "+ other.gameObject.name);
            canBePressed = false;
            GameManager.instance.NoteMissed();
            Instantiate(missedEffect,transform.position, missedEffect.transform.rotation);

        }
    }


私有无效OnTiggerExit2D(碰撞R2D其他)
{
if(other.tag==“Activator”&&transform.position.y<-0.32)
{
Log(“退出游戏对象上的碰撞器:“+other.gameObject.name”);
canBePressed=false;
GameManager.instance.NoteMissed();
实例化(misseDefect、transform.position、misseDefect.transform.rotation);
}
}

一般情况:为什么在
EffectObject.Update
中调用
Destroy
?只能做一次。。那么,如果你说它被要求提前。。你预计什么时候?那你什么时候不会呢?是否会因为相应对象被销毁而发生这种情况?销毁方法用于粒子效果,该效果与noteObject类中的实例化一起出现。它根据命中类型实例化效果对象,然后删除效果。如果您观看教程,它将向您展示如何工作,您说OnTriggerExit2D调用为early?-它更像是在箭头进入某个按钮区域时调用missed消息,但我仍然能够将箭头注册为命中,因为箭头没有在彩色按钮下方移动。箭头有圆形的Collider2d,颜色按钮有BoxCollider2d和Rigibody2D。我将在这篇文章中添加设置的图片。再次非常感谢你。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EffectObject : MonoBehaviour
{
    public float lifeTime = 1f;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
       Destroy(gameObject, lifeTime);
    }
}


 private void OnTriggerExit2D(Collider2D other)
    {
        if(other.tag == "Activator" && transform.position.y < -0.32)
        {
            Debug.Log("Exited collider on game object: "+ other.gameObject.name);
            canBePressed = false;
            GameManager.instance.NoteMissed();
            Instantiate(missedEffect,transform.position, missedEffect.transform.rotation);

        }
    }