Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Major Problem with 2D need help?

Discussion in '2D' started by CelticKnight, Aug 17, 2018.

  1. CelticKnight

    CelticKnight

    Joined:
    Jan 12, 2015
    Posts:
    378
    Hi All,

    I started my current 2D project with a YouTube tutorial that is starting to give me problems, at the start I was prepared to live with the problems, but, now ... as the project is starting to grow, not so much. The tutorial was, looking back, pretty terrible, old, and on some sections of the tutorial the code didn't work. Luckily users, in the comments often fixed the code, so, it would work.

    The most pressing problem concerns the main "player" character in that it uses a collider at the player's feet (base) to determine whether or not the player is on the ground. It works well, you are pretty much stuck to the ground when running and after jumping (or ladder or wall climb), you are back to using the collider and stuck to the ground. All good, didn't notice the problems that would cause until I added some colliders for a patrolling enemy moving back and forth, I was getting stuck on the colliders but most pressing problem I also landing on top the enemy and I need to correct that. The thing is I have many things going on in my mind from other aspects of this project, health issues and another issue which is clouding my judgement, that I can't see the method for correcting this issue!

    Another issue is, that early on the player would get caught when running into the side of the wall. The solution used in this tutorial was to put a collider on all walls, floors, that was a physics 2D material with a Friction of zero and a Bounciness of zero. Now, this caused the player to slide endlessly on the floors, so he introduced a faux friction co-efficient so the player will slow down if you let go of the controls, works well ... except. Now remember that enemy I was talking about earlier, well, if you run into the character, whooshka, the baddie slides away from you until he hits a wall. Or if you stop the player you will be pushed slowly backwards which needs to be corrected. As above I can't see the method for solving this problem also.

    So, if anyone is able to solve these issues, or at least give me some pointers, here is a massive "thankyou" in advance.

    Warmest Regards.
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi @CelticKnight

    if you want to benefit from Physics system for character movement, you pretty much have to use colliders, triggers and rigidbodies (where needed).

    Player and environment pretty much have to have at least colliders, and someone has to have Rigidbody, player most likely. You probably don't want to fall through platforms / ground.

    If I understood properly, you want your player to collide with environment, and your enemies collide with environment, but player and enemies don't collide with each other? So that player can't drop down on top of the enemy? This can be solved with Physics collisions matrix, https://docs.unity3d.com/Manual/LayerBasedCollision.html , just disable player and enemy layers collision. You have to of course assign your characters to layers first.

    Walls too have to have colliders, so they will have Physics material... which can have or have not friction. Unless you are doing some tile based system, like tetris or Dungeon Master, where movement is limited my map data. But then you wouldn't need physics system.

    Player-enemy collision. Here too, if you want to use Physics system, then maybe do same as with player, some case specific hack to stop enemy on player collision? On the other hand, if you didn't want your player and enemies colliding, then enemy / player could just have a trigger and kill player on touch?
     
  3. CelticKnight

    CelticKnight

    Joined:
    Jan 12, 2015
    Posts:
    378
    Not exactly, I wan't the player to be able to kill the enemies three different ways, with melee weapon, a projectile based weapon and as a last resort (with no weapons) jumping on the enemies head, the thing is if they miss the exact spot on the enemies head I don't want them landing on the enemies collider and riding around on top of them - a lot weird. I was hoping for an easy solution but it doesn't look like there is one!

    Looks like I will have to hit some more modern tutorials.
     
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @CelticKnight - Melee weapons and projectiles can be checked with collision or triggers. You can stop the enemy displacement (when player collides with enemy) using code or using friction or by modifying rb velocity. Maybe make a bigger trigger over the enemy head, and use it to detect player landing on their head? You could also change player/enemy collider size depending on what he is doing / state: player walking, rolling or falling. It all depends on what exactly you are trying to achieve I guess.
     
  5. CelticKnight

    CelticKnight

    Joined:
    Jan 12, 2015
    Posts:
    378
    Yes, that really does help! Then I modified the enemy colliders to take in the larger collider on the head. After that I decided to make a separate collider for the sword the enemy carries(this might be overkill, so, if I land on it I will take damage and have a bounce-back effect, so, I am forced off the sword instead of riding around on it. Haven't worked on getting the bounce-back work in both directions, but, it shouldn't be too hard. Says the person having problems with the colliders in the first place :p. Thanks for the idea :cool:.
     
    Last edited: Aug 19, 2018
    eses likes this.