20.3 C
New York
Friday, May 16, 2025

Collision Participant and Tilemap Unity


i’m a pupil in austria. I’m engaged on a venture for college, the place we now have to make our personal first recreation with unity. As a result of it’s the first time utilizing unity, i continually run towards issues. Most of them, i mounted already. However this downside acquired me caught for a couple of days now. I’m very sorry, that i used that many photos. Possibly within the image there may be some info i didn’t offer you. The title of the collision map is “FirstLayer_Col”

That is what i’ve acquired thus far. My map is fairly large. I used the tile palette, to color them in. I additionally used various kinds of layers. The bottom layer is usually gras or water. On high of that, i put the bushes on, that are seen within the subsequent image. I created a brand new tilemap to make it a collision tilemap. I took a random tile and put it on all the things i wish to have a collision on.

enter image description here

I modified to order of layer to 10 for the collision tilemap, so i may see the place i’ve to place the tile to cease the participant from, so he cant stroll over or by way of it. After i put the tile on my wished location, i modified the order of layer again to 0 so i couldnt see it anymore. I dont know if that may be a downside or not. I’ve not discovered some other answer that labored for me. The collision tilemap has a tilemap collider and the participant named “South_0” ,due to the sprite i used for it, has a field collider and a rigidbody. Each collider is a 2D one.
enter image description here

After i begin the applying with the collision layer on 10. I can undergo it, stand on the place, the place the collision must be as there isn’t a collision. When i alter the order of layer to 0, so it’s not seen, i can stroll to the tree, however then my recreation begins to shake and the participant rotates. Then there may be some kind of collision, however when i attempt to undergo it, my recreation begins to shake very robust after which my participant can stand in the midst of the tree as there is no collision. It seems to be very “buggy” and the participant rotates in any course. I cannot work out, how i could make the collision to cease the participant from coming into the sector of my tilemap collision with out making the participant go loopy and make the entire display screen shake. I simply need the participant to cease in entrance of it, in order that there isn’t a method of bugging by way of it. I hope i defined my downside so you possibly can perceive it. Possibly it’s only a small, little repair, however i couldn’t discover any answer. The final image is how my participant takes care of i ran up towards the tree with the collision map on layer 0. The participant mustn’t rotate and simply go, up, down, left, proper.
enter image description here

My Code for my Participant:

utilizing UnityEngine;
utilizing System.Collections;

public class PlayerMovement : MonoBehaviour {

    Path currentDir;
    Vector2 enter;
    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 Begin()
    {
        isAllowedToMove = true;
    }

    void Replace () { 

        if(!isMoving && isAllowedToMove)
        {
            enter = new Vector2(Enter.GetAxis("Horizontal"), Enter.GetAxis("Vertical"));
            if (Mathf.Abs(enter.x) > Mathf.Abs(enter.y))
                enter.y = 0;
            else
                enter.x = 0;

            if(enter != Vector2.zero)
            {

                if(enter.x < 0)
                {
                    currentDir = Path.West;
                }
                if(enter.x > 0)
                {
                    currentDir = Path.East;
                }
                if(enter.y < 0)
                {
                    currentDir = Path.South;
                }
                if (enter.y > 0)
                {
                    currentDir = Path.North;
                }

                swap(currentDir)
                {
                    case Path.North:
                        gameObject.GetComponent<SpriteRenderer>().sprite = northSprite;
                        break;
                    case Path.East:
                        gameObject.GetComponent<SpriteRenderer>().sprite = eastSprite;
                        break;
                    case Path.South:
                        gameObject.GetComponent<SpriteRenderer>().sprite = southSprite;
                        break;
                    case Path.West:
                        gameObject.GetComponent<SpriteRenderer>().sprite = westSprite;
                        break;
                }

                StartCoroutine(Transfer(remodel));
            }

        }

    }

    public IEnumerator Transfer(Rework entity)
    {
        isMoving = true;
        startPos = entity.place;
        t = 0f;

        endPos = new Vector3(startPos.x + System.Math.Signal(enter.x), startPos.y + System.Math.Signal(enter.y), startPos.z);

        whereas (t < walkSpeed)
        {
            t += Time.deltaTime * walkSpeed * walkSpeed;
            entity.place = Vector3.Lerp(startPos, endPos, t);
            yield return null;
        };

        isMoving = false;
        yield return 0;
    }
}

enum Path
{
    North,
    East,
    South,
    West
}
```

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles