#yeah okay

1 messages · Page 1 of 1 (latest)

hoary karma
#

thread

alpine orbit
#

nice

hoary karma
#

What are your new mental roadblocks

alpine orbit
#

fear of asking

hoary karma
#

just ask

#

we're past that point

alpine orbit
#

for help because itll just go into copying code

hoary karma
#

there is no stupid question

alpine orbit
#

okay

#

so wait let me

#

okay so how would i go about stopping the velocity when the key isnt pressed anymore?

hoary karma
#

well

#

the code will do that automatically

alpine orbit
#

it doesnt

hoary karma
#

Show the code you have

alpine orbit
#

it just keeps going

#

currently at x 60

hoary karma
#

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
alpine orbit
hoary karma
#

you should do it no matter what

alpine orbit
#

wait what

hoary karma
#

also you seem to have switched from GetKey to GetKeyDown

#

why did you do that

alpine orbit
#

so the character moves aslong as the key is pressed

#

when its not pressed anymore i want him to stop moving

hoary karma
#

You want GetKey so it's while you hold it

alpine orbit
#

what

#

i thought get key down was for while you hold it

hoary karma
#

no

#

GetKeyDown is only the exact frame when you press it

#

GetKey is every frame while you hold it

alpine orbit
#

ok

#

same behaviour

hoary karma
#

because you didn't do the other thing I said

#

which was to move that line out of the if statement

alpine orbit
#

yeah im confused onw yh not?

#

myrigidbody.velocity right

hoary karma
#

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

alpine orbit
#

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

hoary karma
#

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

alpine orbit
#

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

hoary karma
#

the fact that direction seems greyed out implies to me you're overwriting it later

alpine orbit
# alpine orbit

i put the thing you told me out outside and under the vector2 direction =

hoary karma
#

show the code

#

the full code

#

I don't want to guess at things and confuse you further

alpine orbit
#

everything else is the same

hoary karma
#

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?

alpine orbit
#

yeah

#

lol

#

okay so put it after

hoary karma
#

it doesn't matter if you change direction later - it will be ignored

hoary karma
alpine orbit
#

okay

#

okay it works

alpine orbit
#

current code

#

i added all 4 directions

hoary karma
#

which means you can't combine them

alpine orbit
#

yeah i meant to do that rn

hoary karma
#

ok

alpine orbit
#

just to build up to it

#

how do we accomplish that now

hoary karma
alpine orbit
#

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?

hoary karma
#

indeed

alpine orbit
#

wouldnt that cause conflicts?

#

example

#

well

#

wait would it?

hoary karma
#

What do you mean by conflict

alpine orbit
#

would it keep adding -1 if i kept pressing down?

#

down 0,0 goes into 0,-1, then 0,-2

hoary karma
alpine orbit
#

oh

hoary karma
#

so each frame, it starts at 0

alpine orbit
#

so we keep resetting it?

hoary karma
#

yes

alpine orbit
#

aha

#

i didnt realise that

hoary karma
#

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

alpine orbit
#

i know i can do the += but id rather not rn

hoary karma
#

yea yea

#

ok so is this making sense?

alpine orbit
#

yes

#

very

hoary karma
#

ggreat

#

and it works?

alpine orbit
#

i understand the coordinate system

#

let me check

alpine orbit
hoary karma
#

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.

alpine orbit
#

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

hoary karma
# alpine orbit is how do i get it to face my mouse?

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
alpine orbit
#

lets eat through them one by one

hoary karma
#
  1. is just Input.mousePosition
alpine orbit
#

but where do i put it

hoary karma
#

well, save it in a variable to start with

#
Vector3 mousePos = Input.mousePosition;```
alpine orbit
#

so the name is mousepos ok

hoary karma
#

i just made that up

#

it can be anything

alpine orbit
#

i kept seeing mousepos in the documentation

hoary karma
#

since it's a variable you create

alpine orbit
#

i thought it was something that unity had

hoary karma
#

Input.mousePosition is the thing that is not made up

alpine orbit
#

okay, i put it in the update function right?

#

cause we need it every frame

hoary karma
# alpine orbit

like in this code here we used direction as your variable name, but it could have been jkdjflekwrjwel

alpine orbit
#

yeah yeah

#

ok wait

#

why are we using vector3

hoary karma
#

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

alpine orbit
#

okay

#

so we need it cause the upcoming code needs it

hoary karma
#

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

alpine orbit
#

converted how

hoary karma
#

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

alpine orbit
#

so i dont have to worry about it

hoary karma
#

not right now

alpine orbit
#

okay

hoary karma
#

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

alpine orbit
#

wait wait first

#

okay i wrote the variable

hoary karma
#

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}";```
alpine orbit
#

completely lost me what

#

ok wait

#

vector3 variablename = input.mousePosition;

#

and then were logging the mouse position?

hoary karma
#

yes exactly

#

Debug.Log just lets us print things to the console window. Do you have the console window open in Unity?

alpine orbit
#

yes

#

i learned about the debug thing

#

i know what it is

#

what is the next part of the code tho

hoary karma
#

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

alpine orbit
#

what does $ do

hoary karma
#

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

alpine orbit
#

ah

hoary karma
#

e.g. in the console we will get something like Mouse pos is (4, 5)

#

Try running it to see what happens

alpine orbit
#

like f"blah blah" in python?

hoary karma
#

yes exactly

#
f"Example {myVar}"``` looks like this in python if I recall
#

exactly the same thing as that

alpine orbit
#

yeah i know i studied some python in school

#

thats where i know it from

hoary karma
#

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

alpine orbit
#

lol

hoary karma
#

we had to do something like:

"Example {}" % (myVar)```
#

it was ugly

alpine orbit
#

i can see dear lord

hoary karma
#

anyway back to the task at hand, have you tried running the code?

alpine orbit
#

waiting for unity to do its thing

hoary karma
#

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

alpine orbit
#

yeah

#

im currently on fullhd res

hoary karma
#

ok so when you feel comfortable with this, we can move on to the next bit with the camera

alpine orbit
#

okay yeah

#

i get it

#

so what do we need to conver that into?

#

vector2 coordinates?

hoary karma
#

Well we want coordinates in the game world

alpine orbit
#

vector 3 ig

hoary karma
#

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

alpine orbit
#

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

hoary karma
#

Because the camera is essential to this

alpine orbit
#

yeah but why, what role does it play

hoary karma
#

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

alpine orbit
#

aha okay

hoary karma
#

even without moving the mouse

alpine orbit
#

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

hoary karma
#

yeah... i think that makes sense lol

alpine orbit
#

so if we move the box even if the mouse was in the center before, the new center is what matters

hoary karma
#

yes

alpine orbit
#

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

hoary karma
#

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 😉

alpine orbit
#

so we got flipped timezones

#

its day for you isnt it rn

hoary karma
#

yes

alpine orbit
#

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

hoary karma
#

Well it's your project so I will leave the @ -ing to you

#

I have my own stuff to deal with

alpine orbit
alpine orbit
#

ill probably be open to it around about this time

#

like the last 3-4 hours

#

upto now

alpine orbit
#

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?

alpine orbit
#

@hoary karma ill be able to code tomorrow around this time, works?

alpine orbit
#

@hoary karma i can do today after 7pm CEST, which would be 1pm your time

alpine orbit
#

@hoary karma can you make today?

hoary karma
#

No

alpine orbit
#

okay

alpine orbit
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.

alpine orbit
#

im just trying to understand rotation fully

#

ill wait untill youre free to teach me about them

alpine orbit
#

@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?

alpine orbit
#

the only thing i need is how to transform the coordinates into vector3, the rotation i can handle

alpine orbit
#

@hoary karma different idea

#

current code

#

works almost how i want it to

#

i have an idea

alpine orbit
#

i did it

#

oh cmon

alpine orbit
#

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

alpine orbit
alpine orbit
# alpine orbit

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

alpine orbit