#yeah okay
1 messages · Page 1 of 1 (latest)
nice
What are your new mental roadblocks
fear of asking
for help because itll just go into copying code
there is no stupid question
okay
so wait let me
okay so how would i go about stopping the velocity when the key isnt pressed anymore?
it doesnt
Show the code you have
Show the code you have
But basically if you do:
Vector2 direction = new Vector2(0, 0);
// a bunch of code that doesn't run
rb.velocity = direction;```
This will of course set velocity to 0
that last line should not be inside the if statement
you should do it no matter what
wait what
so the character moves aslong as the key is pressed
when its not pressed anymore i want him to stop moving
You want GetKey so it's while you hold it
no
GetKeyDown is only the exact frame when you press it
GetKey is every frame while you hold it
because you didn't do the other thing I said
which was to move that line out of the if statement
because we want to set it to zero when you're not pressing anything
if you don't set it to anything, it won't be set
it will continue flying along at whatever you last set it to
because that's how physics works
ok wait im getting it
i was hooking the velocity and the direction to a specific direction
forgetting the direction is a variable i can change
in my head i mean
i thought the direction was for right specifically not whatever i set it to
so the velocity is constant
and the only thing that matters is the direction
if its 0,0 it wont move
you are setting velocity based on your variable called direction
velocity itself, once applied to an object, sticks
until some external thing changes it
such as a force, or colliding with something, or your code
yeah okay
heres what im gonna try next
just for testing purpouses
would this work?
if i go into game mode nothing works anymore lol
I can't say without seeing all the code at once
the fact that direction seems greyed out implies to me you're overwriting it later
i put the thing you told me out outside and under the vector2 direction =
show the code
the full code
I don't want to guess at things and confuse you further
well you're assigning the velocity before any of your code that assigns direction
so that code is all pointless now
See how this will always just give a velocity of 0?
it doesn't matter if you change direction later - it will be ignored
yes you need to assign the velocity after you're done working out the direction
current code
i added all 4 directions
great but now you have the same problem you had originally, which is that you're overwriting the direction entirely inside each if statement
which means you can't combine them
yeah i meant to do that rn
ok
Well think back to my example earlier #💻┃unity-talk message
so i would understand the coordinate system
okay let me copy that
Vector2 direction = new Vector2(0, 0);
// (0, 0) + (1, 0)
direction = direction + new Vector2(1, 0);
// (1, 0) + (0, 1)
direction = direction + new Vector2(0, 1);
okay let me try to understand this
we just add the changes instead of setting it to them?
indeed
What do you mean by conflict
would it keep adding -1 if i kept pressing down?
down 0,0 goes into 0,-1, then 0,-2
no because remember we're doing Vector2 direction = new Vector2(0, 0); at the start of each frame
oh
so each frame, it starts at 0
so we keep resetting it?
yes
in fact we keep recreating the variable from scratch, every time the code runs
it has no memory of its earlier life
since this is in Update, the code runs every frame
flawlessly
One small error you have - that actually isn't causing any harm - is that you still have the myrigidbody.velocity = direction * movestrength; line right near the top of the function. You can delete that, since we're actually setting it at the very end.
okay
so wait let me recap
a vector is a type with a coordinate system and i can use it to make variables
i can just use a variable to set the base position of something and then manipulate it from there instead of adding a velocity in a certain direction
i can just give it velocity and the direction i want it to go in
okay
thanks
i learn from teachers the best ig
my next question
is how do i get it to face my mouse?
vector2.mouse doesnt exist
unless it does but my autocomplete wont show me
It's a little complicated. It's a few lines of code because we need to do a few things:
- Get mouse position, which is in screen space coordinates (unlike your object which lives in the game world)
- Convert the mouse position screen coordinates to a world space coordinate
- Calculate a direction from your object towards the mouse world position
- rotate your object to face that direction
lets eat through them one by one
- is just
Input.mousePosition
but where do i put it
well, save it in a variable to start with
Vector3 mousePos = Input.mousePosition;```
so the name is mousepos ok
i kept seeing mousepos in the documentation
since it's a variable you create
i thought it was something that unity had
Input.mousePosition is the thing that is not made up
like in this code here we used direction as your variable name, but it could have been jkdjflekwrjwel
Well, it's a good question
The simple answer is because "that's what unity made it"
we know it will always be 0 for the z part
But we are going to be using it with functions that take Vector3 arguments in a second so we might as well
really Vector2 would also work just as well
it's not going to make a difference
because Vector2 and Vector3 can be converted automatically in most cases
converted how
Well a Vector2 like (3, 4) will get converted to (3, 4, 0)
If you really must know, someone at Unity wrote a function that converts them, and the C# programming language is smart enough to know when that needs to happen
it happens whenever you use one in the place where another is expected
so i dont have to worry about it
not right now
okay
but anyway - to convert screen space to world space we will need to get our camera involved. Because it depends on where our camera is currently looking
Incredibly, while we were talking, I managed to fix a huge bug in my own code
Ok so just to explain for a second what I mean by "screen space", you should do this:
Vector3 mousePos = Input.mousePosition;
Debug.Log($"Mouse pos is {mousePos}";```
completely lost me what
ok wait
vector3 variablename = input.mousePosition;
and then were logging the mouse position?
yes exactly
Debug.Log just lets us print things to the console window. Do you have the console window open in Unity?
yes
i learned about the debug thing
i know what it is
what is the next part of the code tho
Ok great so - with this code you can run the game and then try moving your mouse to different parts of the screen to see what it prints
what does $ do
That lets us replace part of that string with some data
so instead of {mousePos} it's going to put the actual numbers from that variable
ah
e.g. in the console we will get something like Mouse pos is (4, 5)
Try running it to see what happens
like f"blah blah" in python?
yes exactly
f"Example {myVar}"``` looks like this in python if I recall
exactly the same thing as that
python is confusing for me because at work we were stuck with python2.7 for a really long time, which does not have those nice format strings
lol
i can see dear lord
anyway back to the task at hand, have you tried running the code?
waiting for unity to do its thing
You will see that the numbers we get from Input.mousePosition correspond to the actual pixels on the screen your mouse is at. Not at a position in the game world
ok so when you feel comfortable with this, we can move on to the next bit with the camera
okay yeah
i get it
so what do we need to conver that into?
vector2 coordinates?
Well we want coordinates in the game world
vector 3 ig
because your Transforms for your objects are all in the game world
We can use Vector2
since we are ignoring the z axis for now
and we're working in 2D
So the simplest way to get a reference to the camera is to do:
Camera myCam = Camera.main;```
Note that this works only if your camera has the MainCamera tag (which it should by default)
this just gives us a reference to the Camera as a nice variable we can work with to do the next part
wait lets step back a bit
so we need to transform the pixel coordinates to vector2 or 3, cause z will be 0
why do we need the camera to do that
Because the camera is essential to this
yeah but why, what role does it play
if your camera was off in timbuktu, looking at something in timbuktu, then imagine hovering your mouse over something visible in the game
you would want to get a position from timbuktu back
because that's where your camera is looking
Everything we see on the screen is drawn by the camera
So when we hover our mouse over a spot on the screen - the spot in the game world that corresponds to that is determined by where the camera is looking
The camera is a vitally important part of this whole equation
Seeing if i can find a nice diagram to explain
Let's say your mouse is right in the center of the screen. Then the position we want in the world is the position that is right in the center of the camera's vision.
if we move the camera, that point changes
aha okay
even without moving the mouse
allright
so the camera is important because
its
thee
wait let me wrap my head around this
i get it just let me write it down
the camera is a box, if we put something in the box at a position that position is one thing and if we move it within the box well know where it is relative to what the box sees
if we move the box instead the point changes even if we dont move the object in it, the box matters because thats what we see
and what we see is reality
i guess virtual reality lol
yeah... i think that makes sense lol
so if we move the box even if the mouse was in the center before, the new center is what matters
yes
okay so we need the camera(box) because thats what we see
oaky
okay
i want to keep going but i need to get to bed
whats your timezone
sure
us east
I can't guarantee I will be around or at beck and call at any given time
I do have stuff to do 😉
i am europe
so we got flipped timezones
its day for you isnt it rn
yes
then whenever your online just @ me here
ill tell you if we can work on this
speciically tomorrow im doing something so most likely ill be taking a break from this for a day
Well it's your project so I will leave the @ -ing to you
I have my own stuff to deal with
okay
just want to make sure youre open to that
ill probably be open to it around about this time
like the last 3-4 hours
upto now
when you see this i uh
i researched about rotation and i think i want to try and understand it before we connect the mouse to the object
learn rotation , angles the quarternions and the euler rotations
so i can learn rotation and use it on its own instead of just learning it for this since im gonna need rotation lol
also can i put all the ifs into one if statement?
@hoary karma ill be able to code tomorrow around this time, works?
@hoary karma i can do today after 7pm CEST, which would be 1pm your time
@hoary karma can you make today?
No
okay
this is why i said you @ me instead because im free kinda all day every day but i dont know your schedule @hoary karma
I have a very busy schedule, I will not be able to commit to be available for a specific time. Feel free to post details of whatever you're currently doing and I will help if and when I can.
im just trying to understand rotation fully
ill wait untill youre free to teach me about them
@hoary karma im trying to convert the mouse coordinates to vector3 and going from this code
heres mine
im definitely missing something
why cant i just take the allready found mouse posx and y and just use realmousepos instead of doing it individually?
im guessing vector3 requires 3 arguments?
the only thing i need is how to transform the coordinates into vector3, the rotation i can handle
@hoary karma different idea
current code
works almost how i want it to
i have an idea
done
this is the code
BUT i dont understand it
the only part i understand is the last one where we make it so we transform the rotation on the z axis to angle
i just need a nice explination as to how it works exactly
im actually doing pretty well on my own with just tutorials
but very simple ones
i think i really just needed you to explain to me the very basics of c#
ill post the game progress here and you can comment @hoary karma
this isnt my game idea tho, its just what im using to get familiar with c#
and unity
ill make a few more of these minigames