Unity 2D - RPG Tutorial 2024 - Part 04 Collisions

Поделиться
HTML-код
  • Опубликовано: 26 дек 2024

Комментарии • 103

  • @alexanderreiner7694
    @alexanderreiner7694 Год назад +45

    Dont always say:,, sorry for my bad ... " your series is the best I have ever followed so keep up the good work.

    • @d1yapper-z1e
      @d1yapper-z1e 4 месяца назад +3

      no literally he keeps apologizing but hes doing an awesome job

  • @guillermov.2141
    @guillermov.2141 Год назад +16

    Definitely this series has potential
    I'm sure with the time this will be visited as much it deserves!!

  • @jiayudu
    @jiayudu Год назад +17

    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!

    • @blankderechte5382
      @blankderechte5382 Год назад

      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

    • @blankderechte5382
      @blankderechte5382 Год назад

      or better can u add the full skript in this comment would be better to copy ^^

    • @jiayudu
      @jiayudu Год назад

      ​@@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;
      }
      }

    • @FastMagCsgo
      @FastMagCsgo Год назад

      what's finPos??

    • @savasavasavage3953
      @savasavasavage3953 Год назад +2

      Your code works well without RigidBody

  • @XeroVersus
    @XeroVersus Год назад +5

    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. :)

  • @ShipyCreates
    @ShipyCreates Год назад +4

    Using this for college rn you’re a life saver

  • @FilthyNinjaSTEP
    @FilthyNinjaSTEP Год назад +20

    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

  • @anton9583
    @anton9583 9 месяцев назад

    this series is seriously the best. It has helped me out a lot

  • @damdakos
    @damdakos 2 года назад +5

    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.

    • @epicgdev
      @epicgdev  2 года назад +4

      Thank you @gamedakos, I won't stop!!!

  • @MsPopelmann
    @MsPopelmann 11 месяцев назад +10

    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

  • @welcomelittlefellow
    @welcomelittlefellow Год назад +4

    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.

  • @cloud252
    @cloud252 Год назад +23

    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;
    }
    }

    • @reader-7623
      @reader-7623 8 месяцев назад

      thank you !

    • @KhairulFadliajah
      @KhairulFadliajah 6 месяцев назад

      thank you

    • @natdoessomething
      @natdoessomething 5 месяцев назад +1

      Thanks for this! I like checking my code with verifiable data instead of scrolling through the video for the right part. Very useful

  • @satyam6585
    @satyam6585 4 месяца назад

    Your videos are genuinely very useful loved it

  • @carlmeyer96
    @carlmeyer96 11 месяцев назад

    Thank you for this tutorial, it was really helpful! I am busy with a Game Jam and this was easy to follow!

  • @KnowWhatTheyKnow
    @KnowWhatTheyKnow Год назад +1

    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

  • @riccardocorradi5120
    @riccardocorradi5120 10 месяцев назад +2

    I am desperately searching for the name of that Visual Studio Code extension that tells you the info and errors. thanks guys

  • @powerhobo8100
    @powerhobo8100 23 дня назад

    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.

    • @powerhobo8100
      @powerhobo8100 23 дня назад

      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.

  • @la_palma_dei_gamer4346
    @la_palma_dei_gamer4346 15 дней назад

    guys someone still here? i need help why in my tilemap collider 2d do not appear "used by composite"?

  • @daniyarabduldaev9792
    @daniyarabduldaev9792 3 месяца назад

    Why do you need coding part? you can just add box collider to a player. Can someone explain this to me?

  • @alianawakassugui7435
    @alianawakassugui7435 2 года назад +6

    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

    • @original.1263
      @original.1263 Год назад

      Did you find a solution?

    • @alianawakassugui7435
      @alianawakassugui7435 Год назад

      @@original.1263 no

    • @CptJackass325
      @CptJackass325 Год назад

      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
      @DavidSmith-wn5gw Год назад

      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.

    • @yudiren6003
      @yudiren6003 Год назад

      @@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

  • @dorukkarakoc4369
    @dorukkarakoc4369 Год назад

    you are too good. thank you and i hope you still making tutorials. take my subscribe ❤

  • @FortbloxNET
    @FortbloxNET Год назад +2

    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.

  • @minhlengocnguyen6554
    @minhlengocnguyen6554 3 месяца назад

    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.

  • @Gonk-s2o
    @Gonk-s2o 5 месяцев назад

    i have the problem that the tiles dont show up when i'm trying to put these int the Solidobject Layer

  • @baoduc3579
    @baoduc3579 4 месяца назад

    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)

  • @rastamichal8306
    @rastamichal8306 5 месяцев назад

    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

  • @mintea6605
    @mintea6605 Год назад +1

    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.

    • @mintea6605
      @mintea6605 Год назад

      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.

    • @ter_3600
      @ter_3600 11 месяцев назад

      did u fixed it? If yes, help me pls

    • @HeyWTG
      @HeyWTG 9 месяцев назад

      Same here

    • @pawelk3k
      @pawelk3k 2 месяца назад

      Go to Hierarchy -> SolidObjects -> Tile Palette and select "Default Brush". This options is under sprites

  • @Chaoslava
    @Chaoslava Год назад +1

    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.

    • @elperles8440
      @elperles8440 7 месяцев назад

      if u put the object after doing the collider thing just deleted it and put it again, that's how i fixed it

  • @Patrick1985McMahon
    @Patrick1985McMahon Год назад +3

    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

    • @blankderechte5382
      @blankderechte5382 Год назад

      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

    • @tomoyaokazaki2138
      @tomoyaokazaki2138 8 месяцев назад

      @@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 :)

  • @triple3204
    @triple3204 Год назад +1

    Ok I fixed everything but my sprites that have some of h the background showing have grey in it :(

    • @aiirwavesstrong7680
      @aiirwavesstrong7680 Год назад

      What do you mean? If its the same Problem like mine, maybe this will help you: ruclips.net/video/Wf98KrAyB2I/видео.html

  • @TheMostBelu
    @TheMostBelu Год назад +1

    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?

  • @Zanarkand_0
    @Zanarkand_0 Год назад +1

    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?

    • @sensuku
      @sensuku Год назад +1

      i have the same issue! theres collision under it and it seems like theres no collision on the top bit

    • @tomoyaokazaki2138
      @tomoyaokazaki2138 8 месяцев назад +1

      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.

    • @awesomepomegranite
      @awesomepomegranite 6 месяцев назад

      What worked for me was going back to my sprite sheet and centering the sprites on the sprite editor helped me!

  • @mogi_ch
    @mogi_ch Год назад

    Hey, why is my collision size too big, can anyone help fix it?

  • @Brejchadcz
    @Brejchadcz Год назад

    Any idea why, when i select more than 1 tile in my tile pallete it turns 90 degrees to the right in the workspace?

    • @Brejchadcz
      @Brejchadcz Год назад

      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

    • @Brejchadcz
      @Brejchadcz Год назад

      Basically x = y and y = x

  • @farisfauzi4463
    @farisfauzi4463 8 месяцев назад

    can i ask, why my layer does not have a background and player like in video?

  • @HuyenNguyen-fo2rv
    @HuyenNguyen-fo2rv 7 месяцев назад

    i do everything like video but it not work

  • @zygarde.r6461
    @zygarde.r6461 6 месяцев назад

    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?

    • @maxthullen6408
      @maxthullen6408 6 месяцев назад

      Try it with the “L” in overlap as a lowercase

    • @zygarde.r6461
      @zygarde.r6461 6 месяцев назад

      @@maxthullen6408 it didnt work, do you got anything else?

  • @AomSR_SakuFam
    @AomSR_SakuFam Год назад

    help i can't scene transition can you teah about this

  • @shadows_99
    @shadows_99 Год назад +1

    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. 🌹🌹

  • @blankderechte5382
    @blankderechte5382 Год назад

    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 ?

    • @epicgdev
      @epicgdev  Год назад

      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

    • @blankderechte5382
      @blankderechte5382 Год назад

      @@epicgdev ok thank you :)

  • @voidmello
    @voidmello Год назад

    I did the code like you did but my player gets stuck on collisions and makes the screen vibrate, not sure what went wrong

    • @jynsay1220
      @jynsay1220 7 месяцев назад

      im having that problem rn did u find out how to fix it

    • @sagedeleon8396
      @sagedeleon8396 4 месяца назад

      same. driving me nuts :(

    • @faraontv8630
      @faraontv8630 2 месяца назад

      Change Rigidbody 2D. Rigidbody type: to Static.

  • @CK.477
    @CK.477 4 месяца назад

    I built the rest of my game already and ran into this episode 💀😭
    What do I do

  • @bowieziehm6407
    @bowieziehm6407 6 месяцев назад

    Cool

  • @BlahBlah-jn4hj
    @BlahBlah-jn4hj Год назад

    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!

    • @abdiel_hd
      @abdiel_hd Год назад

      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)

  • @SeVii77769
    @SeVii77769 5 месяцев назад

    hello i have problem can you write your discord and i write my problem pls