-
If I have 20 bullets with different damage, would I need to make a BulletScript class for example with 20 integers for the damage and 20 public objects the store the different bullets?
-
Would I need to give the enemy a health parameter so I can reduce the enemy health doesn’t matter of course which enemy it is by the damage of the bullet?
- No. But you can, but that would be awful code.
- Depends.
Perhaps you can show us some of your current code, and what you are trying to do. Usually bullets have a single ‘damage’ property that is use to subtract from the enemy’s ‘health’ property upon hit. What have you implemented? Also, you usually loop through all enemies dynamically, and do not store a link to every instane via inspector.
I haven’t started yet, but that’s why I ask what would be the best approach because I already knew that hardcoding something like this is not really good code writing.
I’ve found (and don’t tell my kids :)) that it’s usually better to first give it a try yourself, so you get familiar with the territory, and then ask questions - you now “better to act and ask forgiveness than ask and be told ‘no’”. Yeah, you usually have to scrap most of your initial code, but you learn a lot this way, and it’s awesome to first cobble together some ugly cludge, and then see someone else’s elegant, and blindingly obvious solution later. THAT helps me a lot.
Why my compiler is continuously saying that * cant be applied to vector3 or boolean.
In the tutorial, it is exactly the same, but he has no error.
motion *= (Mathf.Abs(input.x) == 1 && Mathf.Abs(input.z) == 1) ? .7 : 1;
Ugh. I recommend never to use ternary operators like ?, as they make the code unreadable and prone to errors.
Especially so if you have a lot of other operators in you comparison and are using implied order of execution. You should at the minimum use more paranthesis to make sure that the compiler isn’t screwing up on the == operator versus &&.
It’s entirely possile that the compiler reads above as
Abs(x) == (1 && Abs(z))
So maybe this is better, but I don’t trust ternaries:
motion *= (((Mathf.Abs(input.x) == 1) && (Mathf.Abs(input.z) == 1)) ? .7f : 1f;
Oh, and add the ‘f’ to floats
Also, if you don’t mind: you are comparing floats to numbers, like in
if (Mathf.Abs(x) == 1f) {... do something ...}
That’s usually a bad idea, because exact match with floats is rare. Use ints for that, or check if the absolute of the difference of the two numbers you are comparing is smaller than a threshold like 0.001f