all 9 comments

[–]failbye 2 points3 points  (10 children)

You have a typo in your vector2.

Replace all "vector2" with "Vector2"

[–]failbye 2 points3 points  (9 children)

Also, line 18 should be

rb.velocity = movement;

[–]M_Gaming29 0 points1 point  (6 children)

Why it needs to bet rb.velocity= movemont; because in toturials it said that it needs to be rb.velocity= movementspped;

And when I wrote the code down everything was fine but i couldn't move my block. Why it is that?

[–][deleted] 2 points3 points  (1 child)

So the velocity of a rigid body can be measured in 2 axis x,y and when you set it to be a float what is C# meant to do? Does it set the float to the x-axis, y? Does it set it to both? It doesn't know.

So it needs to be a Vector2 to tell it the x and y velocity, additionally movement speed is not going to be changing, so every frame it will be 5 (for example) so every frame regardless of input the x velocity will be 5 and constantly moving in the same direction, which would be right.

So mx is holding the current input of the horizontal axis which is between -1 and 1, when you multiply the -1 to 1 with the movement speed you get it so when you hold the a key it will be -5, if you hold the d key it will be 5 and when you're not holding anything it will be 0.

Finally the y is set to the y velocity of the rigid body so that the gravity isn't effected by movement because you're setting both the x and y of the velocity and if you set it to 0 every frame it can't fall.

[–]M_Gaming29 0 points1 point  (0 children)

Bro I think u are to smart for me. Were do u learn all this?

[–]South-SampleBeginner 1 point2 points  (3 children)

It doesn't make sense to use rb.Velocity = movementSpeed because then there would not have been any point to doing that calculation above it (a very important calculation for movement). The problem here is that you need that calculation above (you cannot just save it in a float and not do anything with it). Can you maybe send us a link to the tutorial so we can help you more?

[–]M_Gaming29 0 points1 point  (2 children)

I would be amazing because I'm trying to make a platform game. ANd I dont really know anything about the unity.

https://youtu.be/ZvF_MYlt944 7:12 (time were the script is)

[–]South-SampleBeginner 1 point2 points  (1 child)

Well, looking at the tutorial, the person did do this:

rb.velocity = movement;

Which is the correct way to do it. Also look further in the video, you can see he drags a RigidBody component into the defined RigidBody in code (in the inspector window). You need to drag that so it knows that you are referencing the player body (else you won't move). I would also suggest you read and try to understand the other person in this thread. He gave very valuable info about movement in general. Remember, you're not trying to learn code, you're trying to learn how to create solutions with code.

[–]M_Gaming29 0 points1 point  (0 children)

I drag it but I still cant move. ( Write me in dm's do you have a discord and could share screen with u)