Thanks for making these. I've gone through Unity's 3D and general Game Dev Tutorials which have given me such a good foundation and have been seeking tutorials for the 2D side of things & wanted a different style / perspective. I think you present your information well and I really appreciate your inclusion of Assets with the goal of hitting the ground running. Please keep creating educational content, your presentation is calm, informative and easy to follow. I think your mindset about teaching to think and not just providing the code, etc. is important and will really help people learn and create. Looking forward to finishing your series and whatever you have next
You are probably the best teacher in youtube when it comes to unity. You explain the purpose first, and then what each function does. Easy to remember, and gets people to understand the lesson. You also break them down into section. Amazing work!
I loved your explanations on each line of code and it really helped. I started putting comments on them in my own so I refer back and hopefully learn! Thank you for this!
10 years working with Unity and it's the first time I see this approach to player movement outside of a "point and click" situation 😂 Maybe not the best solution for newbies, but nice and smooth "tiled" movement! 😉
This video is really helpful for me, because i am a vocational high school students majoring in game development. Thank you so much for making this kind of video, keep doing it and i already subscribed you
For those who are having trouble with movement even though your code was same as he is check the inspector window and check the "tag" if it's on player and when you input the script on the player component just add 1 on movement bellow the "script name"
Im having problems because input is ambiguous reference between unityengine and unityengine.windows Cs0104 error goes away if i type Input and not input
I think the best way to go about it is to learn c# first. I'd love making a game like this, but I'm currently only doing c#. Managed to make a text-based RPG console app with a leveling system and currently building a console-based library management system lol. I know its simple but I feel proud of it :D I am very much enjoying coding tbh. It's surprisingly creative in that you're actively building stuff, but its also abstract and requires problem-solving which I enjoy. My area of expertise is visual arts, so coding is a really nice supplement to that and game dev is a great way of bringing that together. I'm looking forward to learning more c# and getting more intuitive with it. I''ll come back to these lessons when ready. I think your teaching style is great! :)
If someone wants a "simpler", more elegant solution, it also stores the facing direction in case someone wants to animate the character accordingly: public class PlayerController : MonoBehaviour { public float moveSpeed = 5f; public Vector2 facingDirection { get; private set; } private void Update() { HandleInputs(); } private void HandleInputs() { Vector2 direction = new Vector2( Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical") ); direction = direction.normalized * moveSpeed * Time.deltaTime; transform.position += new Vector3(direction.x, direction.y, 0); facingDirection = direction.normalized; } }
Thanks man, works like a charm! how could you restrict movement to horizontal and vertical to avoid diagonal movement? I tried adding the same line shown in the tutorial but it doesn't work. I'd really appreciate it!
Yes! you could change your HandleInputs to private void HandleInputs() { Vector2 direction = new Vector2( Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical") ); if(direction.x != 0 && direction.y != 0) { direction.x = 0; // if you want to prioritize up/down movement, otherwise use .y } direction = direction.normalized * moveSpeed * Time.deltaTime; transform.position += new Vector3(direction.x, direction.y, 0); facingDirection = direction.normalized; } this way, if the player walks e.g. right/up, he will only walk up instead. @@R1project0
@@pwndcappearl4235 This is my code and it seems to animate as it should using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour { public float moveSpeed; public Vector2 facingDirection { get; private set; } private Animator animator; void Awake() { animator = GetComponent(); } private void Update() { HandleInputs(); } private void HandleInputs() { Vector2 direction = new Vector2(Input.GetAxisRaw("Horizontal"),Input.GetAxisRaw("Vertical")); if(direction.x != 0 && direction.y != 0) { direction.y = 0; } animator.SetBool("IsMoving", true); // change "IsMoving" to whatever your equivalent is direction = direction.normalized * moveSpeed * Time.deltaTime; animator.SetFloat("moveX", direction.x); // change "moveX" to whatever your equivalent is animator.SetFloat("MoveY", direction.y); // change MoveY" to whatever your equivalent is transform.position += new Vector3(direction.x, direction.y, 0); facingDirection = direction.normalized; } }
You should be doing movement in FixedUpdate, not Update. Otherwise your movement is going to be dependent on the user's computer power. A very fast computer will make your character move crazy fast. FixedUpdate solves this problem. At this point I would also suggest using the newer Input system, it just makes this stuff a lot easier.
The deltaTime already takes care of that as although a frame update might be called sooner, the deltatime will be less and therefore the movement increment will be the same for any computer speed. if you want to simplify this code, I would get rid of the coroutine ;) `void Update() { input.x = Input.GetAxisRaw("Horizontal"); input.y = Input.GetAxisRaw("Vertical"); var targetPos = transform.position; targetPos.x += input.x; targetPos.y += input.y; transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime); } `
Hey man great video, you are truly a reference! Btw, in case you are interested, I suggest to replace the code: targetPos.x += input.x; targetPos.y += input.y; with this one targetPos += (Vector3) input; Its a one liner and considers the z coordinate of input as 0, which is exactly what we want.
Had to make an adjustment as my player wasn't moving (even though I had the exact game code). This fixed it for me. Added to variables: //set Vector 3 in scope private Vector3 targetPos; and replace your first if statment with this: if (input != Vector2.zero) { // Calculate the target position targetPos = transform.position + new Vector3(input.x, input.y, 0) * moveSpeed * Time.deltaTime; // Start moving towards the target if not already moving if (!isMoving) { StartCoroutine(Move(targetPos)); } }
I tried this code it worked for me: using System.Collections; using System.Collections.Generic; using UnityEngine; public class playerController : MonoBehaviour { public float moveSpeed; // Start is called before the first frame update void Start() {
} // Update is called once per frame void Update() { Vector3 playerInput = new Vector3(Input.GetAxis("Horizontal"),Input.GetAxis("Vertical"),0); transform.position = transform.position + playerInput.normalized*moveSpeed*Time.deltaTime; } } well I'm a noob so I don't know much. But i think it's fine for now
hi, im still learning C# and game dev thingy. and i want to ask, is that playeInput.normalized is to make diagonal movement have equal speed to horizontal and vertical?? thank you
just getting into game development with unity, had followed a tute for just using java. These videos are quite helpful. I would say, being a programmer I can follow you easy enough, but for people who don't know much, a bit more explanation if possible on the programming side of things would be appreciated - unless of course you were wanting us to go to someone else for a more detailed look of things.
This high quality, I've had coding classes, and I'm looking at things I've never even considered. You're not offering a simple movement script here, you're offer THE movement script. Like this is along the lines of what you should do to make it work professionally.
Can Somone help me please I coded all the player movment in VS Code and applied to my player but i dont have an option to set the movment speed within inspector. Ive done multiple tutorials and this always happens to me i am unsure of what to do i cannot go any further I would really appreciate any help
I think you might have solved this already, but maybe for someone else with the same problem. Make sure you it's Input.GetAxisRaw, with capital I, Input refers to the input system from Unity, input with lower i refers to the variable you created with public Vector2 input;
Yep! My character stays under the floor and the solution given in the comments doesn't work. What worked is to put 1 in "Order in Layer" in the Additional Settings of the character. Also it is so small, I had to put x:4 and y:4 in the Scale option. Why is that? I followed all the video :( I know we sliced the background in 32x32 but idk...
For people that the player is not moving be careful of writing IEnumerable insted of IEnumerator in the Move function. This happened to me and I was struggling for like an hour or so
You shouldn't have to worry about your Z position in a 2D game. In a 2D game, the objects are rendered in the order you tell it to render. Unity simplifies this by letting you specify the 'Order in Layer'. Go to 'Additional settings' in your objects and look for 'Sorting Layer' and 'Order in Layer'. Leave all your objects on the default layer and just adjust the 'Order in Layer'. Example: My background and my sprite are both on the default sorting layer. The background's 'Order in Layer' is 0 and the sprite's 'Order in Layer' is 1.
I have a question! I like the addition of not being able to move diagonally, however when I move up and down I can be holding the up arrow key and press the left arrow key to move left for a moment, then release and continue moving up. However if I hold the left arrow key down and try tapping the up or down arrow keys I stay only moving left. How can I fix this so that both directions act the same?
Hey, quick question just want to make sure I download everything right (New to programming completely) Visual Studio comes with a lot of stuff to ask before installing, totaling to about 11.4GB of downloads. Is that the right path? It's a little long list of tools to write - don't want to clutter your comment box. P.S. If you're willing, consider opening a discord for questions, or just to converse. So stoked that you made this tutorial for us and I just want to make sure I follow it properly. Thanks,
Hey, great video. I made everything until the 3rd NPC is created and now I try to understand everything. What is I don’t understand is why a Coreroutine is used and why it should be used? Isn’t it a bit overkill for such an easy game? Or is ist really important for further game development as an armature?
Hey, I love the tutorials! Unfortunately, a little confused -- how am I supposed to actually move my character? -- I understand the code and why you have put each line, but when I run it, my character doesn't move and my assumption is because I haven't correlated any keys actually WITH movement. What keys are supposed to move the character? How am I meant to move it?
i had this issue too, look at the bottom left corner of unity, if it says "input axis Horizontal/Vertical not set up" then just go into your code on visual studio, and make sure that the letters H and V are capitalised in your input.x = Input.GetAxisRaw("Horizontal"); input.y = Input.GetAxisRaw("Vertical");
I had the same issue, until I realized that in the while loop I wrote: targetPos = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime); instead it should be: transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime); I hope this helps.
really helpful docs, but can you show me how to edit assets with nice borders like yours, when I drag over from the tile palette, the border just isn't pretty and there's a bunch of redundant colors
@@anjubatus I'm having the same issue, mine was set to public. I switched it to private and it still won't let me add anything to "Move Speed" in the component script. Edit: I figured out the issue, I forgot some ; when writing the code. As someone who knows nothing about coding, this tutorial goes much too fast and assumes a lot. I really think the code should have just been a copy paste because even after the video is over, writing this code manually taught me nothing thanks to the explanation being vague or 'google this' being the explanation.
The move speed option isn't appearing on Unity and most of the commands aren't highlighting in my script. I'm not sure what I've done wrong, as I followed every step to the point I was copying
Remember to save the code. I was stumped with this issue for a while until i remembered it was because i forgot to save it. Also check for capital letters and making sure everything is timed correctly
When I type Vector2 the text does not turn green, and when I hover the cursor over it, there seems to be locks on the list of vectors. Can someone help with this?
idk why but my character keeps moving upward. I even tried using the code from the video in the comments. I tried a different keyboard and everything. I'm really at a loss here.
The Code Is Malfunctioning. It doesn't recognize Some of the Code. It is showing me the following errors: Assets\Scripts\PlayerController.cs(7,13): error CS0246: The type or namespace name 'Vector2' could not be found (are you missing a using directive or an assembly reference?) Assets\Scripts\PlayerController.cs(28,22): error CS0246: The type or namespace name 'Vector3' could not be found (are you missing a using directive or an assembly reference?) Assets\Scripts\PlayerController.cs(28,5): error CS0246: The type or namespace name 'IEnumerator' could not be found (are you missing a using directive or an assembly reference?) Assets\Scripts\PlayerController.cs(7,13): error CS0246: The type or namespace name 'Vector2' could not be found (are you missing a using directive or an assembly reference?) Can anyone help me?
Couldn't you simply do "if input.x+input.y > 0" for the Move functrion? why bother doing all that math when you already know how much you're transforming the body by (the values of input.x and input.y)
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour { public float moveSpeed; private bool isMoving; private Vector2 input;
i might be dumb but i get about half a second of delay and about half of my key strokes are simply not output through the movement of my character. The code is the exact same as shown.
hi, im trying to import the game character in the game scene. but when i trag it into the scene i am only able to see the border of the character and not the texture itself
my character always appears behind the background, anyone know how to fix this? Edit: so after toggling a bunch of things, i figured out you just need to create a new layer on sorting layers, then put it behind the layer the player is on
A simpler way is in the inspector. Select your player, then in the inspector under Additional Settings there is "Order in Layer" setting. If you change that from 0 to 1, the player layer will be ordered after the background layer and will be visible in the scene.
Capital Vector3. I have problems and what i did was delete the script in the microsoft viz and delete the script on unity. I started over and retyped everything and it works now
I'm a beginner, please send the code here, I wrote the same as the video, but for some reason it didn't work I get an error Assets\Scripts\PlayerController.cs(35,75): error CS1513: } expected
It's likely that you made some kind of mistake. C# is very strict. I'm extremely new to programming, but I THINK that your error means that the problem is on the row 35 and 75? So check those and also check the rows just before them, so 34 and 74.
Hi, I've gone through most of this series of tutorials and so far have found it brilliant so thank you for making them! Is there any way have character movement be less.. "blocky" - my character moves one grid square at a time. Could it be more responsive? I've changed GetAxisRaw to "input.x = Input.GetAxis("Horizontal");", which allows for movement in a range between 0-1, but it still sometimes keeps moving after I release keys. And horizontal movement just doesn't feel right either. Thanks!
The way that the tutorial sets up the movement is to basically calculate a starting position and target position and then in the coroutine we calculate the direction and movement speed towards the target position. This means essentially that we aren't actually moving but we are saying "move in this direction by one unit" and the code executes that to move by one unit. Another way of doing this (a very simple way) is to use the following code private void FixedUpdate() { float x = Input.GetAxisRaw("Horizontal"); float y = Input.GetAxisRaw("Vertical"); input = new Vector2(x, y); transform.Translate(input*Time.deltaTime); } This allows you to move essentially however small a distance you want, not unit by unit
@@epicgdev I think I did, I checked through all of my code. Thank you so much for the reply though, your tutorials are extremely good for learning. I ended up using another video by “BMo” and used the movement system he used. I loved your video on creating backgrounds, I was up until 4 in the morning creating my map :) I also used your method for making animations
for those wondering why their character is stuck under the tile map, when inspecting the character sprite, you must set its order of layer to 1.
thank you
YES thank you
"no one will help you on the internet" my ass
good man
Or in my case change the z value in position until you can see your character
Thanks for making these. I've gone through Unity's 3D and general Game Dev Tutorials which have given me such a good foundation and have been seeking tutorials for the 2D side of things & wanted a different style / perspective. I think you present your information well and I really appreciate your inclusion of Assets with the goal of hitting the ground running.
Please keep creating educational content, your presentation is calm, informative and easy to follow. I think your mindset about teaching to think and not just providing the code, etc. is important and will really help people learn and create.
Looking forward to finishing your series and whatever you have next
Thank you very much! It made my day!
I had to hard-code the move speed to get my player to move: public float moveSpeed = 2;
Full code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed;
private bool isMoving;
private Vector2 input;
private void Update()
{
if (!isMoving)
{
input.x = Input.GetAxisRaw("Horizontal");
input.y = Input.GetAxisRaw("Vertical");
if (input != Vector2.zero)
{
var targetPos = transform.position;
targetPos.x += input.x;
targetPos.y += input.y;
StartCoroutine(Move(targetPos));
}
}
}
IEnumerator Move(Vector3 targetPos)
{
isMoving = true;
while ((targetPos - transform.position).sqrMagnitude > Mathf.Epsilon)
{
transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime);
yield return null;
}
transform.position = targetPos;
isMoving = false;
}
}
E bro nice, you've done a service.
thx because the script was not working for me
ily.
Thx
Thx
You are probably the best teacher in youtube when it comes to unity. You explain the purpose first, and then what each function does. Easy to remember, and gets people to understand the lesson. You also break them down into section. Amazing work!
I loved your explanations on each line of code and it really helped. I started putting comments on them in my own so I refer back and hopefully learn! Thank you for this!
10 years working with Unity and it's the first time I see this approach to player movement outside of a "point and click" situation 😂
Maybe not the best solution for newbies, but nice and smooth "tiled" movement! 😉
I'm a noobies sharing what i learn :D I would love feedback thank you!!!
This video is really helpful for me, because i am a vocational high school students majoring in game development. Thank you so much for making this kind of video, keep doing it and i already subscribed you
For those who are having trouble with movement even though your code was same as he is check the inspector window and check the "tag" if it's on player and when you input the script on the player component just add 1 on movement bellow the "script name"
How, I don't understand? Can you explain a little more?
Hi I'm not sure I follow, my code is exactly the same, I made the tag "Player" and then set movement to 3 and it still doesn't work.
Im having problems because input is ambiguous reference between unityengine and unityengine.windows
Cs0104 error goes away if i type Input and not input
@@jaypalmer0616 He is talking bout the Player Controller (script) you should see the Move Speed and set that to 1
Excellent Tutorial!Thanks from China. I can't find a good tutorial in my country's local social media, finally ,I find this. Very useful!
Hey! Just discovered your videos, Thank you for your work Bro, very cool!
Cheers
I think the best way to go about it is to learn c# first. I'd love making a game like this, but I'm currently only doing c#. Managed to make a text-based RPG console app with a leveling system and currently building a console-based library management system lol. I know its simple but I feel proud of it :D I am very much enjoying coding tbh. It's surprisingly creative in that you're actively building stuff, but its also abstract and requires problem-solving which I enjoy. My area of expertise is visual arts, so coding is a really nice supplement to that and game dev is a great way of bringing that together. I'm looking forward to learning more c# and getting more intuitive with it. I''ll come back to these lessons when ready. I think your teaching style is great! :)
your explanations are really clear and easy to understand, thank you!
You're very welcome!
If someone wants a "simpler", more elegant solution, it also stores the facing direction in case someone wants to animate the character accordingly:
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 5f;
public Vector2 facingDirection { get; private set; }
private void Update() {
HandleInputs();
}
private void HandleInputs() {
Vector2 direction = new Vector2(
Input.GetAxisRaw("Horizontal"),
Input.GetAxisRaw("Vertical")
);
direction = direction.normalized * moveSpeed * Time.deltaTime;
transform.position += new Vector3(direction.x, direction.y, 0);
facingDirection = direction.normalized;
}
}
Thanks man, works like a charm!
how could you restrict movement to horizontal and vertical to avoid diagonal movement?
I tried adding the same line shown in the tutorial but it doesn't work. I'd really appreciate it!
Yes! you could change your HandleInputs to
private void HandleInputs() {
Vector2 direction = new Vector2(
Input.GetAxisRaw("Horizontal"),
Input.GetAxisRaw("Vertical")
);
if(direction.x != 0 && direction.y != 0) {
direction.x = 0; // if you want to prioritize up/down movement, otherwise use .y
}
direction = direction.normalized * moveSpeed * Time.deltaTime;
transform.position += new Vector3(direction.x, direction.y, 0);
facingDirection = direction.normalized;
}
this way, if the player walks e.g. right/up, he will only walk up instead.
@@R1project0
Thanks so much for this!
How would I animate the character sprites though?
@@pwndcappearl4235 This is my code and it seems to animate as it should
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed;
public Vector2 facingDirection { get; private set; }
private Animator animator;
void Awake()
{
animator = GetComponent();
}
private void Update()
{
HandleInputs();
}
private void HandleInputs()
{
Vector2 direction = new Vector2(Input.GetAxisRaw("Horizontal"),Input.GetAxisRaw("Vertical"));
if(direction.x != 0 && direction.y != 0)
{
direction.y = 0;
}
animator.SetBool("IsMoving", true); // change "IsMoving" to whatever your equivalent is
direction = direction.normalized * moveSpeed * Time.deltaTime;
animator.SetFloat("moveX", direction.x); // change "moveX" to whatever your equivalent is
animator.SetFloat("MoveY", direction.y); // change MoveY" to whatever your equivalent is
transform.position += new Vector3(direction.x, direction.y, 0);
facingDirection = direction.normalized;
}
}
Sick dude! Loved the explanations of the code! The math behind it is a little hard for me to grasp but I hope to understand it someday! :D Keep it up!
You should be doing movement in FixedUpdate, not Update. Otherwise your movement is going to be dependent on the user's computer power. A very fast computer will make your character move crazy fast. FixedUpdate solves this problem. At this point I would also suggest using the newer Input system, it just makes this stuff a lot easier.
How would someone go about fixing that. Just switch update with fixed update?
@@SentinalSlice Yup, anything that you don't want tied to the actual frame rate the computer will play at should go in fixed_update.
The deltaTime already takes care of that as although a frame update might be called sooner, the deltatime will be less and therefore the movement increment will be the same for any computer speed. if you want to simplify this code, I would get rid of the coroutine ;) `void Update()
{
input.x = Input.GetAxisRaw("Horizontal");
input.y = Input.GetAxisRaw("Vertical");
var targetPos = transform.position;
targetPos.x += input.x;
targetPos.y += input.y;
transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime);
} `
@@welcomelittlefellow
(RU) все можно сделать еще проще (но я использую физическое движение), например:
(EN) everything can be made even simpler (but I use physical movement), for example:
private const float VSpeed = 250f;
private const float HSpeed = 200f;
private Rigidbody2D _physics;
private void Awake()
{
_physics = GetComponent();
}
private void FixedUpdate()
{
Movement();
}
private void Movement()
{
_physics.velocity = transform.TransformDirection(new Vector2(GetHorizontal(), GetVertical()));
}
private static float GetVertical()
{
return Input.GetAxis("Vertical") * VSpeed * Time.fixedDeltaTime;
}
private static float GetHorizontal()
{
return Input.GetAxis("Horizontal") * HSpeed * Time.fixedDeltaTime;
}
Morrowind taught me this, don't tie functions to framerate
This's amazing and I learned so much, thank you
Hey man great video, you are truly a reference!
Btw, in case you are interested, I suggest to replace the code:
targetPos.x += input.x;
targetPos.y += input.y;
with this one
targetPos += (Vector3) input;
Its a one liner and considers the z coordinate of input as 0, which is exactly what we want.
Good series so far! Nice level-flexible delivery
Very good video, I downloead unity 1h ago, this is the best I could look for, ty man
Nice tutos, thank you much.
In Windows, just Ctrl+Mousewheel to zoom in or out directly in VS (maybe works with cmd on mac).
M. D.
Thank you so much! I will be checking this out :D
Had to make an adjustment as my player wasn't moving (even though I had the exact game code). This fixed it for me.
Added to variables:
//set Vector 3 in scope
private Vector3 targetPos;
and replace your first if statment with this:
if (input != Vector2.zero)
{
// Calculate the target position
targetPos = transform.position + new Vector3(input.x, input.y, 0) * moveSpeed * Time.deltaTime;
// Start moving towards the target if not already moving
if (!isMoving)
{
StartCoroutine(Move(targetPos));
}
}
nice, looking forward for next tutorial
More coming soon! Thank you for the support!
hey @regejs, have you been trying to learn Unity?
Guys i wrote all the codes, but character doesn't move. Can someone help me?
Love this tutorial , thanks for making it simple
I tried this code it worked for me:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerController : MonoBehaviour
{
public float moveSpeed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Vector3 playerInput = new Vector3(Input.GetAxis("Horizontal"),Input.GetAxis("Vertical"),0);
transform.position = transform.position + playerInput.normalized*moveSpeed*Time.deltaTime;
}
}
well I'm a noob so I don't know much. But i think it's fine for now
hi, im still learning C# and game dev thingy. and i want to ask, is that playeInput.normalized is to make diagonal movement have equal speed to horizontal and vertical??
thank you
@@itsbps Yes it'll make diagonal movement the same speed as the vertical and horizontal instead of adding them both for the diagonal movement
Thanks
These are excellent tutorials
just getting into game development with unity, had followed a tute for just using java. These videos are quite helpful. I would say, being a programmer I can follow you easy enough, but for people who don't know much, a bit more explanation if possible on the programming side of things would be appreciated - unless of course you were wanting us to go to someone else for a more detailed look of things.
This high quality, I've had coding classes, and I'm looking at things I've never even considered. You're not offering a simple movement script here, you're offer THE movement script. Like this is along the lines of what you should do to make it work professionally.
mr Epic Dev bro . good shit bro
. best regards VoidKon.
Can Somone help me please I coded all the player movment in VS Code and applied to my player but i dont have an option to set the movment speed within inspector. Ive done multiple tutorials and this always happens to me i am unsure of what to do i cannot go any further I would really appreciate any help
This has happened to me and all you got to do is if it says moveSpeed you have to always cap the s in it
Can someone help me? It says vector2 does not contain a definition for GetAxisRaw
I got same problem
I think you might have solved this already, but maybe for someone else with the same problem. Make sure you it's Input.GetAxisRaw, with capital I, Input refers to the input system from Unity, input with lower i refers to the variable you created with public Vector2 input;
Thats exactly what i needed :) thanx bro
Yep! My character stays under the floor and the solution given in the comments doesn't work. What worked is to put 1 in "Order in Layer" in the Additional Settings of the character. Also it is so small, I had to put x:4 and y:4 in the Scale option. Why is that? I followed all the video :( I know we sliced the background in 32x32 but idk...
we sliced the background 16x16, no? And the character 16x32? Anyway, I hope you found a solution! c:
For people that the player is not moving be careful of writing IEnumerable insted of IEnumerator in the Move function. This happened to me and I was struggling for like an hour or so
omg ty for typing this i literally had the same problem 😭
Damn. Spent an hour wondering why my character was jittering and it was because I was missing a " } "
thank u for created this series
Help my character is behind the background how do i fix this? Edit: i just added it i didn’t script yet
for those who get their character behind the tileset, go to 3d mode and move your character to the front a bit...
You shouldn't have to worry about your Z position in a 2D game.
In a 2D game, the objects are rendered in the order you tell it to render. Unity simplifies this by letting you specify the 'Order in Layer'.
Go to 'Additional settings' in your objects and look for 'Sorting Layer' and 'Order in Layer'.
Leave all your objects on the default layer and just adjust the 'Order in Layer'.
Example: My background and my sprite are both on the default sorting layer. The background's 'Order in Layer' is 0 and the sprite's 'Order in Layer' is 1.
I have a question! I like the addition of not being able to move diagonally, however when I move up and down I can be holding the up arrow key and press the left arrow key to move left for a moment, then release and continue moving up. However if I hold the left arrow key down and try tapping the up or down arrow keys I stay only moving left. How can I fix this so that both directions act the same?
Hey, quick question just want to make sure I download everything right (New to programming completely) Visual Studio comes with a lot of stuff to ask before installing, totaling to about 11.4GB of downloads. Is that the right path? It's a little long list of tools to write - don't want to clutter your comment box.
P.S. If you're willing, consider opening a discord for questions, or just to converse. So stoked that you made this tutorial for us and I just want to make sure I follow it properly.
Thanks,
I need help it says isMoving = false; has I invalid token
Great video, the only thing I wish that was in it was how you got all the autocomplete and other things ect.
when i drag player sprite it just shows as orange outline, doesnt show in game view
Hey, great video. I made everything until the 3rd NPC is created and now I try to understand everything. What is I don’t understand is why a Coreroutine is used and why it should be used? Isn’t it a bit overkill for such an easy game? Or is ist really important for further game development as an armature?
Hey, I love the tutorials! Unfortunately, a little confused -- how am I supposed to actually move my character? -- I understand the code and why you have put each line, but when I run it, my character doesn't move and my assumption is because I haven't correlated any keys actually WITH movement. What keys are supposed to move the character? How am I meant to move it?
i had this issue too, look at the bottom left corner of unity, if it says "input axis Horizontal/Vertical not set up" then just go into your code on visual studio, and make sure that the letters H and V are capitalised in your input.x = Input.GetAxisRaw("Horizontal");
input.y = Input.GetAxisRaw("Vertical");
@Attyelep i am also having the same problem ? did each step correct... do you have any solution?
hopping on the bandwagon here, I can't figure out what is wrong. code is identical, letters capitalized. at a loss
I had the same issue, until I realized that in the while loop I wrote:
targetPos = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime);
instead it should be:
transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime);
I hope this helps.
really helpful docs, but can you show me how to edit assets with nice borders like yours, when I drag over from the tile palette, the border just isn't pretty and there's a bunch of redundant colors
could anyone explain to me why input is delayed( character keeps moving for a little while after i press the button )
+1
@@zeyadhani2091 thank you
25:31 can anybody tell me why do after I put the script onto the player the "move speed" is not there, somebody pls help
did you set your moveSpeed variable public or private?
@@anjubatus I'm having the same issue, mine was set to public. I switched it to private and it still won't let me add anything to "Move Speed" in the component script.
Edit: I figured out the issue, I forgot some ; when writing the code. As someone who knows nothing about coding, this tutorial goes much too fast and assumes a lot. I really think the code should have just been a copy paste because even after the video is over, writing this code manually taught me nothing thanks to the explanation being vague or 'google this' being the explanation.
my character moves very slow and delayed
I don't get how to move the player can anyone tell me , Please qwq
Great Video
how to move?
has anyone wonder how did he managed to get those clouds around the well??? coz its been 20 minutes and I cant figure out how the heck he did that xD
It took me a half hour to spot that transform.position mistake in 22:30
I hope you would have elaborated I had to rewatch that part almost 20 times🤧
Thanks 🎉
Help i cant see the movment speed
The move speed option isn't appearing on Unity and most of the commands aren't highlighting in my script. I'm not sure what I've done wrong, as I followed every step to the point I was copying
Remember to save the code. I was stumped with this issue for a while until i remembered it was because i forgot to save it. Also check for capital letters and making sure everything is timed correctly
Can you tell me what extension are you using? thank you
When I type Vector2 the text does not turn green, and when I hover the cursor over it, there seems to be locks on the list of vectors. Can someone help with this?
thx for player movement
I'm loving the tutorial so far, but while putting in "targetPos" I get an error. My C# dosen't detect "targetPos" Any reason as to why?
Delete the script and retype the entire thing. Make sure to fully delete old c script file. I had problems but this is a great solution
idk why but my character keeps moving upward. I even tried using the code from the video in the comments. I tried a different keyboard and everything. I'm really at a loss here.
Hello, my character is showing only the outline not the entire character. How to fix this?
Hi! been loving your videos so far, but I got a question. How do you make it so VSC shows you what's inside a function like in 22:30?
I need to make a video about that
How did you assign it to the movement keys???
Thank you for the lesson! The player moves with inertia. What causes it and how to get rid of it?
Found. This because "yield return null" allows coroutine to work a part of the next frame. So maybe better "yield return new WaitForEndOfFrame();"
Can I ask for a Playercontroller script?.....I've tried it many times but it always fails😅
I have a problem :( it’s saying input axis vertical not set up in the console when I start it
The Code Is Malfunctioning. It doesn't recognize Some of the Code. It is showing me the following errors:
Assets\Scripts\PlayerController.cs(7,13): error CS0246: The type or namespace name 'Vector2' could not be found (are you missing a using directive or an assembly reference?)
Assets\Scripts\PlayerController.cs(28,22): error CS0246: The type or namespace name 'Vector3' could not be found (are you missing a using directive or an assembly reference?)
Assets\Scripts\PlayerController.cs(28,5): error CS0246: The type or namespace name 'IEnumerator' could not be found (are you missing a using directive or an assembly reference?)
Assets\Scripts\PlayerController.cs(7,13): error CS0246: The type or namespace name 'Vector2' could not be found (are you missing a using directive or an assembly reference?)
Can anyone help me?
Please Help me with this...
I am using Visual studio version 2019.
@@BornToCode75 Hi! Have you added namespace UnityEngine? It looks like VS cann't find the declaration of Vector2 structure.
why is my character moving just left and right?
hey i keep having error CS1061 stating that vector2 does not contain a definition for GetAxisRaw ! can you please explain how i can fix it
thank you
Just delete old script file completely and retype it. I had same problem. Might be a space or a capital letter or noncapital letter
my charakter is walking to the left autom. What do I do?
Do you have a document with the code completed and typed so we can compare
For some reason, it is giving me 3 errors. any fixes?
same gives me an expression term error, a " ; expected error", and a "} expected" error even though i have the same code
nvm forgot a parenthesis for the while statement
Can I somehow "block" the character from moving diagonally?
Couldn't you simply do "if input.x+input.y > 0" for the Move functrion? why bother doing all that math when you already know how much you're transforming the body by (the values of input.x and input.y)
How can I turn this code into a pixel perfect movement?
how do you get the clouds to look so good
nvm it seems to work now but im moving too fast
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed;
private bool isMoving;
private Vector2 input;
private void Update()
{
if (!isMoving)
{
input.x = Input.GetAxisRaw("Horizontal");
input.y = Input.GetAxisRaw("Vertical");
if (input != Vector2.zero)
{
var targetPos = transform.position;
targetPos.x += input.x;
targetPos.y += input.y;
StartCoroutine(Move(targetPos));
}
}
}
IEnumerator Move(Vector3 targetPos)
{
isMoving = true;
while ((targetPos - transform.position).sqrMagnitude > Mathf.Epsilon)
{
transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime);
yield return null;
}
transform.position = targetPos;
isMoving = false;
}
}
and what about i want to create my own character?
Amazing video, i'm quite new in game dev, im taking it as a hobby, but i'm learning a lot thanks to you
make the layer of the player 1 from the inspector
Hopefully it made sense in the next video when I talk about layers !
I need help I can’t find the script in Visual studio
i might be dumb but i get about half a second of delay and about half of my key strokes are simply not output through the movement of my character. The code is the exact same as shown.
It might be the while loop. Try to run it without the while loop in the coroutine. I also have some problems with the loop.
For some reason my input is inversed.
hi, im trying to import the game character in the game scene. but when i trag it into the scene i am only able to see the border of the character and not the texture itself
Click at the character, then at 'order in layer' and put 1 instead of 0.
How did you draw the cloud tiles.after the first video
Hey i have a question from where dis you get this assets and can i use for comercial use can you give me linki to it if you have
my character always appears behind the background, anyone know how to fix this?
Edit: so after toggling a bunch of things, i figured out you just need to create a new layer on sorting layers, then put it behind the layer the player is on
A simpler way is in the inspector. Select your player, then in the inspector under Additional Settings there is "Order in Layer" setting. If you change that from 0 to 1, the player layer will be ordered after the background layer and will be visible in the scene.
The type or namespace name 'vector3' could not be found (are you missing a using directive or an assembly reference?)
please help
Capital Vector3. I have problems and what i did was delete the script in the microsoft viz and delete the script on unity. I started over and retyped everything and it works now
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed;
private bool isMoving;
private Vector2 input;
private Animator animator;
private void Awake()
{
animator = GetComponent();
}
private void Update()
{
if (!isMoving)
{
input.x = Input.GetAxisRaw("Horizontal");
input.y = Input.GetAxisRaw("Vertical");
Debug.Log("This is input.x" + input.x);
Debug.Log("This is input.y" + input.y);
if (input.x != 0) input.y = 0;
if (input != Vector2.zero)
{
animator.SetFloat("moveX", input.x);
animator.SetFloat("moveY", input.y);
var targetPos = transform.position;
targetPos.x += input.x;
targetPos.y += input.y;
StartCoroutine(Move(targetPos));
}
}
}
IEnumerator Move(Vector3 targetPos)
{
isMoving = true;
while ((targetPos - transform.position).sqrMagnitude > Mathf.Epsilon)
{
transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime);
yield return null;
}
transform.position = targetPos;
isMoving = false;
}
}
My code is the same as yours and i did everything as you, but my character isnt moving
It is due to at 23:29 he changes targetPos to transform.position
I'm a beginner, please send the code here, I wrote the same as the video, but for some reason it didn't work I get an error Assets\Scripts\PlayerController.cs(35,75): error CS1513: } expected
It's likely that you made some kind of mistake. C# is very strict. I'm extremely new to programming, but I THINK that your error means that the problem is on the row 35 and 75? So check those and also check the rows just before them, so 34 and 74.
have you fixed it? i got the same problem but on 42,112 and that aren't the code linec beacuse i don't even have that many
is there anyway to polish the movement
Hi, I've gone through most of this series of tutorials and so far have found it brilliant so thank you for making them!
Is there any way have character movement be less.. "blocky" - my character moves one grid square at a time. Could it be more responsive? I've changed GetAxisRaw to "input.x = Input.GetAxis("Horizontal");", which allows for movement in a range between 0-1, but it still sometimes keeps moving after I release keys. And horizontal movement just doesn't feel right either.
Thanks!
The way that the tutorial sets up the movement is to basically calculate a starting position and target position and then in the coroutine we calculate the direction and movement speed towards the target position. This means essentially that we aren't actually moving but we are saying "move in this direction by one unit" and the code executes that to move by one unit.
Another way of doing this (a very simple way) is to use the following code
private void FixedUpdate()
{
float x = Input.GetAxisRaw("Horizontal");
float y = Input.GetAxisRaw("Vertical");
input = new Vector2(x, y);
transform.Translate(input*Time.deltaTime);
}
This allows you to move essentially however small a distance you want, not unit by unit
This code works much better and is easier to understand. How would I go about changing the move speed of this version of the code? @@Tony-cm8lg
@@Tony-cm8lg I've done this but I can't seem to change the movement speed on my Player?? Is there something I'm not seeing?
I think if you multiply by a constant you can control the speed of the movement. For example, input*Time.deltaTime*2 would move faster I think@@miwny
I have a question... Like why my character is going wildly like he move like the flash. Is there any solution?
🤣🤣lmao
25:43 What keyboard key did you press?
hi, the movement of the player is delayed, what should i do?
i have the same problem. did you find a way to fix it?
Hi! Loving the tutorial so far. I have the same code as you, but I can’t move my character. Is there a specific reason for this?
A lot of people said that the issue was with capitalizing correctly the variables. like MoveX
@@epicgdev I think I did, I checked through all of my code. Thank you so much for the reply though, your tutorials are extremely good for learning. I ended up using another video by “BMo” and used the movement system he used. I loved your video on creating backgrounds, I was up until 4 in the morning creating my map :) I also used your method for making animations
For some reason i can't type !