A little change to avoid the problem that the player stops moving too far away from the solid object: //add a RigidBody 2D component to the player and change code of targetPos to Vector3 targetPos = transform.position; targetPos.x += input.x * moveSpeed * Time.deltaTime; targetPos.y += input.y * moveSpeed * Time.deltaTime; //replace Vector3.MoveTowards() by playerRigidBody.MovePosition(finPos); Hope it is helpful!
Hey how can i add a Rigidbody to the skript ? I add the rb2d to the skript but it says to me playerRigibody is missing idk this skript not know what this is how i fix it
I just started learning Unity. Really appreciate the spontaneous reactions you put in this video, they are giving great credibility to beginners, as they are close to our reactions. :)
When I go to layers there arent any? I can only sort through the hierarchy. I tried making the layers i see on your screen and named them the same Play and Background but idk how to associate them with the actual player and background EDIT I found if you click on the object in hierarchy you can scroll down on inspector to additional settings and associate them into the sorting layer from there. incase anybody else got confused here
I really like your tutorial, but there is a much easier way to do this: Instead of coding the logic with the overlap here, just add a "Box Collider 2D" to the Player! (And under Rigidbody 2D->Constraints set Freeze Rotaion
Just a small note for people using urp. When adding the tilemaps to a sortiing layer they became black. Turns out it had to do with the lit-material that was automatically assigned to the tilemap. When using Sprite-Unlit-Default instead of Lit-default the tilemap reappeared again.
I had an issue with player would go through the objects as if the script wasn't working. Incase anyone has the same issue, my solution was Hierarchy>Player>Inspector(on the right side)> under player controller script in solid objects layer I put "Default". Hope that helps anyone who needs it
Following along in 2024, running into the same problem with the character stopping too far from the statue, but only when approaching from below. When approaching from above the player can actually pass partially beneath the statue, which looks great. I've tried some of the solutions here, but altering the targetPos targets away from the inputs values (always being a hard 1 or 0) kills the movement end position always aligning to the grid, which I would rather keep since that's how all the old-school NES/SNES RPGs did it. I can't seem to see what I missed in the video that allowed his collisions to be close to perfect, as they are. I have a sneaking suspicion that it is related to the pivot point on the player sprite.
Well, I was wrong. The exact thing I'm trying to save (moving in full-unit increments) is what was causing the issue. In the inspector, the player character's starting transform wants to be at a half-unit position. For instance x and y both at 1.5. This will allow the player movement to come right up to the statue as intended. If x and/or y are at an integer value (aligned perfectly to the grid) the player will stop an extra unit's length away from the statue.
hello, thank you for the tutorials 😸 I got a problem with the collision area, everytime the player is about to overlap the object he can't keep walking, but it's too far from the object. I tried to decrease the radius for the overlaping, but the collision shape just don't look right too
It has something to do with the Player's transform. If you have Player inspector opened when you start the game, and move them, you'll notice that for every one step you take in any direction, the X/Y transform only changes by 1, instead of a smaller floating point value. Don't have a solution, just throwing out what's causing it!
@@DavidSmith-wn5gw I guess we could check the isWalkable inside the corouting, and yield break if is not walkable. Note we should check with the result returned by MoveTowards
wait what? do you always program it that way, or was it just for the purpose of the tutorial? i would just use rigidbody velocity to move the player instead of moving the players transform (vector3.movetowards) itself. therefore you don't need any extra code. unity's physics engine does the collision for you. if you move towards a collider with rigidbody.velocity it stops automatically. no need to programm sth thats already in the engine.
late but can someone help me, when i click edit layer and there is no background layer or player layer in sorting layer. Already try gg help but nothing.
could u show the art progress pls (when I draw map some of leftovers of map assest(there are some blue color on the picture when I attact the pic in to the map the blue part are also in the map)) I think it is about over overwrite pic on another pic)
I would like to ask i us VS as well but mine doesnt show me what commands and all the variables does if i move cursor up on em so im jsut curious if u use extension for it or something
When I put the angel in the SolidObjects layer, it goes under the background. The angel only shows when I have the layer as anything else. I followed the directions down to the details so I don’t know why this happens or if this is essential to making the collision work properly.
I also have an issue where the player can still walk over the very edge of the angel’s top left wing. The player can’t walk over anything else but there’s also uneven collision. On the left side, there’s more space while on the right side of the angel, there’s less space between the angel and the player.
You don't need an if statement. You already have the bool. private bool IsWalkable(Vector3 targetPos){ return Physics2D.OverlapCircle(targetPos, 0.2f, solidObjectsLayer) == null; } == and != will return a bool
Hey do you know how to fix the skript ? Look all works fine my player can walk he stops in front of the tiles (but) he stops 2 tiles away from the object and behind the object is 2 tiles aswell i cant walk too?? please help me
@@blankderechte5382 if you're still having trouble with that, try this: change the code in this var targetPos = transform.position; targetPos.x += input.x; targetPos.y += input.y; to this Vector3 targetPos = transform.position; targetPos.x += input.x * moveSpeed * Time.deltaTime; targetPos.y += input.y * moveSpeed * Time.deltaTime; I hope this helps :)
Hi there! Thanks for the tutorial, very helpful indeed. I'm having trouble with the OverlapCircle not returning anything and the whole function count as error? is that a visual studio problem?
The two tiles under the angel sprite are mine have collision and I can't figure out what I did wrong. Is there anything I should check to figure out where I went wrong? I have triple checked my code to make sure it matches yours. Is it a bug in Unity?
it seems to me like when simulating the RigidBody2D on the Statue, it made collision all around it. I'm searching on how to fix it. Y Sorting is part of the problem here. the Layering is basically set to where the Player sprite is in the layer underneath this layer. Which means that the Player Sprite will be in back of this Statue (whether you are in front of it or in back of it). Unity views it as though the Player Sprite is underneath this Statue whether it is above the statue or below the statue. No matter the positioning, the Character Sprite will ALWAYS be underneath.
I mean like they stay fixated facing up but the, rotate to the right lets take the roof i copy the roof entirely then i move to the work space, all of the tiles are pointing up like they shoul however instead of 3 by 4 its 4 by 3
been following the whole tutorial and everything works great until now. i get an error saying Physics2D does not contain a definition for OverLapCircle. does anyone know how to fix this?
I have a problem i did all u doing and worked well but in the end when u walk around ur object ur collider is perfect but mine is like this : In front of it perfect collider for fence but behind it 2 Boxes i cant walk why is that ?
It has to do with the width of the character vs the width of the object. If you characters collider is even 1px colliding with the image he will get stuck
1. If you are using Visual Studio Code, don't. Use Visual Studio instead, which is better integrated for Unity (you can also use Rider) 2. In Unity, go to Edit (top left) -> Preferences -> External Tools -> External Script Editor and then select Visual Studio 3. Intellisense should be working correctly afterwards. (intellisense is what color codes the variable correctly) 4. Hit Regenerate project files if necessary (same path as point 2, in external tools)
Dont always say:,, sorry for my bad ... " your series is the best I have ever followed so keep up the good work.
no literally he keeps apologizing but hes doing an awesome job
Definitely this series has potential
I'm sure with the time this will be visited as much it deserves!!
A little change to avoid the problem that the player stops moving too far away from the solid object:
//add a RigidBody 2D component to the player and change code of targetPos to
Vector3 targetPos = transform.position;
targetPos.x += input.x * moveSpeed * Time.deltaTime;
targetPos.y += input.y * moveSpeed * Time.deltaTime;
//replace Vector3.MoveTowards() by
playerRigidBody.MovePosition(finPos);
Hope it is helpful!
Hey how can i add a Rigidbody to the skript ? I add the rb2d to the skript but it says to me playerRigibody is missing idk this skript not know what this is how i fix it
or better can u add the full skript in this comment would be better to copy ^^
@@blankderechte5382 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class player : MonoBehaviour
{
public Vector2 input;
public float moveSpeed;
public Animator animator;
public bool isMoving;
public Rigidbody2D playerRigidBody;
public LayerMask solidObject;
public LayerMask interactableObject;
private void Awake()
{
animator = GetComponent();
playerRigidBody = GetComponent();
}
void Start()
{
}
void FixedUpdate()
{
isMoving = false;
input.x = Input.GetAxisRaw("Horizontal");
input.y = Input.GetAxisRaw("Vertical");
if (input != Vector2.zero)
{
if (input.x != 0)
{
input.y = 0;
}
animator.SetFloat("MoveX", input.x);
animator.SetFloat("MoveY", input.y);
Vector3 finPos = transform.position;
finPos.x += input.x * moveSpeed * Time.deltaTime;
finPos.y += input.y * moveSpeed * Time.deltaTime;
if (isColliding(finPos)) {
StartCoroutine(Move(finPos));
}
}
animator.SetBool("isMoving", isMoving);
}
IEnumerator Move(Vector3 finPos)
{
if ((transform.position - finPos).sqrMagnitude > 0)
{
isMoving = true;
playerRigidBody.MovePosition(finPos);
yield return null;
}
}
private bool isColliding(Vector3 finPos)
{
var trans = new Vector2(finPos.x, finPos.y - 0.5f);
bool solidIscoll = (Physics2D.OverlapCircle(trans, 0.4f, solidObject) == null);
bool interIscoll = (Physics2D.OverlapCircle(trans, 0.4f, interactableObject) == null);
return solidIscoll && interIscoll;
}
}
what's finPos??
Your code works well without RigidBody
I just started learning Unity. Really appreciate the spontaneous reactions you put in this video, they are giving great credibility to beginners, as they are close to our reactions. :)
Using this for college rn you’re a life saver
yeah
When I go to layers there arent any? I can only sort through the hierarchy. I tried making the layers i see on your screen and named them the same Play and Background but idk how to associate them with the actual player and background
EDIT
I found if you click on the object in hierarchy you can scroll down on inspector to additional settings and associate them into the sorting layer from there. incase anybody else got confused here
was confused about this too, thanks for the tip!
Thank you so much!
Thanks
Thanks mate
thanks
this series is seriously the best. It has helped me out a lot
Great tutorial. Keep doing good tutorials and I am pretty sure your channel will grow. Don't get demotivated :-) Look forward for the next tutorial.
Thank you @gamedakos, I won't stop!!!
I really like your tutorial, but there is a much easier way to do this:
Instead of coding the logic with the overlap here, just add a "Box Collider 2D" to the Player! (And under Rigidbody 2D->Constraints set Freeze Rotaion
Just a small note for people using urp. When adding the tilemaps to a sortiing layer they became black. Turns out it had to do with the lit-material that was automatically assigned to the tilemap. When using Sprite-Unlit-Default instead of Lit-default the tilemap reappeared again.
the 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 Animator animator;
public LayerMask solidObjectLayer;
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;
if(isWalkable(targetPos))
StartCoroutine(Move(targetPos));
}
}
animator.SetBool("isMoving", isMoving);
}
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;
}
private bool isWalkable(Vector3 targetPos)
{
if(Physics2D.OverlapCircle(targetPos, 0.2f, solidObjectLayer) != null)
{
return false;
}
return true;
}
}
thank you !
thank you
Thanks for this! I like checking my code with verifiable data instead of scrolling through the video for the right part. Very useful
Your videos are genuinely very useful loved it
Thank you for this tutorial, it was really helpful! I am busy with a Game Jam and this was easy to follow!
I had an issue with player would go through the objects as if the script wasn't working. Incase anyone has the same issue, my solution was Hierarchy>Player>Inspector(on the right side)> under player controller script in solid objects layer I put "Default". Hope that helps anyone who needs it
I am desperately searching for the name of that Visual Studio Code extension that tells you the info and errors. thanks guys
Following along in 2024, running into the same problem with the character stopping too far from the statue, but only when approaching from below. When approaching from above the player can actually pass partially beneath the statue, which looks great.
I've tried some of the solutions here, but altering the targetPos targets away from the inputs values (always being a hard 1 or 0) kills the movement end position always aligning to the grid, which I would rather keep since that's how all the old-school NES/SNES RPGs did it.
I can't seem to see what I missed in the video that allowed his collisions to be close to perfect, as they are.
I have a sneaking suspicion that it is related to the pivot point on the player sprite.
Well, I was wrong. The exact thing I'm trying to save (moving in full-unit increments) is what was causing the issue. In the inspector, the player character's starting transform wants to be at a half-unit position. For instance x and y both at 1.5. This will allow the player movement to come right up to the statue as intended. If x and/or y are at an integer value (aligned perfectly to the grid) the player will stop an extra unit's length away from the statue.
guys someone still here? i need help why in my tilemap collider 2d do not appear "used by composite"?
Why do you need coding part? you can just add box collider to a player. Can someone explain this to me?
hello, thank you for the tutorials 😸
I got a problem with the collision area, everytime the player is about to overlap the object he can't keep walking, but it's too far from the object. I tried to decrease the radius for the overlaping, but the collision shape just don't look right too
Did you find a solution?
@@original.1263 no
It has something to do with the Player's transform. If you have Player inspector opened when you start the game, and move them, you'll notice that for every one step you take in any direction, the X/Y transform only changes by 1, instead of a smaller floating point value. Don't have a solution, just throwing out what's causing it!
I've also run into this problem, and nothing I've tried works. I don't know if it's because I'm using 2020.3.18f1, but it's driving me a bit nuts.
@@DavidSmith-wn5gw I guess we could check the isWalkable inside the corouting, and yield break if is not walkable. Note we should check with the result returned by MoveTowards
you are too good. thank you and i hope you still making tutorials. take my subscribe ❤
wait what? do you always program it that way, or was it just for the purpose of the tutorial? i would just use rigidbody velocity to move the player instead of moving the players transform (vector3.movetowards) itself. therefore you don't need any extra code. unity's physics engine does the collision for you. if you move towards a collider with rigidbody.velocity it stops automatically. no need to programm sth thats already in the engine.
late but can someone help me, when i click edit layer and there is no background layer or player layer in sorting layer. Already try gg help but nothing.
i have the problem that the tiles dont show up when i'm trying to put these int the Solidobject Layer
could u show the art progress pls (when I draw map some of leftovers of map assest(there are some blue color on the picture when I attact the pic in to the map the blue part are also in the map)) I think it is about over overwrite pic on another pic)
I would like to ask i us VS as well but mine doesnt show me what commands and all the variables does if i move cursor up on em so im jsut curious if u use extension for it or something
When I put the angel in the SolidObjects layer, it goes under the background. The angel only shows when I have the layer as anything else. I followed the directions down to the details so I don’t know why this happens or if this is essential to making the collision work properly.
I also have an issue where the player can still walk over the very edge of the angel’s top left wing. The player can’t walk over anything else but there’s also uneven collision. On the left side, there’s more space while on the right side of the angel, there’s less space between the angel and the player.
did u fixed it? If yes, help me pls
Same here
Go to Hierarchy -> SolidObjects -> Tile Palette and select "Default Brush". This options is under sprites
Hi - I don't get a collider green outline. What can I do to fix this? I'm using a tilemap bush on it's own layer exactly as you've set it up.
if u put the object after doing the collider thing just deleted it and put it again, that's how i fixed it
You don't need an if statement. You already have the bool.
private bool IsWalkable(Vector3 targetPos){
return Physics2D.OverlapCircle(targetPos, 0.2f, solidObjectsLayer) == null;
}
== and != will return a bool
Hey do you know how to fix the skript ? Look all works fine my player can walk he stops in front of the tiles (but) he stops 2 tiles away from the object and behind the object is 2 tiles aswell i cant walk too?? please help me
@@blankderechte5382 if you're still having trouble with that, try this:
change the code in this
var targetPos = transform.position;
targetPos.x += input.x;
targetPos.y += input.y;
to this
Vector3 targetPos = transform.position;
targetPos.x += input.x * moveSpeed * Time.deltaTime;
targetPos.y += input.y * moveSpeed * Time.deltaTime;
I hope this helps :)
Ok I fixed everything but my sprites that have some of h the background showing have grey in it :(
What do you mean? If its the same Problem like mine, maybe this will help you: ruclips.net/video/Wf98KrAyB2I/видео.html
Hi there! Thanks for the tutorial, very helpful indeed. I'm having trouble with the OverlapCircle not returning anything and the whole function count as error? is that a visual studio problem?
Fixed It! :D
The two tiles under the angel sprite are mine have collision and I can't figure out what I did wrong. Is there anything I should check to figure out where I went wrong?
I have triple checked my code to make sure it matches yours. Is it a bug in Unity?
i have the same issue! theres collision under it and it seems like theres no collision on the top bit
it seems to me like when simulating the RigidBody2D on the Statue, it made collision all around it. I'm searching on how to fix it. Y Sorting is part of the problem here. the Layering is basically set to where the Player sprite is in the layer underneath this layer. Which means that the Player Sprite will be in back of this Statue (whether you are in front of it or in back of it).
Unity views it as though the Player Sprite is underneath this Statue whether it is above the statue or below the statue. No matter the positioning, the Character Sprite will ALWAYS be underneath.
What worked for me was going back to my sprite sheet and centering the sprites on the sprite editor helped me!
Hey, why is my collision size too big, can anyone help fix it?
Any idea why, when i select more than 1 tile in my tile pallete it turns 90 degrees to the right in the workspace?
I mean like they stay fixated facing up but the, rotate to the right lets take the roof i copy the roof entirely then i move to the work space, all of the tiles are pointing up like they shoul however instead of 3 by 4 its 4 by 3
Basically x = y and y = x
can i ask, why my layer does not have a background and player like in video?
he built it himself
i do everything like video but it not work
been following the whole tutorial and everything works great until now. i get an error saying Physics2D does not contain a definition for OverLapCircle. does anyone know how to fix this?
Try it with the “L” in overlap as a lowercase
@@maxthullen6408 it didnt work, do you got anything else?
help i can't scene transition can you teah about this
it's Great , but i can't make a road , the edge of my road's collider is so big, so i collision with IT in a far Distance. 🌹🌹
I have a problem i did all u doing and worked well but in the end when u walk around ur object ur collider is perfect but mine is like this : In front of it perfect collider for fence but behind it 2 Boxes i cant walk why is that ?
It has to do with the width of the character vs the width of the object. If you characters collider is even 1px colliding with the image he will get stuck
@@epicgdev ok thank you :)
I did the code like you did but my player gets stuck on collisions and makes the screen vibrate, not sure what went wrong
im having that problem rn did u find out how to fix it
same. driving me nuts :(
Change Rigidbody 2D. Rigidbody type: to Static.
I built the rest of my game already and ran into this episode 💀😭
What do I do
Cool
Some of my variables does not color code properly and my tab to quick write code does not pop up, how would you fix this?
Also love the tutorial!
1. If you are using Visual Studio Code, don't. Use Visual Studio instead, which is better integrated for Unity (you can also use Rider)
2. In Unity, go to Edit (top left) -> Preferences -> External Tools -> External Script Editor and then select Visual Studio
3. Intellisense should be working correctly afterwards. (intellisense is what color codes the variable correctly)
4. Hit Regenerate project files if necessary (same path as point 2, in external tools)
hello i have problem can you write your discord and i write my problem pls