#Shooting Script
1 messages Β· Page 1 of 1 (latest)
@robust timber could you put your code here?
I want to continue this in a thread, because this will take a bit more
Also, a rather important question, do you actually plan on learning code, or is this just a one-off and you will mainly stick to art
Hey, thanks for taking the time!
Here is the original code: https://gdl.space/ijubuceqan.cs
I am an artist by heart and I like designing art for games and for quite sometime now I wanted to start implementing my own ideas into games
I know it's a rocky patth to take
You're the sole dev on the game you're making, correct?
And it will take way long than expected
Yes, yes it will lol
The only thing that concerns me is the time...I'm lacking it π¦
That is indeed problematic
Coding takes a long time
There's a reason why usually there are separate programmers/artists ;P
I mean not by it self, it'd just take a lot longer
That's why I didn't even try to make the code myself but I tried to change it and to make it work
Only problem with that is you won't really learn to code
Which you do want to do that from this
I started making games with Playground some time ago and I learned a lot about the Unity interface and overall, but not much about coding.
Righty
Did you do other languages before?
(Not sure what Playground is but maybe smth like scratch or gamemaker?)
Nope, no other languages atall. Just design and animation.
Righty
Yes, playground is a app for Unity that allows you to make games with drag and drop
the code I'm trying to change is from there
So GameMaker pretty much
There's visual coding for Unity I think
Bolt? I believe
Yes
I checked it out, but it seemed very complicated unlike gamemaker of Playground
It felt like I needed to know how to code to use it...weird...π€¨
Well, you need to know the principles of code, yes
Same goes for Blueprints in Unreal Engine
Bolt I am speaking about in the sentence above
Unreal seemed fun also. But Unity seemed more universal to me so I stayed with it
And I mainly wanna do 2D π
GoDot is good for 2D I heard as well
GoDot can only do 2D
Anyway, I think we should get back to the problem :P
I watched some videos about it, yes. never touched it tho... π
So for your problem, there are two options, option A is you learn inheritance, option B is you do it much more simply/ugly
I've never used it either, but I know it's existance
I will go with the one you suggest
One is more work than the other and harder to explain, and I'll kinda require you to do your own research on that front too
Because it's kind of hard to explain inheritance all by myself
So your choice
(also the consideration if it fits with your problem properly as well or not)
I guess there's even another option but then you need another script that controls the behaviour
And then you're doing more '"Object Oriented Programming"
There's a lot of things you can do differently with code heh
I planned to start the coding classes offered by Unity learn but I never got the time. I really hope that I will have the time to do at least this one.
I'd highly suggest doing that yes
I've never done them myself, but from what I've seen they're good
Yes, after doing those I may have the foundation to even speak with a person like you hehe. I don't even know the basic terminology to answer basic questions regarding what I wanna do as you can see π
Also, from whom did you get the code?
Yeah, I get your time problem though, I have that as well
There's always a lack of time :p
From the Playground app. It's free and you can install it easily in every unity project
I teach at several school in different timezones and it's madness
Oh you're teacher?
2d animation, yes
Cool!
There are some interesting coding choices in there kek
it would have been cooler if I was able to code at least a bit hahaha I admit! Coding is one of the things that I regret not picking up when I had the time and chance
Well, you saved yourself a lot of headaches at least ;P
Which no doubt you'll be getting now since you want to learn it now heh
Anyway I think for now, we'll just go the 'easy' route with the code
Definitely! I admit my mistakes π
That sounds good. Lots of people tend to give up when things get hard. I'm not one of them but it's a lesson I learned from experience π
Honestly I just really don't want to explain OOP or Inheritence that much rn heh
You'll have to explore that for yourself ;)
What do you suggest I can do now?
I think, you want to split up the Update, you want some extra functions so you don't have repeating code
Also, work in one script, the original script preferably, remove the copied one
So, this is your Update rn:
void Update ()
{
if(Input.GetKey(keyToPress)
&& Time.time >= timeOfLastSpawn + creationRate)
{
Vector2 actualBulletDirection = (relativeToRotation) ? (Vector2)(Quaternion.Euler(0, 0, transform.eulerAngles.z) * shootDirection) : shootDirection;
GameObject newObject = Instantiate<GameObject>(prefabToSpawn);
newObject.transform.position = this.transform.position;
newObject.transform.eulerAngles = new Vector3(0f, 0f, Utils.Angle(actualBulletDirection));
newObject.tag = "Bullet";
// push the created objects, but only if they have a Rigidbody2D
Rigidbody2D rigidbody2D = newObject.GetComponent<Rigidbody2D>();
if(rigidbody2D != null)
{
rigidbody2D.AddForce(actualBulletDirection * shootSpeed, ForceMode2D.Impulse);
}
// add a Bullet component if the prefab doesn't already have one, and assign the player ID
BulletAttribute b = newObject.GetComponent<BulletAttribute>();
if(b == null)
{
b = newObject.AddComponent<BulletAttribute>();
}
b.playerId = playerNumber;
timeOfLastSpawn = Time.time;
}
}
You should have it as:
private void ShootBullet()
{
if (Time.time >= timeOfLastSpawn + creationRate)
{
Vector2 actualBulletDirection = (relativeToRotation) ? (Vector2)(Quaternion.Euler(0, 0, transform.eulerAngles.z) * shootDirection) : shootDirection;
GameObject newObject = Instantiate<GameObject>(prefabToSpawn);
newObject.transform.position = this.transform.position;
newObject.transform.eulerAngles = new Vector3(0f, 0f, Utils.Angle(actualBulletDirection));
newObject.tag = "Bullet";
// push the created objects, but only if they have a Rigidbody2D
Rigidbody2D rigidbody2D = newObject.GetComponent<Rigidbody2D>();
if (rigidbody2D != null)
{
rigidbody2D.AddForce(actualBulletDirection * shootSpeed, ForceMode2D.Impulse);
}
// add a Bullet component if the prefab doesn't already have one, and assign the player ID
BulletAttribute b = newObject.GetComponent<BulletAttribute>();
if (b == null)
{
b = newObject.AddComponent<BulletAttribute>();
}
b.playerId = playerNumber;
timeOfLastSpawn = Time.time;
}
}
void Update()
{
if (Input.GetKey(keyToPress))
ShootBullet();
}
Ok, I replaced you'r code
This? Because that is the same is your current update
Also no worries, take your time
Also, a bit of a disclaimer, code which I provide is by no means perfect, there simply are many ways to do one thing in code
So I'll try to my best of abilities to provide decent code, but I can't promise perfect ;P
(Also since we're taking the 'quick' route it'll be less 'neat')
And keep in mind I can't actually test your things, so if something provides errors, you'll have to communicate it back
OK, I'm confused already. Do I insert this one also? π
Par example, I don't have the variables, so I've a lot of errors ;P
I'm grateful you're taking the time at all.
That replaces your old code
No worries if it works or not.
This was simply stating that this is your old update
At least I hope it still is kek
Otherwise things are gonna get confusing if your code is suddenly different heh
Also, I'm assuming your current code DOES work, right?
Does that go above the void update from before?
(If not we should fix that heh)
What do you mean?
yes, my current code does work perfectly.
Okay, that's a good start π
Thaaaaaats unreadable
Even still it's hard read π
sorry π¦
No worries
I don't know how to post bits of code like you do
Maybe just post it like this
Oh
Uhm, It's in the #854851968446365696 or just follow this GIF:
public static void Main()
{
Console.Writeline("Hello, World");
}
that took me like 3 minutes to make π
I appriciated the complete copy though XD
Ok, let's go π
(Also notably normally you'd put it in a pastesite with the length of your code, but this is a thread so it's fine)
it tells me my text is too big π
That's still the exact same though?
I don't see this
nice
Please copy your current code so I don't get confused ;P
Ngl it's pretty funny learning a teacher as a student heh
I know different subjects, but still
Well it's different
Just not exactly what I said
I'm always open to learning new skills. it's just I'm a total noob and it all seems greek to me...so I easily get lots
Also, to reiterate, this will be your Update:
void Update()
{
if (Input.GetKey(keyToPress))
ShootBullet();
}
That's the same
What I'm trying to do, and tell you, is that you split the whole update (which currently manages shooting the bullet) into a seperate function, so that you can call it multiple times in different if statements, so that you don't have repeating code
Ah
Yeah don't do that XD
OK started fresh cause I got confused and I replaced the old void update
Does it work?
heh
I haven't tested it yet. Let me see
shall I keep the old script too or just replace it with the new one?
Do you use git?
cause Im working on top of the old one
nope, never did, but I could I guess. It should not be rocket science π
It's reletively easy use
And if you use it, that means you can always go back to your old script
Like these are all seperate versions
If I want I can go back to whichever version I want
I see what you mean. Like backup
You can also see what changed
the's smart
It's a bit different than that, but essentially, yes
The big difference that each version isn't a COMPLETE copy of the entire game, it's just the difference
So that the repository doesn't get fukin gigantic
However, it'll still act and feel like the complete game
It's just saved differently, it's hard to understand, but it just works okay XD
There are some really smart people behind whoever made the system
I will make a fast prefab and test ti out
I tested it and it didn't work. No new prefab was created
Hm
I find it hard to say if that's the code (it probably isn't unless something was incorrectly copied) or if it's your own doing of using the wrong script/prefab or other shenangins
Do you get errors?
Nope, nothing
Keep in mind you do have to press a button
I don't know which button though, you assigned that
oh, I thought it will go automatically, sorry. let me test it with a button π
Heh
oh yes, that worked
Right hah
it acts like the old one
Yup it should
cool π
It's nothing more than just putting the code in a function
Do you get what you did or not?
That would be great
private void PrintSomeText()
{
print("Hey! I printed some text");
}
private void Start()
{
PrintSomeText();
print("Hey! I printed some text");
}
Here
These two things do the exact same thing, but one is done in a method, and that method is called within the Start, and the other is without method
The thing here is, say I wanted to print it twice
Instead of having copy the entire statement of Hey!...
I can just do:
private void PrintSomeText()
{
print("Hey! I printed some text");
}
private void Start()
{
PrintSomeText();
PrintSomeText();
}
If you want you can test it for yourself
The main advantage of using methods in this case, is that you only have to change the text once, instead of twice
Like, if I want to change what the PrintSomeText says
I do
private void PrintSomeText()
{
print("Hey! Some other text is printed");
}
And that'd change the output
Instead of having to do
private void Start()
{
print("Hey! Some other text is printed");
print("Hey! Some other text is printed");
}
Do you understand it so far?
Yes, I think I do. How will that be helpful in my case? I'm just trying to connect the bits
It seems to make things easier
Well, lets give a bit of bigger example
private void PrintSomeText()
{
print("Hey! I printed some text");
print("Hey! I print text");
print("Hey! printed text I have");
}
private void Start()
{
PrintSomeText();
PrintSomeText();
}
Here you can see how this already saves a whole bunch of lines if I want to print this twice
Cuz if i weren't to use this method, it'd be this:
private void Start()
{
print("Hey! I printed some text");
print("Hey! I print text");
print("Hey! printed text I have");
print("Hey! I printed some text");
print("Hey! I print text");
print("Hey! printed text I have");
}
I assume you get now, why using method is better :P
So now, for your case, you obviously want to execute the same code for shooting and automatic shooting, however you've change when it shoots automatically or not
Yes I get it now
[SerializeField] bool myBool;
private void PrintSomeText()
{
print("Hey! I printed some text");
print("Hey! I print text");
print("Hey! printed text I have");
}
private void Start()
{
if(myBool)
{
PrintSomeText();
print("printed statement 1!")
}
else
{
PrintSomeText();
print("printed statement 2!")
}
}
So here, it only will do the top one
Else the bottom one
(Which will give the same obviously)
Now if you apply that, with your other bool of input, you'll have one automatic one and one non-automatic one dependant on if myBool is true or not
Get it?
Here, updated it
Now there's a difference depending on the myBool value
You can test it if you want
You can even check the bool on or off within the inspector
OK, I don't think I understand everything but let's see
So you mean that it should be possible to have both functions from a single script?
What do you mean exactly?
that like I was reffering to
Ah, yes
(Again we're simplifying here, normally you'd probably use inheritance or OOP for this)
well if that's the case that would be genius! I have no clue why they didn't implement such a function in the first place.
no clue what you are talking about here and it's probably better not to explain! π
haha, don't worry about it for now ;)
Ok, so how to I test it and put it in practice?
Uhm lemme grab your script
[SerializeField] private bool shootAutomatically;
void Update()
{
if (!shootAutomatically)
if (Input.GetKey(keyToPress))
ShootBullet();
else ShootBullet();
}
Something like that
Notably, the ! stand for not
So, if NOT shootAutmatically, get input => shoot bullet
And if it's true, ShootBullet();
(Also your bool would be on top of course where you make the rest of your variables)
(Believe, me, don't put your variables above functions, it gets messy and confusing very quickly)
(Definitely haven't been a victim of it because I felt like it was better at the start)
(Also, do you like brackets? I seem to overuse them currently ;P)
OK, so I think I see where things are heading.
:)
I like Brakeys, youtube channel, so I guess I don't mind yours π
OK so lemme input this one
like so?
it does not seem right i think
π
π
The Update was fine on the position it was :)
From what I can see though, the rest is fine
Wait no
I'm replacing the old update with this one, right?
You've two update again
kek
Yes
You can only ever have one Update
Otherwise Unity will try to kill you :p
and it has, multiple times π
I like how you moved the variable with the update
You really want to stick those two together, don't you? XD
You see all of these variables?
Yeah keep this one there too
Believe me, I'm not doing it purpusely π«
Keep your variables up top, together ;P
I know, but it's funny heh
I told you not to to put it along with the void and you did it twice XD
You just really want the Update and the Boolean to be friends, which is nice of you you
But sadly, the Boolean and Update can't be friends, the Boolean can only be friends with the rest of the variables :P
lol
It looks good I think, but I do also think you still have errors
I think your brackets are bit off
completely possib;e
I think this is a stray bracket
Does anything give errors?
OH
YOUR BLOODY IDE ISN'T CONFIGURED
That's uhm
Very important
And probably also part of the reason why you're struggling with duplicate updates and such
Please refer to the #854851968446365696 for IDE configuration
Try typing random gibberish and see if VS now gets angry at you
Or par example two updates
Maybe I will have to update the VS some day also.
2019 is fine
(It's the one that still comes with Unity Hub)
I just like VS 2022 because I'm lazy
It has better intellisense
Par example
(It adds it in if I press tab)
It's auto complete for code
Doesn't always work well
But when it does it's nice
yeah
It should give errors
Like this should give a fuck ton of errors
nope no errors found yet
Then it's not properly configured
I'd recommend restarting Unity and VS as well
If you haven't already
Nope, I haven't but I should have. π
restrting
both
ok now i Have it
restarting fixes everything, ofcourse
I should have known that from the start π
Hahaha
awh
you can't even send gifs in threads
anyway, yeah restarting stuff helps a lot usually :p
Now I have one error about that bracket
This'll make both the helpers life, as well as yours better :P
but now it doesn't really matter what bracket I put the error stays
There's a reason why the #854851968446365696 states to not help people who's IDE isn't configured
I'd have told to configure it earlier, I just didn't know it wasn't configured π
What do you mean?
I tried to replace the braket with a stray braket and I still get the error
when I remove it I have no erroers
Yeah that's why it's a "stray bracket"
It's not supposed to be there
So if you remove it, then the error is resolved
oh, I get it. I thought I have to replace it...duh
Yes, you did have to replace, but only with a backspace ;)
Make sure you've the boolean on the correct state in the inspector!
For what you want to test
In theory you should be able to switch the modes during runtime
With the inspector tickbox of that boolean
But?
haha
then I tick sooth automatically it stops and it only shoots when it's unchecked
it makes the opposite
Share your current code again?
That's indeed how it's supposed to work
If it's unchecked, it just keeps shooting, no?
I guess you have to check if you want the option to trigger? It's a bit moe intuitive I think.
What?
Could you answer this?
Yes, it keeps shooting when it's unchecked
The script will be abe to be used as a manual shooter too, right?
with a press of a button
So if the automatic shooting is on the key is disabled and if it is off the manual key should be enabled?
Okay wait
or is it? π€¨
Now I'm confused
If the box ISN'T ticked
Then it shouldn't shoot automatically
Right?
yes, that's correct
Okay so when it IS ticked
You can use the button
To shoot
Correct?
Man I'm confusing myself
Okay wait, let me rephrase
If you ticked the box, shootAutomatically, it shoots automitcallly
Right?
And if it isn't, it does not
Okay, so that makes sense to me, as well as you, right?
but now it does the opposite. It shoots when the box is not ticked
I know it's just a minor thing, but it is not intuative
Can't you just make like
A quick recording
So we're not both ultra confused?
That'd be great XD
In the meanwhile I'll have a quick toilet break, so brb
What
I'm so confused
Could you make a screenshot of update?
Oh boi my boss is gonna bring me some fried chicken
Nice
Also I know that's completely off-topic but I had to mention it XD
Food can bring all sorts of emotions to a person... especially if one is hungry! π
I have to leave in about 15 to 20 minutes tho. I have to puck up my daughter from kindergarten and we will pend some time in the park after.
Wait lemme make a commit before I fuck my work lol
I'll try my best to hurry π
:)
If we(you) can't make it today it can be done any other day too, if you have the time and energy ofcourse π
It is kinda working and I'm really excited, to be honest.
I had no expectations what will happen but I decided to give it a shot because, you can never know π
OOOOOOOOOOOOH
MY
FUCKING
GOD
I think I figured it out
By god I am stupid
That else is attached to the wrong if
That's what I get for not using body's ;-;
Try this:
void Update()
{
if (!shootAutomatically)
{
if (Input.GetKey(keyToPress))
ShootBullet();
}
else ShootBullet();
}
(Replace your current Update with that)
If this doesn't work then my brain really died
OMG, it works flawlessly!!! π
God I really need to consume more coffee I feel like XD
My god I am so stupid
God fuck me
XD
Do you understand why it works now though
I would have never been able to figure it out
And why it didn't work now
it's like learning how to read a brand new language
I'm sure you would, you'd just have to practice more :p
Yup
Please answer though if not, I can clarify
I need to go in a bit
And I want you to understand
I think the answer to that question is because the braket. it needed to separate the code somehow to make the difference
I hav no idea how exactly but that's how my basic coding brain can explain it to myself
I wanna understand it myself too
The grouping is different
You can also see it with the indents
The 2nd is on the same line as the 2nd if
And the 1st is on the 1st line of the if
So what it was doing originally, was asking if the variable was false, if so do the if, check the key else shoot
It's a bit hard to explain, but you can mess around with it to see why it matters SO much
It was just an oversight in my part heh
Even rather experienced people make rookie mistakes ;P
Anyway I gtg, I've to eat some fried chicken ;D
If you've more question I'll from you :p
So gtg for now, cya!
The thing is that it actually got even better than I imagined. I never thought both functions can be stored in a single script. That makes life so much easier. Great thinking!
Is it OK if I add you as a friend before you go?
Thanks for the assistance. It was very helpful and much appreciated! I owe you a beer or two π
And we're right on time π
But the fun part is that you shouldn't ;D (in this case)
But that's a bit more complex
Maybe I'll provide some examples when I'm home, no promises though, I've a lot to do and I need my own bit of free time as well ;P
(Gonna leave soon from work, but it's 2hr travel time to my house so it'll take a while before I'm home)
Also thanks for all the GDL pastes kek