#Game Help

1 messages · Page 2 of 1

sterile socket
#

Wdym?

#

Why assign it from start?

keen valve
keen valve
#

no no i mean

#

when you start the game, you're automatically set to horizontal state

#

because as of now, your first state is jump.

#

maybe if I rearranged the order?

sterile socket
keen valve
#

so what would i do for that?

sterile socket
#

Assign it.

keen valve
#

like i know it ain't this

#

or do i use changestate

#

which, we still haven't done anything in changestate

sterile socket
#

No it's not. What is the variable that we use for current state?

keen valve
#

state?

#

ohhh this?

#

yeah i forgot lol

#

anyways, wanna move on to jumping?

sterile socket
#

Try implementing it yourself in a similar fashion.

#

I need to do some work.

keen valve
#

ok. should we continue this tomorrow?

#

or my tomorrow anyways?

#

or... you know what i mean

sterile socket
#

I will be replying when I have time. Try implementing the logic for the rest of the states for now.

keen valve
#

ok

#

thank you

#

i'll show you what i have so far tomorrow when you're ready same time as today

flat nymph
#

@sterile socket I feel like using a raycast for the ground detection may not be the best idea

#

I personally use Physics2D.OverlapCircle()

#

Won't it fit more for this game?

sterile socket
#

It might be better, yes, but it's a minor refactor that they can do later, when the core features work.

flat nymph
#

Yeah but in a game with mechanics such as climbing it might be better for the ground detection to have a radius rather than a single line

keen valve
#

But, I mean, it works for now. And if it doesn’t work later, I can change it to that. But so far, I do have the climbing state working almost perfectly

flat nymph
#

Alright

keen valve
#

@sterile socket @flat nymph @orchid matrix Hello! I'm gonna have to start a little later tonight but, I mean, it shouldn't really affect much. I'll still be around usually the time you are. So anyways, here's what I got done last night. I basically added the rest of the stuff from the updatestate method dlich told me to add. It's not perfect by any means, but it's a nice start. I also listed the issues the code has so far in the progress report in the script at the top. https://gdl.space/cohaculose.cs

#

But anyways check the code when anyone is available, I’ll be back probably in a few hours right around the time you should be available to see. Thanks!

orchid matrix
#

Why is your state machine throwing the commands to do something. It should be mainly for catching inputs, then swtiching to another state

#

hmm.. Anyways, I havent really read what you were discussing yesterday with dlich, maybe we can just wait for him again to review your code.

#

Is that code working so far?

keen valve
keen valve
keen valve
#

@sterile socket @orchid matrix hey guys! I'm back! Do you wanna get started?

#

so yes, definitely gonna need to work on the gravity for stuff like jumping, knockback and a little bit for the climbing too because that isn't perfect either.

sterile socket
#

I'm a bit busy now. I will be able to have a look in 30min-hour

keen valve
keen valve
#

@sterile socket you ready?

sterile socket
# keen valve <@209684227720085505> <@1015230037697499147> <@456226577798135808> Hello! I'm go...
  1. We want to update the state at the very end of update after ChangeState has been called so that we update the correct state.
  2. All the state changes should be processed in ChangeState().
  3. Hurt or Attack are not great names for components. Which might suggest that what they do should be included in this script perhaps. Then, this script should be renamed as well. Maybe CharacterController2D or something.
  4. There are some namespaces that you don't need in the usings. One of them would prevent you from building the game.
  5. Since we move all the change state logic to ChangeState(), OnTriggerEnter2D shouldn't set the state as well. Instead make a separate bool overlappingWithLadder (or smth) and set it to true/false when entering/exiting the ladder trigger.
  6. Jump and knockback issues can be addressed after you implemented the change state logic. Basically, you need additional state for falling down.
keen valve
sterile socket
# keen valve for 3 and 4, Hurt and Attack are calling different scripts. I don't want this sc...

This script is not cluttered. There are cases when you can split the functionality into multiple scripts, but not when it's tightly coupled with the logic in this script. Ask yourself this question: will something break if that other component is not assigned? If yes, then it shouldn't be a separate script or the logic needs to be changed such that current component works even when the other one does not exist.
Anyways, regardless of what you do, Hurt is a very bad naming from the OOP perspective as it describes an action and not an object. At least rename it as HitDetector or something.

keen valve
#

alright. want me to rename the scripts then?

sterile socket
#

As for the namespaces, your ide should tell you what namespaces are redundant

sterile socket
keen valve
#

think maybe a float called "fallingSpeed" or something?

keen valve
#

Hello?

sterile socket
keen valve
#

oh yeah. i move all the state changes into changestate(), renamed the code, renamed the other scripts, added a bool for colliding with the steps, and moved the updatestate call in update like you asked. oh and got rid of the uneccesary break

#

oh and added a falling state, but don't know what to put for falling

sterile socket
#

i move all the state changes into changestate()
I don't see anything like that at all.
You still have the logic of state changing in your Update:

if (Input.GetKeyDown(KeyCode.Space))
        {
            if (isGrounded == true)
            {
                state = MovementState.Jump;
            }

        }
        if (hurt.isHurt == true)
        {
           
        }
        if (Input.GetKey(KeyCode.DownArrow))
        {
            animator.SetBool("isCrouching", true);
            state = MovementState.Crouch;

        }

And some other places:

if (hit.collider.gameObject.CompareTag("Ground"))
            {
               isGrounded = true;
            }

And ChangeState doesn't change the state at all. It sets the same state as it is already in which is totally pointless.

                case MovementState.Jump:
                     state = MovementState.Jump;
                     break;
                case MovementState.Crouch:
                     state = MovementState.Crouch;
                     break;
                case MovementState.Knockback:
                     state = MovementState.Knockback;
                     break;
                case MovementState.Climb:
                     state = MovementState.Climb;
                     break;
                case MovementState.Falling:
                     state = MovementState.Falling;
                     break;
keen valve
#

oh. I'm confused then

#

let's focus on one thing at a time i guess. let's start with the falling state, ig.

#

and isGrounded isn't a state. it's a bool.

orchid matrix
#

your changestate() actually just need one thing to change to be functional at least

#

or to add

keen valve
#

the state?

#

so wait, we want changestate to change the state, yes?

sterile socket
sterile socket
orchid matrix
#

anyways, I think you dont need a changestate method in your game, just change the state direfctly in code lines

keen valve
#

oh, that's because i forgot to get rid of those logic of changing states. i did

sterile socket
#

No, Change state would make things easier to read and debug.

keen valve
#

ok

sterile socket
#

Besides, we might want to handle things like entering and exiting a new state later on, which ChangeState would be useful for.

keen valve
#

alright. so... do you wanna focus on falling now?

orchid matrix
#

just as long as he fix how he is using that, im in haha

keen valve
#

u got it

sterile socket
keen valve
#

do that first?

sterile socket
#

besides, there's not much to fix in the falling state, you just need to figure out what vector to return.

keen valve
#

yeah

#

then i guess let's focus on changestate first

#

so we want change state to change the state you're in

sterile socket
#

Yes.

keen valve
#

ok. so that's the part i'm having the most difficulty with

sterile socket
#

Do you remember that in your pseudocode you had CAN *DO SOMETHING* thing?

keen valve
#

yes

sterile socket
#

well, that's where you can use that logic.

keen valve
#

so like
if (state != this || state != that)?

sterile socket
#

go through the state in the ChangeState and see what states you can transition to from your current state.

sterile socket
keen valve
#

oh

#

let's start with horizontalmovement state

sterile socket
#

All you need is to put the correct conditions in the right case and change state if they're satisfied.

keen valve
#

ok. so for horizontal movement, you're not jumping, you're not attacking, and you're not croucing, and you're not climbing, and you're not hurt

#

oh! first thing's first

#

what type is changestate()? is it a vector2 like updatestate?

#

i guess not because all we're doing is changing the state you're in

sterile socket
#

You might want it to return bool in the future perhaps, but it's fine as it is for now.

orchid matrix
#

you can actually use any type, as long as you are returning the right type

sterile socket
#

You can change that later.

#

keep it simple

keen valve
#

aight

orchid matrix
#

imagine void as the pawn in a chess

keen valve
#

horizontalmovement. i guess my main issue isn't idk what you mean when you say put in the conditions, but how to word it

#

like how to code that

orchid matrix
#

if you promote it, it have to stay as that

keen valve
#

cause you said it's not just
if this is true

orchid matrix
#

so void is perfect if a function dont need to return any value in the moment

sterile socket
keen valve
#

so for horizontal, it's possible to change to pretty much any state ig

#

and they depend on stuff like if i'm pressing jump or crouch or getting hit or if the climbing bool is correct

sterile socket
#

Is it possible to change to a falling state from horizontal movement though?

keen valve
#

no midair control

#

until you hit the ground

#

i know this isn't right but can you tell me what i'm doing wrong?

sterile socket
#

Do you understand how switch statements work?

keen valve
#

yes

#

it's like a list of if statements

sterile socket
#

Okay. So what is state equal to when you're in this case?

keen valve
#

horizontal movement

sterile socket
#

then why would you need addition checks?

keen valve
#

idk

sterile socket
#

Why did you type what you did?

keen valve
#

idk you told me to put in all the conditions you can change to horizontal movement

sterile socket
#

By conditions, I mean all the other things beside the current state

keen valve
#

so put in... all the states you can change to horizontal movement

#

put not horizontal movement

sterile socket
#

That's the point of the state machine: it narrows down the conditions you need to make for your logic, because you know what state you're in and only need to make checks relevant to that state.

sterile socket
keen valve
#

not right tho

#

so idk

sterile socket
#

Yes

keen valve
#

oh

#

so do put that in

sterile socket
#

Yes, changing state is what we're doing there, no?

keen valve
#

yeah.

#

so put in all the states you can change to

sterile socket
#

yes.

keen valve
#

like this?

#

so then that doesn't seem to make sense. could you explain it to me a bit

#

so i'm putting in the states. the machine looks at all these states. and then... what?

keen valve
#

i did it wrong

sterile socket
#

Let's pretend you're the CPU again. Go through your ChangeState method line by line and describe what happens. Let's assume that state is HorizontalMovement when you start.

#

Before that.

#

What is the purpose of ChangeState method?

keen valve
#

to change the state

#

ohhh waiwaiwaiwait

sterile socket
#

Yep. Now step through the code.

keen valve
#

so...

sterile socket
#

Okay

keen valve
#

not right, i guess?

#

cause i was thinking, you told me to put in all the conditions that would cause the state to change

sterile socket
#

Not wrong

#

I was just wondering if that's the only condition you need. But perhaps it is.

#

Complete it for the rest of the states and test.

keen valve
sterile socket
keen valve
#

no

sterile socket
#

Then there's no need to check it.

keen valve
#

ahhhhh

#

fair

sterile socket
#

In fact, if you were, then there's probably a bug somewhere else.

sterile socket
# keen valve if i were what?

If you were not grounded in the horizontal movement state, then there is probably a bug somewhere else in your code.

keen valve
#

oh

#

so how's this?

sterile socket
#

well, test it out.

keen valve
#

it seems like it works

#

gotta do the other states though to see if i'm right

sterile socket
#

Good. Now to the same for the rest of the cases.

keen valve
#

so for jump, how would i switch from that to horizontal movement? if i landed?

#

here's the tricky part for jump. i can jump horizontally, but no midair control. can i horizontal move in the jump state?

sterile socket
keen valve
#

jump

sterile socket
keen valve
#

true but i ain't gonna change it

sterile socket
#

What does the state do?

keen valve
#

you... move up in the air... then fall down

sterile socket
#

is falling down really part of the jumping?

keen valve
#

no

sterile socket
#

So it's not part of the jump state

keen valve
#

it's it's own state

sterile socket
#

Then what does jump state do again?

keen valve
#

move up

sterile socket
#

Okay. Does it move up indefinitely?

keen valve
#

no

sterile socket
#

Then describe what that state actually does(including how long or under what condition)

keen valve
#

if you're on the ground, you move up in the air for, like, a half second, then change to the fall state

sterile socket
#

So there's a a time constraint? Or maybe distance constraint?

#

Let's assume it's time

#

Then what would be the condition to change from jumping to falling?

keen valve
#

when you reach a certain time

sterile socket
#

more precisely, "when certain time has passed since jumping(entering the jump state)"

keen valve
#

yeh

#

so when in the jump state... countdown to a certain time, then switch to the fall state

#

and you can't do anything else when jumping so i guess do that

sterile socket
#

What do you think that does?

keen valve
#

uses the coroutine to countdown and then change to the falling state

sterile socket
#

Turn into a CPU for a moment and step through the lines in this case, what happens line by line?

keen valve
#

startcoroutine. go to the coroutine i made (jumpCountdown)
then go to the falling state. set the state to the result of that

sterile socket
#

and that all happens within one frame

#

right?

keen valve
#

yeah

sterile socket
#

So you basically transition to the Falling state immediately

keen valve
#

ohhh

#

so what do i do? put that state = movementstate.falling into the coroutine?

sterile socket
#

Well, that's one option yeah. But there's another problem.
Let's say it's the second frame of being in the Jumping state, what happens then?

keen valve
#

you're... still jumping?

sterile socket
#

Code wise. When it runs through the ChangeState method

keen valve
#

oh.

#

it runs the coroutine again?

sterile socket
#

*it starts a new coroutine

#

In addition to what is already running

keen valve
#

oh

sterile socket
#

So every frame of the jump state, you start one more coroutine

#

That can't lead to anything good

keen valve
#

yeah

#

what if... no coroutine... but...idk a for loop?
but i guess that wouldn't work either

sterile socket
#

let's hear that though? for loop what? And why do you think that it's not gonna work?

keen valve
#

so we make a variable, have a for loop that either subtracts or adds to that variable until it reaches a certain value, then you switch to falling

#

but like the coroutine, every frame, it's just gonna cause the for loop to repeat

sterile socket
#

Well, yeah. More importantly, for loop is gonna loop through all the iterations before moving to the next line of code. Code is executed one line at a time. while your loop is being executed, the whole engine is frozen. That's why if you were to do something like wait actual real time in that loop, it would've frozen your game entirely for a certain period.

keen valve
#

yes.

sterile socket
#

The issue is essentially in the fact that we want to execute some code only once when entering a state, right?

keen valve
#

yes

sterile socket
#

That's where another essential part of the state machine can be helpful. When we change state, we could implement an EnterState and ExitState functions that would be called precisely once, when we enter/exit a state.

keen valve
#

oh. so... make two new methods called enterstate and exitstate?

sterile socket
#

So far, we don't have much use for ExitState, so start from implementing EnterState

keen valve
#

more methods... greeeeat 🤣

sterile socket
#

Ideally, it would've been several separate classes, but your state machine is simple enough to have it hardcoded in one script

keen valve
#

so what type would it be? just void?

#

but it couldn't, right? it needs to return something right?

#

return the state you're changing to?

sterile socket
keen valve
#

oh

#

so i guess then i'm confused

#

we are making a new method, right?

sterile socket
#

Yes

#

What is the confusion about?

keen valve
#

i guess what the type is

sterile socket
#

You're thinking from the wrong end.
A method type is dictated by what we want it to return.
What do you want the EnterStateMethod to return?

keen valve
#

the state?

sterile socket
#

Why?

#

What's the purpose of the EnterState method?

keen valve
#

to enter the state

sterile socket
#

Well, that's not wrong, but why did we make it in the first place?

keen valve
#

so you only enter it once

sterile socket
#

We already enter the state once

#

Think. What was the issue that we were trying to solve

keen valve
#

the starting a new coroutine over and over

sterile socket
#

Basically, the same code executing more than once, right?

keen valve
#

yes

sterile socket
#

Taking that into account, what is the purpose of EnterState method?

keen valve
#

only running it once

sterile socket
#

That

#

So, going back to it returning something, does it need to return anything?

keen valve
#

no

#

so it's a void

#

aight so what do we put in this method? the coroutine?

sterile socket
#

It would be wise to have a switch inside as well so that we can reuse it for all the states

keen valve
#

ohhh so true

sterile socket
#

And in each of the cases you add code that you only want to be executed once when you enter that state.

keen valve
#

yes

#

so....

sterile socket
keen valve
sterile socket
#

That does nothing at all

keen valve
#

oh

sterile socket
#

What was it that you wanted to do after the countdown

keen valve
#

go to fall state

#

put that in there?

sterile socket
#

yes

keen valve
#

so... let's talk about that fall state

#

since we discussing it

sterile socket
#

Ok

keen valve
#

vectors. i'm thinking what should y be?

sterile socket
#

What do we use that vector for?

keen valve
#

physics

sterile socket
#

physics?

keen valve
#

your rgbd physics

#

er velocity

#

sorry

sterile socket
#

And what does that do?

keen valve
#

changes the velocity

#

moves the player

sterile socket
#

Right. What kind of velocity do we want when falling?

keen valve
#

negative

sterile socket
#

Well, that's not wrong.
But thinking from real life physics perspective, how would you calculate a falling object velocity?

keen valve
#

time * acceleration

sterile socket
#

and what is acceleration equal to?

keen valve
#

isn't it, like, -9.81?

#

or do you mean the acceleration of gravity?

sterile socket
#

that IS the acceleration of gravity

#

To sum it up, when an object is falling, it only has gravity force acting on it.

keen valve
#

yeah

#

unless there's an outside force

sterile socket
#

So, how would the velocity change between frames when falling?

keen valve
#

it gets faster

sterile socket
#

Write it as an equation

keen valve
#

velocity = gravity * time

sterile socket
#

That's if you calculate the velocity disregarding the current velocity

keen valve
#

oh

#

idk

sterile socket
#

I mean isn't it very simple? You just want to add up to the current velocity
velocity = currentVelocity + gravity * time since last frame.

keen valve
#

yeah.

sterile socket
#

Current velocity is the same variable, in code that would look like this:

velocity = velocity + gravity * deltaTime;

Or even simpler:

velocity += gravity * deltaTime;
#

However

keen valve
#

so... y would be rgdb.velocity.y + gravityScale * deltaTime?

sterile socket
#

What about the x part then?

keen valve
#

MovementSpeed?

sterile socket
#

Why?

#

what do you want to happen on the x axis when falling?

keen valve
#

...the velocity of your x

#

so... rgbd.velocity.x?

sterile socket
#

Yes. Although, velocity of your x doesn't sound like the correct way to say it.

#

Anyways, since both x and y use your current velocity, you can expand that expression.
Instead of creating a new vector, you can add the additional velocity vector instead, like I did here:

velocity += gravity * deltaTime;
keen valve
#

so is this right?

sterile socket
#

Yes, but let's rewrite it as I suggested to make it cleaner.

keen valve
#

so no vector?

sterile socket
#

no vector

#

vector math, you know?

keen valve
#

remind me

sterile socket
#

This is what you have now:
Va = new Vector2(Vb.x + Vc.x, Vb.y + Vc.y);
It's the same as this:
Va = Vb + Vc;
Which one reads better?

keen valve
#

second

#

this gives me an error too

sterile socket
#

First of all, you don't need to assign it here. You return the result instead.
If you do want to assign it to a new vector first, then do that before returning.

#

And second, gravity needs to be represented as a vector.

keen valve
#

if it's all the same to you, i'd prefer to use the first one i did

#

the one you said was right

sterile socket
#

I'd prefer that you learned something from that, but okay...

keen valve
#

yeah but just to save time

#

it's already 1:11 in the morning here

#

lol

sterile socket
#

That being said, it's not exactly right.

keen valve
#

oh it's not?

sterile socket
#

Well, it would have been if you were controlling the object movement entirely by yourself.

keen valve
#

oh

#

so what needs to be changed?

sterile socket
#

But since you're using a dynamic rb, unity physics would apply forces to it as well

#

Like gravity

#

In fact, the value of velocity already includes the calculation of gravity on that frame

#

So, what do you need to return?

keen valve
#

the velocity of the object in every frame?

#

or... idk

sterile socket
keen valve
#

so anyways how would we do that?

sterile socket
#

Well, if physics system already does everything you need for you, do you need to modify the velocity?

keen valve
#

no

sterile socket
#

Which means..?

keen valve
#

don't include the velocity

#

just the gravityscale and deltatime?

sterile socket
#

where did that came from?

keen valve
#

idk

#

but don't include the velocity?

sterile socket
#

Why not?

keen valve
#

cause it's alrady doing the velocity

sterile socket
#

It is, but what are we doing with the returned value?

keen valve
#

adding it to the velocity of rgbd

sterile socket
#

where are we adding it?

keen valve
#

in update

#

or from the updatestate, which then gets called in update

sterile socket
#

paste the snippet

#

of that line

keen valve
#

what line?

sterile socket
#

Where we "add" it

keen valve
sterile socket
#

This is not "addition", is it?

keen valve
#

no

#

wait that doesn't need to be changed?

sterile socket
#

bingo

keen valve
#

it does?

sterile socket
#

Ah, I thought that was response to a different question...

#

No. It doesn't need to be changed

keen valve
#

ok

sterile socket
#

But if it's not "addition", then what is it?

keen valve
#

setting

sterile socket
#

Yep

keen valve
#

so we're setting the velocity

sterile socket
#

So we are setting the returned value

keen valve
#

yes

sterile socket
#

Yes. Taking that into account, if physics system already calculates everything for us, what value do we need to set?

keen valve
#

the x and y

sterile socket
#

?

keen valve
#

in the vector

sterile socket
#

Do we need to change the velocity in any way?

keen valve
#

no

sterile socket
#

Then what value do we assign to velocity?

#

In order for it not to change

keen valve
sterile socket
#

Why..?

#

What does y need to be in order for x value to remain unchanged?

var x = some value;
var y = ?
x = y;
keen valve
#

0

sterile socket
#

hey hey. That's serious math issues there...

keen valve
#

... rgbdvelocity.x?

sterile socket
#

How is that related to my question?

keen valve
#

idk

#

i'm just trying to figure out what x and y is

sterile socket
#

Ok. Go through this line by line with your answer and see what value x has in the end.

var x = some value;
var y = ?
x = y;
sterile socket
keen valve
#

var x = 0
var y = 0;
0 = 0;

#

i...don't know

sterile socket
#

You can't change x. Only y

keen valve
#

okay let's start over

#

let's do one parameter at a time

#

starting with x

sterile socket
#

Yes. It has some value. You don't know what it is

keen valve
#

so we don't want x to change... unless i'm jumping while moving then it changes but you just can't control yourself in midair

keen valve
sterile socket
#

if you want, we can rephrase it like this:

var x = c;
var y = ?
x = y;
keen valve
#

x = y * c?

sterile socket
#

no. I'm not asking you to solve for x. I'm asking you what would you put in place of ?

keen valve
#

c?

sterile socket
#

Yes. But let's say you can't use c there.

keen valve
#

y = x?

sterile socket
#

that

#

in the end you need to assign x to itself for it to not change.

#

Now going back to what we need to assign to velocity if we don't want it to change

keen valve
#

itself?

sterile socket
#

So, what do we need to return from the falling state?

keen valve
#

the velocity

#

so it would be new Vectror2(rgbd.velocity.x, rgbd.velocity.y)

sterile socket
#

Why create a new vector2 though?

#

What type is rb.velocity?

keen valve
#

so just return rgbd.velocity

sterile socket
#

Yes

keen valve
#

ok

#

here's my new problem

#

i can jump, yes

#

but i can't move forward when jumping while moving

sterile socket
#

Share the jumping state update code

keen valve
#

mean this?

#

or you mean the method?

sterile socket
#

This is the general state update method. where do you update the jump state?

keen valve
sterile socket
#

what is air speed and movementspeed?

keen valve
#

airspeed is the movement of your character while jumping

sterile socket
#

Where do you set it?

keen valve
#

at the start of the script

#

but i don't think it's doing anything

sterile socket
#

What value?

keen valve
#

like, 50

#

which is why idk if it's doing anything

sterile socket
#

and MovementSpeed?

keen valve
#

3

sterile socket
#

It should be doing something indeed.

keen valve
#

but it ain't

sterile socket
#

Are you just moving vertically then?

keen valve
#

yes

#

which, if i was standing still is fine

sterile socket
#

Okay. Here's where the current approach is good for debugging.

#

instead of assigning the velocity to an rb directly, assign it to a public/serailized variable.

#

then assign that variable to the rb velocity.

#

With this you'll be able to see the value in the inspector.

keen valve
#

trying that. getting an error

sterile socket
#

You don't assign it at declaration

#

and velocity is not a float

keen valve
#

okay... assign it at start?

#

and what is it? a vector?

sterile socket
#

No. I already mentioned where you need to assign it.

sterile socket
sterile socket
keen valve
#

but i did?

sterile socket
keen valve
sterile socket
#

pay attention to the word "instead". It's very important

keen valve
#

but...

#

ok

sterile socket
#

There should only be one place in your code where you assign rb velocity. So when I say "instead" it can only be that place.

keen valve
#

when you say "place" you mean place in the script?

sterile socket
#

yes

keen valve
#

or, like, place in a variable?

keen valve
sterile socket
keen valve
#

in awake

sterile socket
#

since when..?

keen valve
#

since... never

#

in update?

sterile socket
#

Yes

keen valve
#

so... we make... a variable there

sterile socket
#

we assign the variable there

keen valve
#

ok

#

i assigned it.

#

now i can't move at all

sterile socket
#

because that's only half of what I told you to do

keen valve
#

oh

#

not this?

sterile socket
#

nnn... Several things are in the wrong order...

#

think logically.

keen valve
#

sure they are

sterile socket
#

You still want the rb velocity to be assigned properly as before, right?

keen valve
#

yes

sterile socket
#

and you also want the velocity variable to be updated, right?

keen valve
#

this?

sterile socket
#

Well, that'll do

keen valve
#

ok

#

so i'm lookin at x when i jump. it automatically gets set to 0

sterile socket
#

Hmmm

#

Share the whole script

keen valve
sterile socket
#
  1. There's an issue with EnterState(or rather where you're calling it). We didn't finish with it yet. But That's probably not the cause of the issue.
#

Can you confirm, that when you're falling, the active state is actually "falling"?

#

aah

keen valve
#

yeh

sterile socket
#

Wait. Nvm. The issue is with the Jump state, right?

keen valve
#

ig

sterile socket
#

Ok

#

You said that you set airspeed somewhere, but I can't find it in your code.

keen valve
#

you're so right

sterile socket
#

That's half of the problem.

#

fix it and see how it works

keen valve
#

you only go right

#

when i press space and left, i still go right

sterile socket
#

Yep. Do you understand why?

keen valve
#

because i'm always setting it to a positive number

sterile socket
#

Yes. To be precise, because you're always setting it to the same number

keen valve
#

yes

#

so how to fix

sterile socket
#

What you want is it to be affected by the velocity when you started the jump, right?

keen valve
#

yeh

#

so add... velocity.x?

sterile socket
#

Then you just need to remember velocity x at the start of the jump and use that

#

Or rather

#

you just leave the x velocity unchanged during jump

keen valve
sterile socket
#

test it

#

also, is that comment still relevant?

keen valve
#

nope

sterile socket
#

Then remove it

keen valve
#

i did

#

hmmm. when i press jump and direction, they don't move as far as when i'm moving and jumping

#

what if i multiply by airspeed?

#

dunno if that'll work

sterile socket
#

I guess it's because input doesn't go to 1 right away, so your velocity is increasing over some time

#

Wait, you're not using axis...

keen valve
#

can we... have you go max velocity right away?

#

cause you really only just going one speed

sterile socket
#

Or do you? What's InputHandler?

keen valve
#

yes that uses GetAxisRaw

#

er not raw

sterile socket
#

Ah, GetAxisRaw does return discreet values. So the problem is somewhere else.

keen valve
#

just GetAxis

sterile socket
#

Change it to Raw

keen valve
#

ok

#

didn't do anything lol

#

y'know what? it's fine as is ig

#

anyways i assume same for knockback except just going to opposite direction?

#

or maybe make a different variable for knockback so you're always getting knockbacked the same distance?

sterile socket
sterile socket
sterile socket
keen valve
#

dlich imma be real with you

#

it's 2:30 in the morning and im tired af

#

can we continue this another night?

sterile socket
#

Yes. You could continue on your own when you have time. It's really just assigning the correct velocity in different states now. Refer to your previous code where needed. If you face any issues, try solving them on your own first. If nothing works, come back and share the updated code.

There's still the EnterState that doesn't work correctly now, so I guess I could help with that.

keen valve
keen valve
#

@sterile socket @orchid matrix hey! so I made a change to checking for ground. for the falling state, the isgrounded method didn't work because it would automatically switch to horizontal movement state even in the air. so i changed it from a raycast to overlapcircle like i did in my previous script. problem here is that it works fine when i jump, but for knockback, for some reason, i won't switch back to horizontal movement when i land. I'll post the script here. But could anyone explain what's wrong here? https://gdl.space/bahobalawi.cs

sterile socket
#

Falling?

keen valve
keen valve
sterile socket
keen valve
#

if isgrounded() == true

sterile socket
#

So that would mean that this condition is not satisfied, right?

#

Or that we change to the falling state again immediately.

#

Here's how you can debug it:
Add a previousState variable of type MovementState in your ChangeEtate method and ssign it the state value.
At the bottom of the method check if previousState is equal to state, and if not Debug log the new current state.

keen valve
#

specifically if i'm, like, walking off a platform

#

think i solved it

sterile socket
#

Sorry, can you rephrase it? If you're walking off the platform what happens? And especially, what states changes happen?

keen valve
#

so i made it smaller. seems to work now

#

anyways so make a previousstate for movementstate and check to see if it equals state

keen valve
sterile socket
#

What error?

keen valve
sterile socket
#

Ah, well, you didn't clean your namespaces as I suggested before, did you..?

#

Is there any specific reason you need System.Diagnostics in your script?

keen valve
#

no?

#

what do i delete?

sterile socket
#

The "using" statement.

keen valve
#

gotcha! i was looking at the wrong script lol

#

okay so basically it's saying i'm switching between knockback and horizontalmovement constantly

#

you think it might have something to do with my hitdetector script?

sterile socket
#

Possibly. What are the conditions for switching between these 2 states?

keen valve
sterile socket
#

Feel free to share your thinking process of solving the issue and might give feedback on it.

keen valve
#

ok

#

figured it out

#

i think

#

okay yeah it works. just gotta, like, add invincibility frames to my character and it's golden

#

also an animation issue, you don't switch back to your idle state, but i think i can fix that later

keen valve
#

@sterile socket I think I got the majority of it down (say for some animation issue). there are a couple of bugs, but the one i really wanna focus on is the physics for climbing steps

#

i mentioned it's kinda wonky

sterile socket
#

Describe the issue and share the updated code

keen valve
#

ehh it's kinda hard to describe but going down the steps is weird

#

like, you can't really go down them, but you don't go up either if i press down. you just, sorta... either not move or just float slowly down

keen valve
sterile socket
#

Did you confirm that the same state is active during that time?

keen valve
#

odd... sometimes you don't even switch to climbing state at all

sterile socket
#

From what state?

keen valve
#

horizontalmovement

sterile socket
#

If that's really what's happening, then there's only one possible explanation. isOverlapWithLadder is not being set to true.

#

I think the issue is in your OnTriggerEnter2D

        if (collision.gameObject.CompareTag("Ladder"))
        {
            if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.DownArrow))
            {
                isOverlapWithLadder = true;

            }
        }
#

Is there a guarantee that one of the inputs is true during the physics update when you first start colliding with the ladder?

keen valve
#

i think i got it to work

#

if anything happens, i'll let you know

#

but basically what i did was use an overlap circle instead of on trigger

#

cause i was thinking: my character doesn't have a trigger

sterile socket
#

The reason is most likely what I pointed out previously.

#

You solution fixes it too, because you probably do the overlap check in regular update.

keen valve
#

i think something else is triggering my being able to go up steps

#

cause i'm still always in horizontal movement even when i go up steps

#

and i even untagged the change from horizontal to climb in changestate and it still works

#

only difference is i don't have the bug anymore

#

i wonder if that's a glitch in unity's part

#

lemme restart unity

sterile socket
#

No. There's no such "glitches" on the unity part.

#

there's clearly an issue with your logic.

keen valve
#

yes

#

question is: if it's working perfectly fine: does that still make it an issue?

sterile socket
#

If it works perfectly fine, then there is no issue, is there?

keen valve
#

no i guess not

#

hold on...

#

ah. unity didn't save the update for changing the stepscheck

#

odd

#

except oh wait it did

#

except now I'm facing the same issues again

keen valve
#

it WAS just a glitch LMAO I'M DEAD

sterile socket
#

I'm not sure I even understand what was it that you called a "glitch"

keen valve
sterile socket
#

that doesn't explain anything

keen valve
#

idk how else to describe it. I close unity. i open it back up. everything in the code is the same, but now the issues i had are back

sterile socket
#

What issues??

sterile socket
#

None of what you're saying now makes any sense. You said that it got fixed after restarting unity. But now you're saying the opposite thing..?

keen valve
sterile socket
#

Then why did you restart unity if there was no issue?

keen valve
#

because i wanted to see if the "fix" was really just because i'm a coding genius or just unity acting up LOL

sterile socket
#

So the conclusion is?

keen valve
#

and turns out it was the second

#

it was just unity acting up

#

so we're back to square one

sterile socket
#

Unity "acting up" fixed your issue before the restart?

keen valve
#

yes

sterile socket
#

That doesn't make sense, but ok.

keen valve
#

i know it doesn't but that's my only explanation

sterile socket
#

So what's the issue that is left?

keen valve
#

basically you can't go down steps and if you try to mid-climbing, you just float slowly down

#

and you also don't switch to climbing state

sterile socket
#

Share the updated code

keen valve
sterile socket
keen valve
#

i don't know. must be something else calling it

#

but what? idk

sterile socket
#

What calling what?

#

What do you see in your console about state changes? Or in your inspector, what is the current during climbing?

keen valve
#

okay this is weird

#

now I CAN change to climb state

#

lemme restart unity to see if this is also unity acting weird

#

so you can climb

#

but new problem time

sterile socket
keen valve
#

don't go back to "horizontalmovement" when touching ground

#

or i mean... platform

#

cause, like, i climb up steps, usually i'll land on the platform, but now... i just keep climbing

#

i don't go back to horizontal state

#

i wanna say it's a hitbox thing

sterile socket
#

What is the condition for switching to a horizontal state from climbing?

keen valve
#

nah nah more than likely it's hitbox related

#

cause if i climb back to the ground, it'll switch back

sterile socket
#

The most important problem with your thinking is that you jump to conclusions.

keen valve
#

oh

sterile socket
#

There's always a certain chain of logic that you can follow from the issue occurring to it's cause.

#

If you don't check each link in the chain, you might miss the cause. By trying to fix something that is not the cause, you might introduce more bugs

keen valve
#

i get that

#

what do you think the problem is?

#

lemme try overlap circle again

#

okay so... i check debug

#

apparently, i'm switching to climb and then immediately switching to horizontal movement

#

i mean, it works... but now when i reach the platform on top the steps, i fall through it lol

#

but y'know what? THAT is a hitbox issue cause i tested repositioning the platform and it worked

keen valve
#

Okay so I restarted unity, I even restarted my computer, unplugged my flash drive (safely), plugged it back in, and opened unity

#

Safe to say it’s not messing around anymore and this issue… somehow… resolved itself

#

I have no idea why, it doesn’t make any sense, Dunno how long it’ll last, but bugger all I’m not gonna complain 🤣

#

Think I figured out what the problem is

#

Or was

#

I am climbing. It does set me to the climbing state, but then immediately switches to horizontal movement. Which means if I were to, like, comment out the climb logic in changestate, I’ll have the same issues I mentioned about not being able to climb down

#

I’m HOPING that’s the problem. Because that seems like it

sterile socket
keen valve
#

It does but only for, like, a split second

sterile socket
#

Ok, so I guess the real issue is that your debugging is incorrect. You don't consider all the available information or you misinterpret it. Making you draw wrong conclusions.

sterile socket
keen valve
#

Well I showed you my debugging script, yes?

sterile socket
#

debugging script?

keen valve
#

Er

#

Code

#

The thing you told me to add

sterile socket
#

Yeah. There's no problem with it.

#

Although it could've been more verbose to make it easier to understand the situation

#

for example, you could print something like transitioning from *previousState* to *currentState*

#

What I meant though, is that you did not look at the console at all or did not interpret the logs correctly.

keen valve
#

Yknow what it could be?

#

Since the condition for changing from climb to horizontal is if I’m on the ground, and when I touch the ladder, I’m on the ground

#

This happened with jump too

sterile socket
#

There's only one way climbing -> horizontal movement transition can happen.

#

Investigate why it happens and you'll be able to fix it.

keen valve
#

So we gotta figure out how to make it so you climb, keep climbing, and when you get to your destination, THEN you go back to horizontal movement

#

Cause, like, if I switch to climb, what’ll happen is even if I’m touching the platform on top the steps, I still keep climbing

sterile socket
#

Logically thinking about the correct behavior, is just touching the ladder means that you're climbing?

keen valve
#

No, also when you press up or down

#

Ohhhh!

#

But then, wait, what if I’m still pressing up when I reach the top? I’ll still climb

#

Even if I hit the ground, I’ll climb in the air

sterile socket
#

Are you still touching the ladder, when you "climb in the air"?

keen valve
#

No

sterile socket
#

So, there's another case where you want to transition out of the climbing state, right?

keen valve
#

Yeah

#

So I make another state called DoneClimbing?

#

Set it for when I’m not touching the steps?

sterile socket
#

No

#

another case where you want to transition out of the climbing state
Means that there is another condition that could lead to state change

keen valve
#

Ohhh. Like “if isClimbing == false”

#

IsClimbing being the overlap circle

#

I’m not touching the ladder

sterile socket
#

yes

#

isn't it logical? If you're not touching the ladder, what are you supposed to climb?

keen valve
#

True

#

I mean I just added that to the script and now I have no bugs whatsoever

#

Cause, like, at first, I had a bug where if you climb then exit out of climbing then immediately go back to climbing you’ll be permanently stuck in the climb state

#

But I don’t have that issue anymore now that I added that condition

#

But I know that’s not the issue here. The issue is switching to climbing state.

sterile socket
#

Then that's just another different bug

keen valve
#

Yeah

sterile socket
#

-analyze what's happening
-why it is happening
-confirm with available information. Output more debug information if necessary
-once's you know what the cause, think of what solution would fix it without causing new bugs

keen valve
#

Hmmm well we know
-I’m immediately switching from climb to horizontal
-we know this through the debugging
-it’s more than likely cause of the groundcheck condition
-gotta figure out how to make it so that you can continue to stay in the climbing state until you are no longer touching the steps

sterile socket
#

-I’m immediately switching from climb to horizontal
This basically means, that the current condition is to weak, it's satisfied in situations even when you don't want to change the state.

#

Figure out what's the additional condition needs to be.

keen valve
#

Well I thought it would be (&& isClimbingSteps false) (the overlap circle) but that ain’t it either

sterile socket
#

why not?

keen valve
#

Cause I tested it

#

Didn’t work

sterile socket
#

what's the logic behind it though?

#

just testing is not enough

#

you need to understand why

#

maybe there's a fault with your testing

#

how can you know that without understanding the cause?

#

Or maybe there's an additional factor that affected the testing. Either way you wouldn't understand from just testing.

keen valve
#

That’s… new

#

But okay let’s see

#

If I’m grounded and I’m not touching the steps and not pressing up and down, that means I’m not going up the steps

#

If I DO do those things, I am climbing the steps

sterile socket
#

Okay

#

Is that what you actually have in code?

keen valve
#

…minus the up and down arrows

#

Shoulda added that huh?

sterile socket
#

According to your logic, that should be there, no?

keen valve
#

Okay I added it

#

… nothing

#

Didn’t do anything

sterile socket
#

What nothing?

keen valve
#

I mean it didn’t work

sterile socket
#

So what's the current issue again?

keen valve
#

Well technically none, but it’s just not switching to climbing state

#

Or it is, but then immediately back to horizontal movement

sterile socket
#

That sounds like an issue

sterile socket
keen valve
#

Second one

sterile socket
#

And what are the conditions of switching from climbing to horizontal now?

keen valve
#

As of now

#

If (isGrounded() == true || (isClimbing() == false)

sterile socket
#

So, if you're on the ground or not overlapping with a ladder

keen valve
#

Yes

sterile socket
#

Is that enough?

keen valve
#

Probably not

#

You know what is weird tho?
If I change the || to &&, or just get rid of that second condition altogether…

#

If I go up the steps and then midway stop climbing and then climb again, THEN it switches to climbing state

sterile socket
#

Start with changing to &&

keen valve
#

I already did

#

Oh you mean analyze

sterile socket
#

Yes.

keen valve
#

So if I’m touching the ground AND not touching the steps

sterile socket
#

Yea

keen valve
#

Which is… not what I want?

sterile socket
#

You don't want that?

keen valve
#

I mean I guess I do but, it doesn’t work

sterile socket
#

What doesn't work?

keen valve
#

The climbing

#

I still go to horizontal

#

Only difference is if I let go of the arrow keys and then press them again midnclimb, then it switches to climb state

sterile socket
#

Which means that one of the conditions doesn't return the value that you expect.

keen valve
#

And I keep climbing even if I’m not touching the ladder

#

…it can’t be the groundcheck could it?

sterile socket
#

If you suspect one of the conditions, just debug them.

#

Make sure you always know what values they have.

keen valve
#

I don’t know

#

Look, man…

#

Okay

#

Dlich can I be honest with you?
It’s running perfectly fine. I have no issues going up the steps. And tbh, if it were to be set to climb state, it’d have numerous glitches in it like continuing to climb in the air. Plus, it’s going on 2 in the morning here. I think I might just look back into it later and move on with stuff like animations and maybe tuning out some minor glitches. Thank you for your help tonight. If I need any help, I’ll let you and the others know. But for now, I might need to stop for the night.

#

I'm sorry.

sterile socket
#

I mean, you shouldn't be saying that to me. It's your project. I don't really care whether it's working or not. I'm here only to help you learn something about game dev.

keen valve
sterile socket
#

It's not like your reply time matters, so you don't need to say that every time. Just reply when you can. That's the same for me.

keen valve
#

Ok

#

Idk, I just feel bad because I know I should know this stuff but then I get to actually programming and it’s hard to problem solve for me. Maybe I just need some more practice and learning, and who knows? Maybe after this game is done, I can practice some more. Make some more smaller games, get better at it. I just feel like my progress on learning this stuff is very slow.

sterile socket
#

Yeah, I think that's what college does to you. Instead of actually pursuing learning, you aim for the grades and finishing it off. In the end, even if you did get something into your head temporarily, your brain just discards it as not important later, when you have to move to another topic.
Practical things like software development, can only be learned with constant practice, preferably, while working on a project that is actually meaningful to you and not your grades.

#

Don't misunderstand me. I'm not saying that you should quit college. I'm just saying that it's not enough. If you don't do some self-learning in addition, you're gonna graduate without any actual skills and with only random information noise in your head.

keen valve
#

You’re right. I need to keep practicing after college. Maybe even take some additional courses too

flat nymph
#

Well I missed quite a lot

#

ugh I hate the time zone difference

#

Sorry @keen valve

#

The difference of time from where I live and the USA is ~9 hours 💀

#

Anyways, I'm aware I'm probably texting at 4 AM for you, but @keen valve , focus on college and try your best to learn new hobbies. Don't feel bad for asking questions to the community

#

we're here to help