最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Unity 2d Platform game - Ladder climbing programming issue - Stack Overflow

programmeradmin4浏览0评论

I am in the process of creating my first game which is a very simple remake of the ZX Spectrum game Fred. I have stumbled on my first issue and it relates to the activation of the Box Collider.

To break this down for you:

Fred can successfully climb up and down ladders. Fred has both a Box Collider 2D and Circle Collider 2D. The Circle Collider 2D encapsulates the overall size of the sprite/character. The Box Collider 2D is the size of a small strip reaching the height of the sprite/character.

See Fred Sprite below:

Fred with Colliders

The Ladder, which is essentially a rope, has a Box Collider 2D which also encapsulates the sprites shape's width and height.

See Rope Sprite below:

Rope with Box Collider

The reason for assigning both a Box Collider 2D and Circle Collider 2D to Fred is because when an enemy attacks, i want the attack to impact the Circle Collider 2D, as this affects the general width of Fred.

The Box Collider 2D is a narrow strip assigned to the center of the character. The reason for this is for when Fred approaches the Rope Sprite, he changes animation from walk to climb. Fred can climb the Rope whether the Box Collider 2D or Circle Collider 2D are activated, however i want the Box Collider 2D to take precedence when Fred approaches and climbs the Rope Sprite.

I am using OnTriggerEnter2D for the character to successfully interact with the rope, See below the code which i have assigned to the Rope Sprite:

`

   private void OnTriggerEnter2D(Collider2D collision)
     {
       if (collision.CompareTag("Ladder"))
        {
        isLadder = true;
        isClimbing = true;
        }
     }

   private void OnTriggerExit2D(Collider2D collision)
     {
       if (collision.CompareTag("Ladder"))
        {
        isLadder = false;
        isClimbing = false;
        }
     }

`

Because i am limited in my knowledge of c# i am trying to find a way to code around the Circle Collider 2D and activate the Box Collider 2D for when the character approaches the rope and climbs either up or down.

I have attempted to apply OnCollisonEnter() however this overrides OnCollisionEnter2D and deactivates Rope Climbing.

I am in the process of creating my first game which is a very simple remake of the ZX Spectrum game Fred. I have stumbled on my first issue and it relates to the activation of the Box Collider.

To break this down for you:

Fred can successfully climb up and down ladders. Fred has both a Box Collider 2D and Circle Collider 2D. The Circle Collider 2D encapsulates the overall size of the sprite/character. The Box Collider 2D is the size of a small strip reaching the height of the sprite/character.

See Fred Sprite below:

Fred with Colliders

The Ladder, which is essentially a rope, has a Box Collider 2D which also encapsulates the sprites shape's width and height.

See Rope Sprite below:

Rope with Box Collider

The reason for assigning both a Box Collider 2D and Circle Collider 2D to Fred is because when an enemy attacks, i want the attack to impact the Circle Collider 2D, as this affects the general width of Fred.

The Box Collider 2D is a narrow strip assigned to the center of the character. The reason for this is for when Fred approaches the Rope Sprite, he changes animation from walk to climb. Fred can climb the Rope whether the Box Collider 2D or Circle Collider 2D are activated, however i want the Box Collider 2D to take precedence when Fred approaches and climbs the Rope Sprite.

I am using OnTriggerEnter2D for the character to successfully interact with the rope, See below the code which i have assigned to the Rope Sprite:

`

   private void OnTriggerEnter2D(Collider2D collision)
     {
       if (collision.CompareTag("Ladder"))
        {
        isLadder = true;
        isClimbing = true;
        }
     }

   private void OnTriggerExit2D(Collider2D collision)
     {
       if (collision.CompareTag("Ladder"))
        {
        isLadder = false;
        isClimbing = false;
        }
     }

`

Because i am limited in my knowledge of c# i am trying to find a way to code around the Circle Collider 2D and activate the Box Collider 2D for when the character approaches the rope and climbs either up or down.

I have attempted to apply OnCollisonEnter() however this overrides OnCollisionEnter2D and deactivates Rope Climbing.

Share Improve this question asked Mar 8 at 4:03 SeanSean 112 bronze badges 1
  • Ignore the circle collider if it encounters a "rope"? – Gerry Schmitz Commented Mar 8 at 17:16
Add a comment  | 

1 Answer 1

Reset to default 0

From what i understand the code you added is written for the player gameobject if so, there are two workarounds you can make, one is to move the on trigger enter and exit to the ladder gameobject and check the collider type using

collision.GetType().ToString();

The other workaround is to create another gameobject under the player gameobject which will only have the box collider and to handle the ontriggerenter/exit there.

There is a way to actually solve your problem written here. How to know which collider triggered the call to OnTriggerEnter in Unity (on a gameobject with multiple colliders)

However i think this is a bit much compared to the other easier solutions.

发布评论

评论列表(0)

  1. 暂无评论