I am making a 2D Rhythm Game and I want it so when the arrow collides with the button and the right key is pressed a point will be added the my score system. Right now a point is added just when the key pressed no matter if the arrow has collided with the button.
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;
}
}
} ```
CodePudding user response:
Regarding "Right now a point is added just when the key pressed no matter if the arrow has collided with the button." Two considerations:
1.- According to the code the behaviour is that the score is added if the arrow has collided with the button, no matter if the key is pressed in the first collision. Because the score is added in the OnTriggerEnter2D
.
2.- Then you gameObject.SetActive(false);
in the Update
so the OnTriggerExit2D
does not take place and canBePressed
is left to true
.
"Note: Trigger events will be sent to disabled MonoBehaviours, to allow enabling Behaviours in response to collisions." from the documentation. So you would be disabling the gameObject for each keyPress but no more points would be added according to the shown code because ScoreManager.Instance.AddPoint();
is in the OnTriggerEnter2D
that will not take place anymore.
So please let us know if the explanation of the code behaviuour is correct.
To my understanding you need to add the score when both conditions are met,
1.- While the collision is taking place (canBePressed == true
)
2. Key is pressed (Input.GetKeyDown(keyToPress) == true
)
So the ScoreManager.Instance.AddPoint();
make more sense in the Update
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);
ScoreManager.Instance.AddPoint(); //put here
}
}
}
void OnTriggerEnter2D(Collider2D other)
{
GameObject tempObject = other.GameObject();
if(tempObject.tag == "Activator")
{
Debug.Log("collided(:");
canBePressed = true;
//ScoreManager.Instance.AddPoint(); //removed
}
}
void OnTriggerExit2D(Collider2D other)
{
GameObject tempObject = other.GameObject();
if (tempObject.tag == "Activator")
{
canBePressed = false;
}
}
}