If I get your code well, you want to increase a power bar while the mouse button is down and when the mouse is up, you perform a shoot with the accumulated power.
Itās hard to keep your code simple with Coroutines, even more when your code contains some conditional branching.
I suggest to use Panda BT for such logic.
Using this tool, Here is a stripped down version of what you want to do:
This a BT script implementing the charge-and-shoot logic:
tree("Root")
sequence
IsMouseButtonPressed(0) // Don't go further if the mouse button is not pressed.
fallback
while IsMouseButtonPressed(0) // Increase power while mouse button is pressed.
IncreasePower
Shoot // Shoot when the mouse button has been released.
And here is the implementation of the tasks:
using UnityEngine;
using Panda;
public class ShootingPower : MonoBehaviour
{
public float power = 0.0f;
public float powerIncreaseRate = 1.0f;
[Task]
void IncreasePower()
{
if (Task.current.isStarting)
power = 0.0f;
power += powerIncreaseRate * Time.deltaTime;
}
[Task]
void Shoot()
{
Debug.Log("Shooting power at:" + power);
Task.current.Succeed();
}
}
Panda BT is a framework based on Behaviour Tree, which is a nice tool to write logic that runs over several frames.
The code always starts neat and simple. Then, as you add lines to implement what you want to do, itās likely to grow towards spaghetti code, even faster when what you want to do involves some timing and branching. And when you read it back, it becomes less and less clear what it does, which makes it easier for bugs to creep in.
With Panda BT, each function focus on one simple task completely decoupled from other functions, which is easier to managed. Then the BT script makes use of these simple tasks to describe the overall logic, which is more readable.
I am more a man with an electric screwdriver, and this problem looks more like a screw to me.
Iād agree that using such power tool for a single screw, might be overkill. But at least it demonstrates how the tool works and you might think twice before using your hand screwdriver (or your hammer) next time you need to ānail downā a large number of screws.
Well thanks everyone. I donāt really want to use the Panda framework as I have no idea what it does yet, and are probably easier ways for something such as this. Iāll try again using Update examples.
Not for a competent developer, however these are the unity forums, where incompetence is rife (its ok, its only because people are new to developmentā¦ not because theyāre stupidā¦mostly ;))
sorry, butā¦
[LIST=1]
[*]tree("Root")
[*] sequence
[*] IsMouseButtonPressed(0) // Don't go further if the mouse button is not pressed.
[*] fallback
[*] while IsMouseButtonPressed(0) // Increase power while mouse button is pressed.
[*] IncreasePower
[*] Shoot // Shoot when the mouse button has been released
[/LIST]
does not look easier to manage, and adds a bunch of overhead mangling your bespoke scripting.
Sorry, but competent developers are not proofed against spaghetti code. Unstructured code or even code that does not makes sens is pretty common. Otherwise peer review or code refactoring would not be necessary. And nobody said people on these forums are stupid, or did you?
Alright, It might looks like overhead-mangled-bespoked-scripts-by-ericbegueā¢ to you because you are not familiar with it (and I got the feeling youāre are not going to be soon :)). Youāve made your mind about this tool and it seems is definitely not something for you. Though, in my opinion, you should really have a look at Behaviour Tree, not particularly to Panda BT, but at the general AI concept. Itās a really good tool to have as a programmer.