i am a student in austria. I am working on a project for school, where we have to make our own first game with unity. Because it is the first time using unity, i constantly run against problems. Most of them, i fixed already. But this problem got me stuck for a few days now. I am very sorry, that i used that many pictures. Maybe in the picture there is some information i did not give you. The name of the collision map is "FirstLayer_Col"
This is what i have got so far. My map is pretty big. I used the tile palette, to paint them in. I also used different types of layers. The ground layer is mostly gras or water. On top of that, i put the trees on, which are seen in the next picture. I created a new tilemap to make it a collision tilemap. I took a random tile and put it on everything i want to have a collision on.
I changed to order of layer to 10 for the collision tilemap, so i could see where i have to put the tile to stop the player from, so he cant walk over or through it. After i put the tile on my wanted location, i changed the order of layer back to 0 so i couldnt see it anymore. I dont know if that is a problem or not. I have not found any other solution that worked for me. The collision tilemap has a tilemap collider and the player named "South_0" ,because of the sprite i used for it, has a box collider and a rigidbody. Every collider is a 2D one.
After i start the application with the collision layer on 10. I can go through it, stand on the place, where the collision should be as there is no collision. When i change the order of layer to 0, so it is not visible, i can walk to the tree, but then my game starts to shake and the player rotates. Then there is some type of collision, but when i try to go through it, my game starts to shake very strong and then my player can stand in the middle of the tree as there is not any collision. It looks very "buggy" and the player rotates in any direction. I can not figure out, how i can make the collision to stop the player from entering the field of my tilemap collision without making the player go crazy and make the whole screen shake. I just want the player to stop in front of it, so that there is no way of bugging through it. I hope i explained my problem so you can understand it. Maybe it is just a small, little fix, but i could not find any solution. The last picture is how my player looks after i ran up against the tree with the collision map on layer 0. The player should not rotate and just go, up, down, left, right.
My Code for my Player:
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
Direction currentDir;
Vector2 input;
bool isMoving = false;
Vector3 startPos;
Vector3 endPos;
public float t;
public Sprite northSprite;
public Sprite eastSprite;
public Sprite southSprite;
public Sprite westSprite;
public float walkSpeed = 0.5f;
public bool isAllowedToMove = true;
void Start()
{
isAllowedToMove = true;
}
void Update () {
if(!isMoving && isAllowedToMove)
{
input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
if (Mathf.Abs(input.x) > Mathf.Abs(input.y))
input.y = 0;
else
input.x = 0;
if(input != Vector2.zero)
{
if(input.x < 0)
{
currentDir = Direction.West;
}
if(input.x > 0)
{
currentDir = Direction.East;
}
if(input.y < 0)
{
currentDir = Direction.South;
}
if (input.y > 0)
{
currentDir = Direction.North;
}
switch(currentDir)
{
case Direction.North:
gameObject.GetComponent<SpriteRenderer>().sprite = northSprite;
break;
case Direction.East:
gameObject.GetComponent<SpriteRenderer>().sprite = eastSprite;
break;
case Direction.South:
gameObject.GetComponent<SpriteRenderer>().sprite = southSprite;
break;
case Direction.West:
gameObject.GetComponent<SpriteRenderer>().sprite = westSprite;
break;
}
StartCoroutine(Move(transform));
}
}
}
public IEnumerator Move(Transform entity)
{
isMoving = true;
startPos = entity.position;
t = 0f;
endPos = new Vector3(startPos.x + System.Math.Sign(input.x), startPos.y + System.Math.Sign(input.y), startPos.z);
while (t < walkSpeed)
{
t += Time.deltaTime * walkSpeed * walkSpeed;
entity.position = Vector3.Lerp(startPos, endPos, t);
yield return null;
};
isMoving = false;
yield return 0;
}
}
enum Direction
{
North,
East,
South,
West
}
```