【发布时间】:2022-01-20 00:38:33
【问题描述】:
我正在制作一个 2D 节奏游戏,我想要它,所以当箭头与按钮碰撞并按下右键时,我的分数系统中将添加一个点。现在无论箭头是否与按钮碰撞,都会在按下键时添加一个点。
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Unity.VisualScripting;
using UnityEngine.UIElements;
public class NoteObject : MonoBehaviour
{
public bool canBePressed;
public KeyCode keyToPress;
public ScoreManager scoreManager;
private void Start()
{
}
void Update()
{
if (Input.GetKeyDown(keyToPress))
{
if (canBePressed)
{
gameObject.SetActive(false);
}
}
}
void OnTriggerEnter2D(Collider2D other)
{
GameObject tempObject = other.GameObject();
if(tempObject.tag == "Activator")
{
Debug.Log("collided(:");
canBePressed = true;
ScoreManager.Instance.AddPoint();
}
}
void OnTriggerExit2D(Collider2D other)
{
GameObject tempObject = other.GameObject();
if (tempObject.tag == "Activator")
{
canBePressed = false;
}
}
} ```
【问题讨论】:
-
other.GameObject();这根本不应该编译...你的意思是other.gameObject...? -
Right now a point is added just when the key pressed no matter if the arrow has collided with the button....在我看来,现在似乎为碰撞添加了一个点,根本没有任何按键...因为这就是您调用ScoreManager.Instance.AddPoint();的方式。 ..
标签: c# visual-studio unity3d game-engine