0

I'm new to Unity and after watching and reading some tutorials I'm now trying to make a simple 2D platformer kind of game. In the said game both enemies and player can jump to different platforms and traverse it like the old SnowBros game.

The problem I'm facing is related to programming the enemy movement. In my game there would be several types of enemies but generally for now there are two types, one that can jump from the platform and one that only walks upto the length of the platform, wait for 1 second then flip and walk backwards; meaning they don't get off the platform. Now this is an issue I'm having trouble with. I can't seem to find a way to calculate the length of the underlying current platform. I thought of using the collider.bound.min.x and max.x to come around the problem but the issue is I can't seem to find a simple way to reach the current platform's collider without fetching the script and then going through it.

Since, there would be many platforms of many different sizes and each platform is made up of a prefab of other platforms, it just doesn't seem like a workable solution to use the platform script and then traverse through it.

Pavel Pája Halbich
  • 1,529
  • 2
  • 17
  • 22
Dashing Boy
  • 477
  • 2
  • 10
  • 21

2 Answers2

0

Well, I hope that you have prefabs of platform and in those prefabs put colliders at start and at end. You can specify layers and tags so that only enemy can detect those colliders and then you can detect tags on collision from enemy script and make your operation.

First create layers by opening Edit -> Project Settings -> Tags and Layers. Create two Layers

Tags and Layers

Create layers for enemy and enemyColliders and Goto Edit -> Project Settings -> Physics2D. Set collision matrix as EnemyCollider will collider with enemy only.

Collision Matrix

And your prefab will be looks like,

Platform

I hope this would help you.

Hamza Hasan
  • 1,368
  • 9
  • 17
  • Thanks for the response. I'm already aware of this solution, in fact I'm using it to solve another problem I have related to platform jumping. This solution is not feasable because of two things first, the length of the final platform could be made up let's say platform prefab 1 + platform prefab 2+....+., and there will be multiple levels and many different platform so I would then have to code seperarately for each platform. – Dashing Boy Dec 29 '15 at 15:30
  • 1
    Thanks for your answer, this is very useful anyway. I also think this is still works with multiplatforms because you can create prefab with params such as length or array of platforms and just add colliders at the end of each side. – Dzianis Yafimau Dec 29 '15 at 15:48
  • It could be then something like keep enable left collider of first platform, disable the second one, and for all the middle platforms disable both colliders and enable right collider of last platform – Hamza Hasan Dec 29 '15 at 16:02
0

You can use Physics2D.Raycast to "sense" for collisions on a Ray. Imagine the enemy putting their toe one step forward, feeling if there is still solid ground, and then deciding whether to stop.

void Update()
{
    Vector2 newPosition = transform.position + velocity * Time.deltaTime;
    Vector2 downwardDirection = Vector2.down;  // you may have to replace this with your downward direction if you have a different one

    RaycastHit2D hit = Physics2D.Raycast(newPosition, downwardDirection);
    if (hit.collider != null)
    {
        // solid ground detected

        transform.position = newPosition;
    }
    else
    {
        // no ground detected. Do something else...
    }
}

You can and should define the Layers your Raycast is supposed to hit, so it can ignore the enemy itself and avoid self-collision.

Thomas Hilbert
  • 3,559
  • 2
  • 13
  • 33
  • Hey thanks, this is something I'm looking forward to. But the problem with this hit.collider will not be null even when the current platform end but below that platform there is another platform. Instead of direction can I just cast ray between two points? Say newPosition and newposition.y - 1 since at that point it will only check collision for the current platform and secondly could I also narrow it down to the respective layer? – Dashing Boy Dec 29 '15 at 15:34
  • The `Raycast` method also takes an optional `length` parameter. With this you can define the maximum distance along the `Ray` to search for collsions. – Thomas Hilbert Dec 29 '15 at 15:47
  • I not recommend to use this method as it is very "hacky" and resource consuming. You better use standard Unity mechanism like colliders and layers. Also consider using Physics2D.RaycastNonAlloc instead of Physics2D.Raycast to improve performance – Dzianis Yafimau Dec 29 '15 at 15:53
  • BTW, if your main question is to determine platform length you can use Renderer.bounds.size property I believe – Dzianis Yafimau Dec 29 '15 at 15:57
  • I would argue that this approach requires fewer colliders, and much more logical ones. One platform only requires one collider for its solid area. No left and right boundary colliders required, who may be problematic where two platforms are very close to each other. Also it is bad design if a platform has to know that there will be enemies who require additional colliders, instead of just providing one collider for itself and letting the enemies worry about their requirements. – Thomas Hilbert Dec 29 '15 at 15:59
  • Thanks Thomas and Denis. Your solution worked. I'm using RaycastNonAlloc. – Dashing Boy Dec 29 '15 at 18:19