- Home /
unity2D colliders not working properly,,Unity2D Colliders don't seem to be working HELP PLEASE!
Hi, so me and my friend are beginners to unity and we decided to create a simple 2D game where the players and enemies are sprites formed as cubes(2D) and it is a grid based movement system(so the sprites moves one tile at a time(using tilemaps). we have the same movement script to both the player and the enemy because we want them to move the same at the same time(that to say, if we press down they both go down even if they arent on the same tile) its kind of the movement system in 2048 where all the cubes moves the same direction. well that out of the way we just cant seem to get the colliders to work between the enemies and the player, but it works on walls etc because on the walls we have given them a specific Layer and that works, but if i do the same on the enemis and the player, they dont go into eachother and merge, which we want but then theres another probelm where if they are over and under eachother and you for example move down, the player/enemy at the bottom only goes down and not the on at the top, and as i said we wanted to have them move the same direction and not merge. but if i dont have the layers on the player and enemy this problem don't occur but then instead they merge. we have follow ALOT of tutorials on collision etc, but nothing works. we have box colliders on both objects (IS TRIGGER is off) and we have rigidbodys on both, now idk if i have to code for it to work but almost every tutorial showed that it isn't needed. we have these movepoints attatched to the player and enemy where the movepoint move and the sprites go there.
Here is the movement script attacherd to both the enemy and the player:
public class Movement : MonoBehaviour { public float moveSpeed = 5f; public Transform movePoint;
public LayerMask whatStopsMovement;
public LayerMask FinishPlayer;
public Transform movePoint; public float moveSpeed = 5f;
GameObject CheckPlayerWin;
Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
movePoint.parent = null;
}
void Update()
{
transform.position = Vector3.MoveTowards(transform.position, movePoint.position, moveSpeed * Time.deltaTime);
if(Vector3.Distance(transform.position, movePoint.position) <= .05f)
{
if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) == 1f)
{
if (!Physics2D.OverlapCircle(movePoint.position + new Vector3(Input.GetAxisRaw("Horizontal"), 0f, 0f), .02f, whatStopsMovement))
{
movePoint.position += new Vector3(Input.GetAxisRaw("Horizontal"), 0f, 0f);
}
} else if (Mathf.Abs(Input.GetAxisRaw("Vertical")) == 1f)
{
if (!Physics2D.OverlapCircle(movePoint.position + new Vector3(0f, Input.GetAxisRaw("Vertical"), 0f), .02f, whatStopsMovement))
{
movePoint.position += new Vector3(0f, Input.GetAxisRaw("Vertical"), 0f);
}
}
}
}
}
Thanks for your time and it would be appreciated if we could get help :)
Put in debug logs to find out what is happening, where the code is going.
So when i add in debug logs it prints in the console where the player and enemy moves, wheter it's vertical or horizontal. it obviously prints when i move and they still do when they have merged(which they're not supposed to do in the first place) so that works but i don't know how to keep them from merging, if you need screenshots or something of the settings attached to the player and enemy, tell me.
thanks in advance!