#Game Help
1 messages Ā· Page 1 of 1 (latest)
So dlich you said don't let animation control logic
but how can i have certain things only happen during different states?
if conditions
i know that
I personally use a separate script for animation handling
i have one for attacking
i have one for crouch attacking
when you walk, you run the walk animation
easiest to handle animations inside coroutine
if you want it to get tied to a function
The states should be handled in your movement logic. Animation states are for animation only. You shouldn't rely on them.
when you jump, jump animation
ok
so then create seperate states for stuff like attacking
separate from your actual animation
this is getting WAY too off topic from my actual issue
Actually, animation should be the last thing you want to add
it
was
whatever
i get it. i did a lot of things wrong
but can you blame me? it worked and I'm new
it works great
example:
else if (Input.GetButtonDown("Fire1"))
{
Animator.Play(aniamtion.name)
}
that's true
Write your logic as pseudo code or block diagram on paper first. Without considering animations at all. It would help us understand what kind of logic you want and then you can reference it when refactoring the code.
please relax, dude. We aren't blaming anyone, we'll try to help
i mean i have the attack code in a different script
Start with simple movement.
well when you tell me i gotta rewrite my entire game? yeah I'm kinda angry
Does your attack affect movement?
then have a state handler
like when you attack, you don't move? yes
then have bools for when you are in different state
like when attacking and is being attacked
We never said rewrite your game, there are just different scripts that could use some changes
I can't stress how much times I had to start a game from scratch, this isn't the same
if any of those bools failed to check, dont accept input
can these bools be used in a different script?
any it is easier to read, really
anywhere, as long as you can reference to it
because i know animator has bools too
I know I was late to the thread but did anybody debug any conditions?
but i guess that's wrong
if(attackscriptreference.IsAttacking)
Complete side note. I have a very hard time reading your code effectively - not judging just saying it is hard to read through so it is hard to help.
But, if you want to stop the player when they hit the ground after being hit, we should talk about that part a little bit more. Do you want them to stop cold and dead in their tracks?
set animation bools as you change your normal bools. Then turn it off as it goes off as well
but dont make it rely on each other
If so, track when they get hit, and as soon as they land, set the rigidbody velocity to 0f and the angular velocity to 0f. Ugly but quick solution for the moment.
if (Attack.isAttacking == true)
done make IsAttacking = IsPlayingAttackAnimation
but that's not right
you dont need to == bools
okay but even then, it's not working
exactly, plain and simple
yes
see?
track when the player gets hit midair, once it reaches the ground, make the rigidbody velocity to 0.
doesn't call the bool
not the script
If you really want to eliminate the root of the problem, you should refactor you movement logic.
As I said previously, start with defining your logic on paper.
As it is now, a quick fix would fix your current problem in the best case. In the worst case, it would create more bugs.
you need to get a field reference, then connect them
idk what that is
public Attack attack;
then drag the proper script in the inspector, or use GetComponent<>
@keen valve as dlich said before the thread, always make your animations depend on movement, not the other way around
yes.
And also, just to re-summarize your problem was that you kept moving when attacked midair? @keen valve
yes
when you land on the ground, you don't stop moving
but only if you're hit in midair
ok thx
so how do i stop it?
also lemme resend a sorta update code
for movement
You could add a book at the top
bool hitInAir = false;
Then in the Update
if(!hitInAir && !isGrounded && isHurt)
hitInAir = true;
if(isGrounded && hitInAir)
{
hitInAir= false;
rgbd.velocity = 0f;
rgbd.angularVelocity = 0f;
}
Changing those variables to the proper ones mind you
now i can't climb steps
(cries)
and to make matters worse, i'm back to just moving constantly when hit
great. more issues lol
guys?
I provided my two cents, I can't help more without rewriting it, and I have to move on with my evening
You can always ask from help, it doesn't have to be any specific person helping
ok
It will keep on getting worse, unless you address the root of the problem.
because now i can't climb steps
and i did
But the reason it is so fragile is because it does need a cleanup and rewrite.
A small way to get started is just to let visual studio fix the indentation for you
i'm doing what lyxerel said
No you didn't. At least not the way I meant.
how does it do that?
I suggested to step back from your code, write down your logic. Then move to refactoring(honestly, rewriting from 0 would be better)
i really don't want to rewrite it
please don't do this to me
i have so much else to do
You are embedding many ifs within ifs. Without proper indenting it is hard to tell what belongs to what.
Cleaning that up helps with readabiliry.
you say it does that automatically? how?
i can't rewrite my entire script.
there has to be another way
Lol, that would be the easiest way to solve all your issues. And any issues that might come in the future with the current setup.
But suit yourself.š¤·āāļø
you don't understand. i have to do this for a college assignment and i have so much other stuff to do here. i can't rewrite what took me weeks to do
Control A + Control K would normally auto indent
I'm suspecting you aren't using git though or source control. So it is probably hard to backtrack and try things
And you don't understand that you'll spend more time fixing poor code rather than rewriting it properly one time.
i don't think it's bad tho
it just needs some tuning
Clearly not
If you did follow a tutorial, then why are there issues?
can't I just, like, rewrite certain aspects of it?
You can
i feel so sick to my stomach
If your code was not spaghetti. At the moment everything is intertwined and related.
If you rewrite one thing something else would break.
and again, if i do rewrite it, what's to say it won't look the same?
Well, this time you could ask feedback from experienced people from time to time and they will direct you to writing a good code.
i did
I think it would take me all of 5 minutes to rewrite it.. but then you aren't getting the most out of your classes.
And walking through what I would change and why, is a laborious process. Especially over discord.
All you need to do, is what I shared up top. Track when you are hit in the air. And when you hit the ground zero out the velocities.
But with the code how it is, it may break other things, and that setting may get ignored and overwritten.
ok
i don't want to completely rewrite the code
is there a way i can, like, clean it up?
I'm not sure about it. At least the way it is now doesn't look like you were given any advices on good code structure. Or you didn't follow any.
i did. i asked here multiple times
Then you were asking the wrong questions. I bet all you got were quick fixes that only made your codebase worse.
dang
If you have any desire to make it right, I'm willing to walk you through the correct process of writing your code.
If not, then keep on looking for quick fixes.
ok
but how long will it take?
Depends. Shouldn't be more than a day or 2. Can be a few hours if you're quick.
okay.
can we do it tomorrow then?
when are you available?
@sterile socket actually idk
i'm kinda torn
i'm so torn here
because then wouldn't it just be, like, you writing the whole thing for me?
You don't need me available. Just follow the steps and report them here. Then I'll give you some feedback and we can move to the next step if there are no problems.
ok i'll write notes on my phone
Either would do.
how exact should they be?
like
when you move left/right, you move left/right?
here's the thing
i already have an input handler from my class
i'm using that
so I'm keeping that
It would be best if it's in the form of a block diagram or pseudo code.
alright. i'll see what i can do
but i really hope you'll be available when i'm ready
even though i REALLY don't want to do this
I'm at work, but I can have a look any time.
ok but, like, if we can get it done by maybe next week? if not, i'm just gonna have to keep using the old code
because i really can't afford to waste anymore time
If we're not done by Saturday/Sunday we can jump on a call, but I'm certain that wouldn't be necessary.
Ok. Again Iām just torn because
A. It seems unnecessary
B. I feel like it would just be you writing the whole thing for me and I feel bad
But if we can do it quickly and itās simple and itāll still work and I do most the coding then I think itāll work
A. It's very necessary. You might not realize it now, but your code is super complex for a very simple task making it very hard to debug and opening opportunities for new bugs.
B. I'm not gonna write anything
Only guide you to a correct implementation. You might as well learning something from that.
I just donāt want to waste a lot of time on something I already did
When I still gotta do stuff like design the level, add textures, cutscenes by mid April
Decided to refactor? That's nice
I'll be honest, him volunteering his time to help you move forward - that's a mentorship opportunity you won't get often in life.
if you are serious into game deving, practicing to make your code more readable is never waste of time, ever
Okay so for the movement I gotta
-Move left and right
Jump with no midair movement
-Crouch
-climb steps
Yes. Thank you guys
I am serious with game design
Im just 3 months into it, but if you compare my first prototypes, youll dump it in the garbage on first glance
So I hope I can make this work with your help
But if itās just the movement code and we do it quickly and I have your help, letās start tomorrow
Iāll write some of the pseudo code tomorrow, send it to @sterile socket or maybe I can tag you guys in this thread and get some input
If thatās alright?
Thanks guys. Sorry if Iām stressing a lot. This is a very important project.
Itās my senior capstone
oh goodluck there, anyway if it is still due to april, time is not much concern, you actually is fast
Ok. Due mid April I believe
Early-mid April
So if we can get the movement code reworked by, say, the end of next week I might still be on track
Is it okay if I DM any of you too?
@sterile socket or just post here in the thread?
Here is enough so we can all see your progress
Posting here would be the most efficient imho.
@keen valve Rewriting efficient code should not take more than 2 days
We can help if u want
Thanks. I didnāt sleep last night because Iām so stressed. Iāll write the pseudo code today and send you when Iām done
@sterile socket @flat nymph @orchid matrix @outer jewel i'm not really skilled at writing pseudocode but I hope this is a good start.
It covers basically everything that the original code covers but more simplified and hopefully explained better
oop i forgot to add some stuff for vertical movement. add private float vertical and set it to
_input.InputVector.y; in update
Hey! Just reminding I posted the pseudo code
Please calm down. You can't expect people to be available 24/7.
Ok. This is not exactly what I expected. It looks like you just took your script, went through it and rewrote it in other words. What I asked you to do is think of the simplest logic of your program and write it down(no looking at the script). You don't need any variables, method names or anything. Only the logic flow.
For example:
//Movement
If knocked back
Do nothing
If grounded
Process horizontal movement
Else
Process jump movement
Think of what you want to happen in your game and try writing down all your logic in a similar way.
Okay lemme rewrite it then
@sterile socket is this better?
hmmm.. put everything in branches. When writing a pseudocode, the most important is you know where you are in the code. You just listed all the capabilities of your character but currently there is no chain of what can happen and when it should happen.
Example?
STATE CHECK
STANDING
CAN ATTACK
CAN JUMP
CAN CROUCH
CROUCHING
CAN ATTACK
CAN JUMP
CAN STAND
JUMPING
CAN JUMP(DOUBLE)
CAN ATTACK```
A pseudocode can be as simple as this
then you keep extending the branches
Because I did put each one in different categories like (MOVEMENT) and (JUMPING) is that what you mean?
Yea, you never know what will happen, you just make an outline on how will you write the code
Ohhh let me try
@orchid matrix how's this?
probably not right? is there anything i should change?
remove all the Can't Lines, you dont need it and it unnecessary makes things complicated
oh so just the Cans?
ye
just make sure you write everything you "can" do at any time tho
anyways you can easily add them later
Now decide what are the extra conditions is, you have a IsHurt before right
what will it limit
what "cant" you do while hurt
you told me not to tho?
oh wait you mean, like, stuff you can't do for states like climbing, attacking, or getting hurt?
They are supposed to go here
if ishurt, then do something
if not then something else
so like this?
idk. i don't think i did it right?
It's still too complicated and a lot of unnecessary text.
This
(CROUCHING)
CAN JUMP
CAN ATTACK
if down arrow key pressed{
crouch
play crouch animation
you can't move while crouching
}
Can be rewritten as
If Crouching
Process crouching
No need to specify what you CAN or can not do here. This should be obvious from the conditions of the other state:
//When processing movement
If not crouching and not in the air
Process horizontal movement
Oh I was actually going to just do boolean handlers so he wont have hard time to set up the inputs. So in the end he can simply do like this
hmmm that seems too complicated. this is supposed to be simple, yes?
We're still talking about pseudo code. The actually implementation doesn't matter here. I just want to strip it all to bare bones logic.
Anyway, I guess I ll just watch hehe, I am actually doing my game as well so I might be unresponsive and cant help much anyways hehe
good luck! if you help out too as well when you can I'd really appreciate it!
anyways dlich, is the pseudocode better?
okay okay, ill just keep on standby, it wouldnt be nice he is reading books in different pages haha
This is better. But essentially, your movement logic is just this part:
(MOVEMENT)
if not attacking, climbing, crouching, hurt, or in air{
process movement
}
if climbing{
process going up steps
}
is that not right?
It is right. I'm just telling you, that the essence of your movement logic is just these few lines.
That being said, what about when the character is in the air? Does it not move at all? How will it get to the ground? Same about other states. Does the character not move at all there?
there's no horizontal midair control. think of old Castlevania. you just, you can't control yourself when in air. but if you jump while you're moving, you jump towards that direction
how do you get to the ground? use gravity. use the groundcheck i put in there
But no where in that quoted text there's anything about horizontal movement, is there?
Gravity is also part of the movement, no? Or does it teleport your character magically to the ground?
you fall to the ground
That sounds like movement to me.
oh. so you mean i didn't specify horizontal movement
That. But you should also include the effect of gravity in your logic. Since it affects your movement(it doesn't matter if you do it manually or let unity handle it).
Your logic needs to describe the character movement in it's entirety, regardless of where it originates from.
so like this?
Ok, let's leave it like that for now.
The horizontal input line is unnecessary as it's about specific implementation, which we don't want here, but I'll just pretend I don't see it.
Now, what about when the character is knocked down? Is it not supposed to be moved back or something?
yes
Shouldn't that be in the movement logic as well?
I made a separate one for knockback
but i should put that in movement as well?
i was thinking: you're basically just jumping just in the opposite direction
Let's clarify one thing. When we're talking about "movement" in a game, we refer to change in position over time. Regardless of what's causing it.
Movent is not only when you press the arrow buttons
should i do the same with jumping?
Yes
Yes, all of it
Does it change the position of your character?
i don't think so
Then, no
so like this?
also give me, like, a half hour. my laptop's battery is running low. but i'll be back in, like, a half hour
Alright.
Let's have a look at this part:
if not attacking, climbing, crouching, hurt, or in air{
process horizontal movement
horizontal movement from horizontal input * movespeed
vertical movement from gravity
}
Specifically, it's condition. It seems to be safe to say, that the only time we want horizontal movement processed is when we are not in any of the other states, right?
In this case wouldn't it make sense to express it like that?
If Crouching
...
If Knocked
...
//If other relevant states
...
Else
Process horizontal movement
Further more, all of the states have something in common. They calculate the movement of the character in that state and apply it to the character. In your script you were using velocity for that. Since in the end they're all doing the same thing(applying movement), we can take it out of the states and place it at the very end of the logic. This way we will be sure that our movement is always processed, regardless of the state. And judging on its value we would be able to make assumptions on what went wrong when we face a bug.
So to sum it up we can write it in a similar fashion:
//We start with no movement at all. Alternatively/additionally we could take into account the movement from the last frame
Movement = none
//Then we add up movement based on the current state of the character
If Crouching
Movement += Process crouching
If knocked
Movement += Process knocked
...
Else
Movement += Process input
//Lastly we apply the calculated movement
ApplyMovement(Movement)
Does that make sense?
maybe. so if i understand what you're saying, all things require movement. the difference is what movement i'm doing depending on the state. so apply the movement to all staes?
Exactly.
If you don't want to move at all, you just apply no movement.
Taking that into account rewrite the movement logic pseudo code.
Okay how is this?
@sterile socket you there?
hey bro, i'm gonna go to bed in, like, an hour. if this pseudocode looks fine, you want me to, like, get started setting up the code or do it tomorrow?
cause, like, if you're busy now, that's fine. we can always pick it up tomorrow. i just wanna make sure the pseudocode is fine and ready for getting started on the actual coding tomorrow. š
Why is the climbing in the else block here and not outside along with the other states? Is there any special reason for that?
Else{
if not climbing {
process horizontal movement
horizontal movement from horizontal input * movespeed
vertical movement from gravity
else if climbing {
process horizontal movement from horizontal input * movespeed
process vertical movement from vertical input
no gravity
}
}
Because itās similar to regular movement except itās for climbing steps
But if thatās not right I can move it
It might be similar from the perspective of gameplay, but logically, it's a totally different state that should be treated o. Equal terms as the other ones.
Okay lemme change it then
aight how's this then?
Ok, so looking at all this states of Movement, is it correct to say that only one of them is active at the same time?
(MOVEMENT)
Movement = none;
if Jumping{
if you are on the ground and not hurt {
movement += procress jumping
no midair movement. moves in direction when jumping while moving
}
if on ground{
movement += process landing
stop jump animation
}
(KNOCKBACK)
if is hurt{
movement += knockback
similar to jumping just in different direction
}
(DIRECTION CHANGE)
if right arrow key pressed {
face right
}
if left arrow key pressed {
face left
}
(CROUCHING)
if crouching{
movement += process crouch
}
if climbing {
process horizontal movement from horizontal input * movespeed
process vertical movement from vertical input
no gravity
}
Else{
process horizontal movement
horizontal movement from horizontal input * movespeed
vertical movement from gravity
}
Apply Movement
Well, I mean, you can attack when you move. But you stop moving when you attack. You can attack while in the air too
Think thatās what lyxerexyl was saying when he said make a flow
You're mixing up things again. We're talking about movement only now. It doesn't matter what else you can do in these states.
Oh
Can you rephrase your question then?
Youāre saying only on state is active at a time?
Yes.
So, like, when Iām attacking, Iām not doing anything else?
Can you both crouch and move normally horizontally for example?
Oh. No but you can jump when you crouch
Jump, knockback, crouching, climbing, normal movement. Can any of these be active at the same time?
No
So you can be crouching while in the air?
Yes weāll actually my jump animation is the same as my crouch animation
I told you before, forget about animations. We're only talking about movement.
How is it different from not crouching while in the air?
Then in what sense can you be crouching while in the air?
Fair point
Anyways, it seems like you can only be in one of these states at a time. That means that we can use a simple state machines to handle them.
Do you know how to use enums?
Ok
No
Well
Sorta
Iāve practiced with them but Iām a bit rusty and i havenāt done it in unity
Well, you'll need to refresh that as it's the simplest way to implement a state machine.
Enums and switch statements.
Alright
The general idea is like that:
switch(current state)
{
Case state0:
Movement = ProcessState0()
Case state1:
Movement = ProcessState1()
//Etc
}
This is pseudo code. You'll need to use correct syntax for switch statements.
Current state should be an enum that you made for all the states we were talking about until now.
You would go through all of your checks and assign the current state of the frame.
Then this switch statement would be calling the appropriate method depending on the current state, to process and calculate the movement. Finally you apply your movement in the end of update.
Makes sense?
Yeah Iām looking at guides for it now
So you want me to rewrite the pseudo code again for this?
Make it a switch statement using enum?
No. You can start writing it in actual code. Only implement the movement part for now.
Yes.
But see this is where Iām confused
If using an enum and switch statement, how will it activate stuff like if Iām pressing the jump button?
Lemme write it out so I can show you if I have the right idea?
@sterile socket so do you mean something like this?
Kinda. You'll need to fix the syntax errors.
Oh yeah
Again, you're confusing things. Input should be handled separately along with all other checks that determine the current state.
So if I press space, Iām putting it to the jump state?
Yes.
Same with stuff like climbing steps
If pressing up or down while touching steps, Iām in the climbing state
Yes. Obviously, it's not gonna be as simple as just checking the input. You'll probably need more conditions.
Oh. Such as what?
Such as the current state, grounded/hurt/current movement and any other checks necessary to decide whether you should change state and if yes, what state to.
Getting lost again lol
To put it simply:
- Check if we should change state, and change if necessary.
- Process the state.
Okay so, like,
If space is pressed
If is grounded
Change state to jump
If on the last frame you were moving normally, but this frame you got hit, then you know that you should change to knockback state.
Yes like that.
So
If hurt
Change to knockback state
If moving
If hurt
Go to knockback state
Should I put those in the pseudo code or just when we get to actual coding?
It's fine to put it in the actual code.
alright. i'm still a bit confused on it but we can work it out during the coding ig.
The pseudo code gets unwieldy anyway. There's no much point in it anymore. Maybe block diagrams would've been better...
ah. so the pseudocode isn't good then ig?
What exactly are you confused about?
It's not like it's not good. It just doesn't serve the purpose that I intended it to have. But nevermind that. It was useful to a degree.
do you want me to rewrite it or just leave as is?
Leave it as it is. You can still reference it when coding.
i guess the main thing i'm confused about it stuff like jumping or knockback when on the ground, if you are on the ground when doing that state, it would automatically, like, idk how to word it right.
but, like, i guess it's more an animation issue
like if you jump, you start off from the ground. so if i call the jump state, you will be on the ground when you jump
The animation state machine is gonna be implemented in the animator. It should work independent of this movement state machine.
yeah ig
but i also followed a youtube tutorial on it as well
and stuff like the walk animation is determined by your movement
and jump/midair animation is based on if you're in the air
One frame you're in the normal movement state, then if jump is pressed and we can jump, you change to jump state. Then at some point you change to airborne/falling/whatever state.
gotcha
Yes. It is dependent on the movement. Why do you think that it doesn't fit what were doing now?
like, i get what you're saying. it makes sense. i guess i'm just having difficulty imagining how it's gonna be written. but again, i guess we'll have to see when we start with coding
and pseudocode can help with it too
anyways @sterile socket it's getting late here and i gotta go to bed. but do you want to continue this same time tomorrow? i'll let you know when I'm ready and when you're ready we can get started. thanks fo much for your help!
It's more convenient for me in 6-7 hours from now. That's when I have some free time.
i'll be in bed that time lol
But I'll reply whenever I have time either way.
okay. i'm thinking maybe same time as today. like around six or sevenish for me. what timezone are you?
*p.m.
*rn it's 11:49 at night for me
It 2pm here. I'm at work around this time.
oh. well again, i'll message you probably same time as today and if you're ready, let me know. thanks.
i'll also message some of the other people in this thread too
Heyo
what did i miss
Judging by previous messages the drastic time zones are a problem š
Anyways @keen valve how's your pseudocode going?
Itās alright. Dlich suggested we use a state machine for different states like jumping, crouching and knockback.
Gonna hopefully see if I can get his help later today
Just in case, I'm available now for an hour or so before I go to sleep, so if you have any questions, now's a good time.
Oh Iām sorry I didnāt see this. But Iāll try again later today after Iām done school
@sterile socket @flat nymph @orchid matrix @outer jewel Yo! Anyone up for helpin with the movement code?
Last time, dlich suggested using a state machine using enum and switch statements
My main issue is going to be how itās going to work. As well as stuff like actually moving the player and jumping
I am in class today, might only just visit from time. Can help you timely though, where is the latest pseudocode now?
well the finished one i posted last night
this
your night? š
yeah lemme show you what i have so far to see if i'm on the right track
What is these 2```cs
public Hurt hurt;
public StepsCheck stepscheck;
those are referencing two different scripts
also put comments on variable that you assigned already but havent used yet```cs
private float MovementSpeed = 3;
private float horizontal;
private float vertical;
yes, ik but why does they have to be another script, dlich asked you to?
nvm, it may be more organized if you do that
no but you don't want one script to be cluttered with everything
they're scripts for certain things i made before we started doing this
yeah
so if i wanna check to see if i got hurt, call the hurt state from the hurt script
same with the attack state
so now my main issue i guess is what's going to go where, and what i'm going to use for movement? physics? transform?
do i use what i used before? or was that too complex?
- Your pseudo dont have logic that accept input, not sure what dlich will teach you about it too so I cant help there. 2) Choosing physics or transform is your discretion, physics are better though unless you want to have more control on how things move
transform movements are more flexible because you do all the logic
okay then i should use physics
well it's simple movement i guess so physics might be the better way to go
oh and i think dlich said that we can worry about that with the actual coding but maybe i'm remembering it wrong?
wait were calling attendance hehe
who your class?
"oh and i think dlich said that we can worry" what do you mean?
i meant, like, he said we can focus more on the logic when we get to designing the code
but again, i might be remembering it wrong
did dlich already give a go to start coding?
yeah that's what i'm doing now
nice, okay
i showed you what i have so far with coding
i'm waiting on dlich too to get his input as well
first make a method that will do the stuffs your character will do
ie. jump(), crouch()
well we're doing enum and states
switch statements
he also said to take into account other logic for states like if you're on the ground
if Jumping{
if you are on the ground and not hurt {
movement += procress jumping
no midair movement. moves in direction when jumping while moving
}
if on ground{
movement += process landing
stop jump animation
}
```you can start by making this into a sinple method
well dlich said the simpler way would be to do a state machine
since you're really only doing one at a time
void jump()
{
if(all conditions that should be true) // if any is false, you cant jump
{
// what happens when you jump
}
}```
honestly, i'm fine with either method.
as long as it works and it doesn't look too cluttered
that wont conflict, with his idea
im following a state machines as well
anyways, I think im bad as teacher, im just directly telling what to do
we should just wait dlich, he is about to go online around this time anyways
hmmm well i already have the enum set up
yes. if you need to go back to class that's fine but if you want to stick around, i'd appreciate it. i have to go take a shower now. be right back
in, like, a half hour
void UnitHour()
{
switch(currentstate)
{
case State.Idle:
if(target != null)
{
attack();
move(target.transform.position, -1*speed);
currentstate = State.Attacking;
}
else if(moveposition != (Vector2)transform.position)
{
currentstate = State.Moving;
}
else if(isReturning || gunBroke)
{
stateChange("Returning");
}
else
{
moveposition = restoreposition;
}
break;
case State.Moving:
if(target != null)
{
attack();
}
move(moveposition, speed);
if(moveposition == (Vector2)transform.position)
{
if(target != null)
{
strafe();
currentstate = State.Attacking;
}
else
{
currentstate = State.Idle;
}
}
Drawline(moveposition);
break;``` this makes things organized by making the methods that `do` stuffs separate. This wont hurt a statemachine
that is a script from what im currently working on, this is not accepting input, but you can simply add them inside the methods()
You might need to add additional states as we go. But add the normal movement(horizontal movement) one now.
We want to put the switch statement in its own method. Let's call it UpdateState.
Then we'll also need another method for changing between states. That can also be in a separate method and it will also need the same switch statement as what state we can change too would probably depends on the current state.
@sterile socket @orchid matrix i'm back
so you want something like this? https://gdl.space/osidezosan.cs
is the normal movement it's own state? or would that be, like, default?
It's better to give it its own state.
ok
Why is ChangeState() nested in UpdateState though?
it is?
my bad lemme change that
okay so, then, shall I add the groundcheck next?
that would be a collission check, yes?
Add where?
I was thinking a OnCollissionEnter2D
Hmmm... I think it's better to do a raycast every frame.
oh. like i was doing before?
I don't remember what you were doing before, but I guess so.
private bool isGrounded()
{
float ydirection = transform.position.y - groundCheck.position.y;
if (ydirection >= 0)
{
return Physics2D.OverlapCircle(groundCheck.position, 1.01f, Ground);
}
else
{
return false;
}
}
oh man, and we just learned about this in class too lol
lemme check that code
waiting for that to load
hey so would it be something like this?
Why transform.forward?
idk that's what he said. what should i change it as?
i guess maybe if you land on your feet, the bottom of your character? idk.
Who said?
Explain what your raycast line does in simple words.
my professor. he didn't really explain it well
Is there any situation where you don't land on your feet?
but according to this guide i'm reading, the parameters are the origin, the direction, and max distance
That's correct. So in your shared code, what is the direction?
What is transform.forward? Where does it point?
forward?
Okay. So then, how is that related to a ground check?
Yes.
how would i do that?
What's the relevant direction?
down
Yes, but is it available as it is as a property/field of Transform?
no
What do we have available that we can use?
Here's the Transform docs page:
https://docs.unity3d.com/ScriptReference/Transform.html
What axis is the down direction on?
up?
If that's the answer to the first question, then yes.
gimme a sec. my project crashed
but i assume the green axis?
And what do we call the green axis?
y
the y axis
Correct. That was a leading question to the first one though.
so... the direction parameter would be transform.up?
So you use transform.up to get the down direction.
ok
You don't just use it as it is.
dammit
Down is obviously opposite to up
-transform.up?
Yep
okay so how's this?
That would do. Maybe tag checking is not the best way but you can deal with it later. Also, if you're checking tags,it's preferable to use CompareTag method.
let's deal with it now just to get it out of the way
then let's do checking if colliding with steps
which would that be a raycast too?
No, that would be better done with OnTriggerEnter in my opinion
ok
what about oncollisionenter
That would imply that your ladders have colliders and don't let the character walk through them.
oh yeah
so ontriggerenter would work
Yes. You can deal with all of that later. Focus on getting the basics movement working first.
alright
let
s start with moving on the ground
which is here, right?
is that in updatestate or changestate?
Right. Create a method specific to that state update and call it from here. Then put your movent logic in that method.
Start with update.
ok
?
Put it in its own method
Yes. Do you remember how we said that the states should only calculate the movement and not apply it?
yeah
okay so... should I test to see if it works or is there more I should add for it?
just to see if basic movement works
But you are applying it right here. Instead, you should return a Vector2. And apply it to velocity after the state machine update.
And yes.make sure the horizontal state is always active and test it.
i'm confused
About what?
about what you said. could you make it a bit more clear?
Which part exactly?
so return a vector in the movement method?
Yes
or in the state machine?
Both
How do we make a method return something?
return
That's only one part of it.
oh
What's the void at the method declaration for?
it doesn't return a value
To be more precise, it's the return type of the method. Void means that it's none.
What type do we want to return?
idk
What are we returning?
a vector2
And Vector2 is what?
vector
a float?
No, but ok. What's a float then?
When you declare a variable like that: float a. What is float in this case? Why do we need it?
What this Something in Something a when we declare a variable?
What does it tell about the variable a?
We're not. You're missing a very key important point here. It's so important that I'm surprised you were able to code so far...
i can. i'm just blanking out rn.
Ok. Google on how to declare a variable in C# and what each part of it means.
type is the type of variable
name is the name of the variable
the value of the variable
type name = value
just done class, what's happening?
dlich is teaching me what a vector is
yea I can see haha
Good, so going back to float a what is float?
so we can figure out how to return a vector
a floating point number, usually with decimals
Refer to what you just learned from googling.
float is the type
yes
Then what is Vector2?
type
Correct. Now, going back to where we started, what type do we want to return?
vector2
yeah but
So how do we declare the method to return a Vector2?
that's what i'm trying to figure out
Let's go back here:
#1070195919154323558 message
What void means again?
none
None what?
no return type
Yep. So what do we change it to to have a return type?
vector2
Correct.
son of a-
Well, there's still an issue with how you're creating a vector
add new
Yes.
and you're saying i gotta put that in the state machine too?
And while you're at it, remove the line above that sets the velocity.
Put what?
the return
so how's this?
and how will we change the velocity then?
oop
You need to change the return type of the UpdateState method and return whatever you're returning from the state update.
that... needs to be worded better sorry
so, what? UpdateState is a vector2 too?
or make it something else?
It's return type should be that, yes.
no but, like, that makes no sense
Well, what does the error say?
Why does it not make sense?
not all code paths return a value
everything lol
If your method has a return type it absolutely has to return a variable of the specified type.
Be more specific. It can't be everything.
okay so... you're saying all of the states have to return a vector2?
Yes. But for now you can return a zero Vector2 at the very bottom of the method.
okay so, like?
No.
this
So we have the HorizontalMovement method, right? What did we just do to it?
have it return a vector
Correct. And how do we access what it returns?
Try that - assign what HorizontalMovement returns to a variable.
?
assign it to a vector2... then assign velocity to that vector2?
No
Ughhh... You really are missing the very basics... How do we assign a function result(it's return value) to something?
i'm sorry
It's fine. I wish you did learn C# basics before moving on to unity project though...
we call the method
I did. i'm just rusty
which is weird
because i was just learning about it too
Okay. So if we want to return something we return from a different method, what do we do?
i should know this
we are making no progress š¤£
Let's say we have 2 methods:
float A()
float B()
And you need the B value, but you can only access B in A. How do you get that value from somewhere you can access A?
yes
we use a parameter, right?
yes
Not really. But try to demonstrate it with code.
idk like say you have a method...
oh wait shit
no i'm thinking it wrong
so you have 2 methods A and B. you can only access B in A?
so floatA(B)?
idk maybe either i'm interpreting what you're saying wrong or you're not phrasing it right?
because i do NOT remember them teaching this to us
and this is years of college
Ok. Let's make it simpler. The facts is that you've already used it so many times.
When you do bool a = Input.GetKeyDown();
What's happening on that line?
you're setting it to when you press down
What happens from the perspective of the program? What is GetKeyDown?
a is true when you Input.GetKeyDown
a is true when you press down
that's not right, is it?
Why is a becoming true?
because apparently five years of college means jack lol
it's a bool
That doesn't answer the question
Really. I wonder what you've been learning so far...
C Sharp, Java, all that good stuff. But I guess I forgot most of it... even though i didn't. but i guess i did
maybe i just need to practice it more
but anyways this is so off topic do you remember what we're even doing?
trying to figure out what to put in the movement state
We're not going anywhere unless you can answer these simple questions
ok
They're not off topic. They're absolutely necessary topics for you to complete your project.
It's not a value.
but it's a = input.getkeydown
oh it needs a parameter
No. Unrelated.
fuck
Ok, let's go from the other side. What's a function/method in C#(or really any programming language)?
a named set of actions
Nnn... Not wrong, but it's not exactly the correct definition.
But okay, syntax wise, how do you tell if something is a function?
it has a paranthesis?
that's literally what my class notes said lol
Okay. That's better.
so getkeydown is the function
Then going back to GetKeyDown() what is it?
a function
Correct.
so you are calling the function
Okay, good. We call it and then what happens?
we set the key
No
we don't?
Before that
oh... huh?
What happens after we call it?
Bool is the type of what we assign. But where does the value come from?
What's probably happening on the other side?
it's looking for what to return?
returns a value?
Correct
so we have a variable that calls a function that returns a value
The variable doesn't call anything. Your code does.
No
i...
At least the wording is incorrect.
Let's pretend were the CPU that executes that code. What are the steps that we go through?
Assuming we just arrived on that line.
we assign the value
i mean variable
How do we know what to assign?
the type
What type? How do we know what value to assign?
say no
it's a bool
we call it a bool
What's bool?š
Okay, but what does it have to do with what we assign?
jack
How do we know that?
this is, like, going into DEEEEEP
No. It's very shallow
bool. its a type. returns true or false
that's what the professor said.
believed them all these years
apparently not true
Forget what the professor said. You're a CPU now. You execute the line. What steps are you going through?
Is that a step..? What are you trying to say by that?
it's a step. we're looking at what's the type?
Okay, let's assume so. What's next?
not right, huh?
Not really. But it doesn't matter even if it was.
at this point i'm running low on patience
Write down the steps that explain the execution of that line.
i forgot what you even asked
how does a CPU execute a line of code?
oh do you mean fetch, decode, execute?
we give it the code. it decodes the line. then stores the code and runs it
That specific line of code
a = GetKeyDown(someKey);
so we get the line of code
No. That's not what I'm asking. I'm asking about the program execution flow. Basically, what logically happens there.
i'm about to cry
Ok, programming is too hard, leta go to math.
You will, but it's necessary to understand it logically.
We have a formula x = a * y
What can we call the right side of the equation?
the solution
How do we solve it step by step?
No. We want to find x. Let's say we know y and a.
Okay, so we basically do what with the function?
we multiply it
No. In a general sense.
we solve it
Correct.
Solve or execute it if we thing from the programming perspective.
And the what do we do with the x?
We assign the result of executing the function to the left side, correct?
yes
Ok. Now going back to our code line. We have a variable that we want to assign the results of the function on the right, correct?
yes
The same thing as in math, right?
So how would we solve it?
We execute it, yes.
yes
And then
then we assign the result to a
Correct.
so a cpu:
-executes the function
-assigns the result to the variable
Yes.
How do we provide the result of the function from within the function?
so we have a function, who is assigned another function
Function is not assigned. Remember the steps that the CPU does? What assigned is the result of the function execution.
so the function is assigned the result of another function
Mind writing that in code? I'm trying to understand if you're misunderstanding or just using poor wording.
so functio a = result of function b?
i guess i'm misunderstanding
function a returns the result of function b?
Step through that line as if you're a CPU.
function a(){
return b()
}
function b(){
return answer
}
Okay, that's closer, but why are there parenthesis in the return line?
Yes
yes remove the paranthesis?
No. That was correct
oh okay
Ok, so this way you get the result of b from calling a.
yes
Now going back to our state machine, we want the result of the current state to be returned by the state update function, right?
yes
So how do we write it?
return horizontalmovement()?
Correct
then how do we set velocity?
At the bottom of the update function
in default?
No. After the switch
No. You'll need to save the state function result to a temporary variable and then assign it to velocity.
There's only one place you an save it. Where you call the function.
and this variable is a vector2 yes?
Yes
so like this?
No. What happens after return statement?
you return the function
Exactly. That means that any code after that is not executed.
put it before the return
No. There's a reason why we return the horizontal movement, right?
So that we get the result where?
We return from this state update function to the Update, right?
yes
And that's where we get the value from the specific state.
We can use that value then to assign to the velocity.
I guess it's a bit different from what we were trying to do at the beginning
yeah
But anyways, that would do.
i'm sorry. i'm still kinda lost
Lost about what?
If you refer to rb velocity, then yes.
Maybe not ideal and it might be good to refactor it later, but let's keep it as is for now and test.
well if it's not ideal, what should we do?
In fact, if you do it in update you can assign the return value directly to velocity.
It's fine as is for now.
okay. what exactly should we do?
Assign velocity
Wherever we get the return value.
No. Where the state machine is being called from.
And it's not correct to call it "state machine" it's just a method that updates the current state.
If it's there, then yes.
tbh, i don't think i even assigned it anywhere. my bad lol.
should assign it in update?
Assign what?
Yes.
and set rgbd.velocity to that right?
You want it to be updated every frame.
Yes
so this?
Why would you call it twice?
Yes
okay let me send you the code i have so far
okay so basic movement works.
but can we assign it to horizontal movement at start?
The formatting is terrible, but it's ok for now.
i just hit ctrl+a so it should look better now
or is there something else wrong?
is this better? https://gdl.space/ebowajamin.cs
i'm looking at it and i guess not
anyways, is there a way i can assign horizontal movement from the start?