#Moving a player(going insane)
1 messages · Page 1 of 1 (latest)
Can you give me a short rundown on what you mean or have so far? I am not up to date
What behaviour is happening?
What do you want to happen?
Then we can look at code
so what i want to happen is have a toggle where when you press I, you 'go insane' causing the player to walk and turn in random directions.
the player is not moving a t a l l
Gotcha
Ok so Lets just do some simple debugging, you may already have some in place.
Do you know if the toggle is working?
meaning When you click i the bool is changing
Thats the first step
yes. i have a toggle in the insane and sane functions in the insane code that says "insane" or "sane"
Ok so comment out everything except the debug.log in Insanity, just for simplicity sake. We are ONLY testing to see if the method is being called correctly at first, then we can worry about the movement
Yupp. And we will know if its working if we see in the console, the correct thing
If we dont see Anything in the console, it isnt even being called
Which would be the first thing to fix
Sorry i meant sanity
oh
Therse a button called 'collapse' at the top of the console tab
click that and itll make it easier to read console
all of this after I press 'I'
Ok so show me the code where you are calling these functions from
Seems to be working so far
sorry if its a mess, i havent cleaned up and tbh i got the movement code from a tut
Just when you take your screenshot be sure to include the line numbers more to the left
just the first one 🤣
the like 10000000 or something was to test if maybe it was just moving realllllllllllllllly slow
Ok so heres the thing, yes your code is sloppy, but I wouldn't mind helping you out but we gotta clean some stuff up and in the process, you'll learn some things as to the thought process. Would that be something your ok with?
im all for it
Correct.
yup
Interchangable by the way when people say script or class
i inferred that
A class is a blueprint to how something is, or should work.
yeah
So your Insane class is a blueprint to........whether a player is insane or not.
Cool. By the way I'm going to ask you questions, these questions are going to hopefully get you to see some stuff
go ahead
So knowing thats what the insane class is about, whats your PlayerMovement class about?
its about, moving, jumping, and sprinting using the character controller
What are all those things, whats the common theme about them?
and it also asks for the input that makes you go insane
they are all movement based
yup
So first big thing I see wrong is, what is your Insanity() method doing?
what i tried to do is make it make you start walking, without needing input
and it also stores a reference to the script, so that i can access the methods and variables from the movement script
So in essence, your Insanity() method is
Getting a reference
Making the player move
yeah
In programming there is something called the SRP - Single responsibility rule. Its basically that a class should handle the things its about and not much else. What can you infer is wrong with Insane class handling movement?
that referencing it may make it harder than it needs to be
Thats one thing yupp. The main thing is that now you are handling movement in two places. And you are handling movement outside of the PlayerMovement class
OOOOOOOH
So when looking for movement related bugs, how many places can things go wrong
like mannnny
yupp
but to be simple 2+
So the idea is that Insane should only handle logic relating to .....being sane or insane. PlayerMovement should handle most of the .....movement
to startttttt
i moved all of the movement stuff out of the Insane class and took the stuff about insanity out of the movement class and put that into the Insane class
thanks
First thing, do you think we need a reference to movement in the Insane class anymore?
true i dont
yeahhhhhhhhhhhhhhhh
Ok so lets go to next step in Insane class
What is happening in the
If (input.blah blah balah Keycode.I)
{
}
Explain it to me
This whole thing we are about to do is going to maybe take a little bit longer but once you get it, its going to be awesome
so basically the first one says "hey have we pressed I? we have? insane. no? not insane
nope sratch
I only care about the I part
wrong order
crazy = !crazy. i was told that would work as a toggle
Yupp. Do you understand why that is?
Yupp, what are the possible values of crazy?
true,false, and maybbbbbbbbbbeeeeeee null?
i thought so i jsut wanted to check
Ok so it can only be true or false.
So this is whats actually happening
Lets say crazy is false at the start.
if (Input.GetKeyDown(Keycode.I))
{ //false true
crazy = !crazy;
}
Now that crazy is true, lets see what happens when you click i again
if (Input.GetKeyDown(Keycode.I))
{ //true false
crazy = !crazy;
}
I've written the actualy value above the statement so you can see
ok, sorry but that looks normal
What do you mean by that?
it seems to switch
Im just making sure you are aware of the actual value crazy is holding at any certain point.
gotcha
Yea its working. I just want to make sure you know what that means
i thought you were showing me that something was wrong
oh
So you have those 2 functions right?
yup, insane and sane
Actually ill start with something else.
Right below the first if statement in Update, you have 2 other if statements
Not at all
You dont sound rude lol
Dont worry about that
sounding rude that is, there is no emotion shown in typing most of the time
yeah
Ok so heres my questions to you
Are the 2 functions in Insane class going to SET whether a player is insane or are they just there to what should happen WHEN a player is sane or insane
crazy is the bool which says if insane or not, the two functions just dictate the workings of the insanity or sanity
Whats inside of Insanity() or Sanity(), is it all movement related? Or is there more to it?
Im asking in the future
all good
Thats what inside them right now. What you need to ask yourself is "What is everything I want to happen if Sane or Insane right.
oh gotcha
For example -
Lets say the only thing that happens when something goes insane, is movement related.
Then it would actually make perfect sense to put that logic of Insanity inside of PlayerMovement, because its only movement related, and PlayerMovement handles all of the movement
the thing that i want to happen, as this is for a prototype is that it makes the player run randomly and make turns randomly. I also want it to perhaps put some effects on, sound effects, what not
Ok well, for the purpose of this little learning thing goin on, lets say assume its movement only right now. You can always refactor (reorganize, rename, etc) it later if you want to add more stuff and then it makes less sense for the playermovement class to handle it all.
yup
So I dont want to go crazy here cuz I want you to learn alot personally but then again, I'm going to just nudge you in the right direction
ok
The if else below your if (i is pressed) statement seem redundant to me
oh yeahhhhh
They are saying to check every frame
If (i am crazy)
{
Do crazy stuff
}
else
{
Do sane stuff
}
Which is essentially what your toggle could be doing if the logic is placed inside of PlayerMovement.
yeah
So imagine this Insane class handles....being insane or not
yup
so put the insane methods into playermovement?
So where would you place the actual logic to being insane or sanity
Yupp.
This is why naming is kinda a big deal
I know you said you are 'fine' with your naming but the sooner you grow at this part of programming is makes everything so much easier
So for bools
gotcha
which can only be true or false
usually the names start with
is or has
isCrazy
hasApples
hasGold
isAlive
gotcha
so that when you call them from another class using the .
it makes sense
Imagine this
yup
than
if (Insane.crazy == true)
So just a thought
you dont have to rename everything right now
i already renamed crazy to IsCrazy
oh
This helps yourself and others know what things are just by glancing at it
Or what kind of things they are, such as is this a boolean, or is it a class?
yeah
isMoving is easy to see instantly, hey thats a boolean
yup
yup
Ok so How bout you get rid of everything you dont need in Insane class
really only need the toggle right now
woooh thats gonna be alot to unpack
It wont be near as hard now tho
oh
because this other class is so simple now
brb gtta grab something
k
back
When you get back can you paste your PlayerMovement code into a site like https://paste.ofcode.org/
so i can read it and not use screenshots
all you have to do is copy and paste your code into the text spot and make sure the pull down menu below is on c#
and then hit paste it
it will give you a link you can share with me
Ok so first off, do you think we need the public bool Insane; in this class?
i had it because i wanted to reference it but now i dont, so no
Well so you werent really referencing there. You were creating another one that has nothing to do with the one in Insane class
another bool, completely separate from the other Insane class
yupp dont need it
done
Cool now we have this toggle going on in the game already that states a bool (crazy) is going to be on or off
where in the PlayerMovement code, do you think you need to toggle it on and off?
Basically where do you want it to happen at?
nowhere?
Oh ok let me ask a question i guess
So you want this stuff to happen when you click i right
exactly
Ok, so in Update you can add the logic of if insane do this stuff with the movement and if not do regular stuff
yeah
go ahead
You've got ALLLL this code doing stuff in update, and its not all doing one thing
can you explain to me the different parts of the update. What I mean by that is, for example, part of this code in update is handling whether you should jump or not right?
Give me a short list of some things that are being done in Update. Not every single little thing, but just the big ideas
Like Jumping
ok, it sets the variables that will help move the player, it also handles said movement, jumping, and sprinting
ngl im winging it here cause i am confused at the code from the tut
yup
Those are some 'high level' things its doing, thats what I was looking for
gotcha
So what I want you to do is realize that you are reading a ton of....kinda hard to read code cuz you have to look at all of it in update at any one time
makes sense
If you want to read code about jumping, well its in the middle of all this other code right?
It can get overwhelming
yessssss
So what IDE are you in?
too much stimuli
yes
Code?
Ah ok
So ill show you the manual way first
Lets start with the jumping part of the code
You know you can hit control + z to undo anything right?
Dont do it just letting you know
That if you make something dissapear, its ok you can get it back as we go through this
yup
Ok cool
So First thing would be make a new function to put the Jumping part of the code in Update in
okay
this can be a private function too
tada
Beautiful
Now what would be the next step
still pertaining to Jump()
you may have already done this
trying to either access it or toggle it on or off
yup
yes
So the problem of reading is the same
ohhhhhhhhhhhhhhhhhhhhhh
would i move this down to outside of update so i can stop the error which says that movementdirectiony doesnt exist in its current context
Don't focus on those problem just yet
ok
Also ill offer this, I can hop in a call and explain this better than typing, but totally cool if you dont want to do that
I know this can be slow
lemme get a mic brb
k
Ok so call Jump() back from where it was
you put it at top of Update but it wasnt there
tbh i think thats the only time its called 🤣
No what i mean is
The place where the jump code was before you put it inside the function, you need to call the function at the same place
replace it essentiall
Like if the jump code was on line 28 - 32
call Jump() on line 28
You just placed it on the top of the Update()
it wasn't at that spot originally
yay
grab the Sprint section of the code and place it in a function
ok
preferable called.....?
Sprint
Yupp. Function names do very well as verbs
yuppp
what do i do about these? they could make the player sprint OR walk
True. So back to the SRP right. A class should be about one thing preferably. A function should do one thing preferably. Its ok to do one or more related things though in this case
ok
For now Sprint() should suffice
Yupp we will clean those up in a sec
gotcha
Clean up the majority of Update then fix all that stuff
So what does update look like now?
What else can you abstract out of Update. I see a big chunk at the bottom
and the comment kinda gives a good clue as to a good function name
ohg?
yup
What else can be abstracted to a function in Update?
so....You have functions and they do stuff
right
sometimes you need a function that does stuff, and then returns an answer/result/calculation
Like its not just doing something, you actually need the result of what has been done
oh i think i know what you mean
does that make sense
like the stuff in the ()'s at the end of void blahblahblah
No those are parameters and we will get to those ina little bit
So make the funciton
and we will go from there
already did
ok show it to me
the recalculateaxes
Yupp
it sets the vector3s to there respective directions
oh recalculates the axes for the vector3's
Yupp it does a calculation
and why do we do calculations?
1 + 1 = ?
What are we trying to get?
yupp 2 is the result. Its what we want right
yup
ok
You can change the Type of the function by replacing void
so what is this function going to return to us
the axes
And what Type are axes.
vector3s
ok
one sec gotta phone call
k
Ok back srry
Also I think I was a tiny bit mistaken, it can be void srry, Its a good thing to know that functions can return results but I dont think we need that on this
So change back to void
my bad
and replace function where the code was in Update
ok
Now, do you think you still need the comments above some of these?
Or does the name itself explain the same thing the comments were doing
ok well I think thats good enough for now , Now remove unneeded lines and clean up the comments and new pic pls
Cool
Ok so idk if you are asking yourself why we are doing all this
but now when you come into this class
can you see how reading whats going on is any easier now?
definitely, way more readable
Cool. Not only that but imagine, your jump isnt working
Now, because you've named the Class well, PlayerMovement, you go into it because junmping is....a form of movement. And now you have a function called Jump(), where it would be a damn good first place to look at that code to see potentially where the bug arises
exactly
yup
scope is like the bigness of something, the height of how hard it may be
or grand
like how big the ambition for the game is. an example of big scope would be: I wanna make a game that simulates real life and has real life graphics now this a stupidly big example but you get the point
So thats is for games. I will explain scope to you for programming. Not similar at all
oh
Class Player : Monobehaviour {
Vector3 forward;
void Shoot() {
//shoot code
}
void Jump() {
//jump code
}
void Move() {
Vector3 forward = transform.transformDirection(blah blah);
//move code
}
}
You've got a class and some functions right
yup
I will edit as we go through
If you create a variable inside of a function
it can be only be used, or accessed in other words, by that function
So does Jump() have access to forward?
no
Cool
Its of local scope to the function Move()
But, if we create forward
up at the top at a class level scope
ohhhh
Now all the functions inside the class can use it and its data.
wait so do i declare them at the top instead of in update
Only if you need to.
Thats the key
And to figure out whether you need to or not
You ask yourself, well, how many functions need this?, on other words, does more than one need it right now?
And right now, how many do?
are you asking me?
yupp
Gimme a whole new website script
So real quick, is that actually the code that moves stuff?
probably
and i think i should move this into move too, as it has to do with movemetn
nooo
i shoulkd
Dont move that in just yet
Because by clumping these things together you may make something out of order to do with this tutorial you are following
So sometimes its not need to make every single thing a function
You cleaned up alot its ok if some little statements for now are still in update
ok
So think to yourself, what variables need to be shared between some functions right now.
Start with the moveDirection =
line
RecalculateAxes. so i would put it all the way at the top so it shares to everyone
Well you would create the variables that need to be shared at the top
and then instead of creating them in the functions, you would just assign them
wdym
If something has a Type in front of it
Vector3 forward = blah blah blah //this is being created AND defined at the same time
forward = blah blah blah // this is being defined only
So you create what you need to create at the top and then define it only in the variables
does that make sense
Yupp
your basically seperating the creation and definition of them into 2 parts so that everything can know about them
what do i do about this because its the opposite
because in this case this is what needs variables. and the variables are in a function, but when i put in the code into the function, it freaks out
Can you screenshot what part you are talking about
So are you talking about the forward for instance at the top?
Also show line numbers and then you can tell me what line you are talking about
Yupp so lets walk through this, look at the ReCalculateAxes()
ok
you are both creating and defining all in one
yup
Which means only RecalculateAxes knows about em
but you can see something else needs to know about them as well
so what to do?
create it at the top
create it and only create it
ok
I mean you could define it at the top too if you wanted to with something like
Vector3 forward = Vector3.ZERO
but you dont have to
thats what it does
What what does?
Cool now you've created it at the top
next step is to stop creating it anddefining in the function and
only define it
you dont need to create it twice
you see?
Well you are creating and defining it there
So you'll get this soon. show me the recalculateAxes function one more time
yupp same concept
ok
have a go
fixed it
already ahead, used the same trick to fix the last one 😎
Hows it goin
hey
You just saying hey?
I meant like hows the fixing coming
i wasn't just saying 'sup' 😄
oh, gotcha, i thought you were fixing the other ones
so whats next, any sqigglies?
0
Are is it seemingly working at this point again
Nice
And do you have the same bug as before
can you verify all the code is working the same as before all of this
well we took out the insane stuff so im guessing it is toggling but i cant tell because the thingwas taken out
ohhhh
works just the same
cool.
So toggling is working
now you can write some simple logic to say
if (im insane)
do this stuff
else
be sane
Give me an updated script btw
if you still want help
so I am where you are current
Hey
wassup
i was hoping maybe you could help me figure out how to make the player move itself, cause I think that i might have to switch off the moving being = to the input
but im not sure how to go about that
Ok so you know how to do most of this I think, minus maybe the movement itself.
You need to write some logic in update now saying what happens when insane vs sane
oh gotcha
Write the general structure first. You can use comments as placeholders for the little details
Lemme know if you need a nudge
In the right direction
That's pretty much it you got it
cause from what i learned, with the spr rule, i should perhaps make a seperate function for the out-of-control movement and turning
Yupp and what class should handle all the movement?
see thats where im at a crossroads because one one hand: the insane class handles insane stuff, but at the same time the playermovement class handles movement
So this is a good thought you jad
thx
The insane class only handles whether or not something IS insane.
Not what happen when insanity happens if that makes sense
gotcha
If you put some movement into the insane class you'd have the same problem as yesterday
In that now you've got some other class handling movement and that doesn't make much sense
yeah
thats what that would be
and im thinking that I have to somehow switch it from that to just a codeable action(moving)
Technically that's sprinting not just regular moving no?
I'd honestly comment the sprinting out till I got regular movement working, gotta do one thing at a time and just keep building out
ok
I'm on my phone btw so much harder to type and I don't have your classes in view like yesterday
Did you get the sprint code from a tutorial
For the sake of learning I agree
yeah
I bet you could make your own, simpler sprint from scratch
yeah
It'd be really simple once you got some basic movement up and running
lemme see what i can cook up
do you think i could be on the right track? im just scratching my brain for some kind of variable that would be needed to be set to each input axis