- Home /
An effect problem
Hello. I have a problem. The player does magic when you hold down the space key. I want the player to detect enemies at a certain distance and play an effect on them while the button is pressed.
For this, I tried the raycast and the instantiate method, but it plays in every frame because it is called inside the update function. But I just want only one effect to play on enemies while the button is pressed. I don't know if I could explain what I mean. But how should I go about it?
I'm new to unity. I have to do this project for my final exam. Thanks in advice.
Here is the picture; cloning effects constantly.
Answer by Harry_Drew · May 04, 2021 at 08:08 PM
private void Update()
{
if (Input.GetKey(KeyCode.Space))
{
//Place your code that you only want to active when the space press
}
}
The code above will make it so that the code inside of the if statement will only be active when the space bar is pressed in. So in your case I would place the raycast code inside of the IF statement so that it's only active when the space bar is pressed down.
Hope this helps
Thank you for your answer. I already did that, but the new effect is constantly cloned while the button is pressed. Is there any way to prevent this?
Your answer
Follow this Question
Related Questions
Unity Raycast2D ignored instantiated Prefab object 0 Answers
How to activate a button? 1 Answer
How instantiate only one prefab at different spawn place ? 0 Answers
Instantiate as a child? 3 Answers