简体   繁体   English

Unity育儿和育儿游戏对象

[英]Unity parenting and deparenting game objects

I'm currently implementing a wall climbing system into my 2D game and I'm stuck on a few of the trickier parts. 我目前正在2D游戏中实现攀岩系统,但遇到了一些棘手的问题。

My thinking is that I would have my player parent to the wall object when it collides with the 2D collider attached. 我的想法是,当墙对象与附加的2D碰撞器碰撞时,我将使播放器成为父对象。 When the player collides with the wall, the player becomes a child of that wall and is limited to only moving up and down on the wall. 当玩家与墙壁碰撞时,玩家会成为该墙壁的孩子,并且仅限于在墙壁上上下移动。 When the player jumps, or reaches the top, they are no longer a child of the wall. 当玩家跳跃或到达顶部时,他们不再是墙的孩子。 But the player has the ability to jump on any point onto the wall they land on and stays at that point. 但是,玩家可以在任何点上跳到他们所停留的墙壁上并停留在该点上。

Right now I have the parenting part worked out with the following code (this code is attached to the player): 现在,我用以下代码解决了育儿部分(此代码已附加到播放器上):

void OnCollisionEnter2D(Collision2D collision)
{
    if(collision.gameObject.tag == "Wall")
    {
        this.transform.parent = collision.transform;
        Debug.Log("hit a wall");
    }
}

The two areas I'm struggling with are de parenting my player from the wall and having the player still to the position on the wall where they land. 我正在苦苦挣扎的两个领域是将玩家从墙壁上移开,并让玩家仍停留在他们着陆的墙壁上的位置。

With the first part (de-parenting) I believe I'll need to make use of the following code: 相信在第一部分(去父母化)中,我需要使用以下代码:

void OnCollisionExit2D(Collision2D collision)
{

    if(collision.gameObject.tag == null)
    {
         this.transform.parent = null;
        Debug.Log("not hitting anythin");
    }
}

However, when I run this, my player doesn't deparent right away. 但是,当我运行此命令时,我的播放器不会立即消失。 Am I doing it correctly? 我做得对吗?

I'm also clueless as to how to begin my other problem of having the player stick to the part of the wall they connect with. 对于如何开始让玩家坚持与他们相连的那部分墙的其他问题,我也一无所知。 Can someone please help me with my issues? 有人可以帮我解决我的问题吗?

Ideally you'd use tranform.SetParent for the extra you get over position. 理想情况下,您将使用tranform.SetParent获得超出职位的额外收入。

transform.SetParent(parentTransform, true);
transform.SetParent(null, true);

if the latter line is fired it should detach from it's parent to the best of my understanding and the 2nd parameter on SetParent should fix positioning issues. 如果后一行被触发,就我所知,它应该与它的父级分离,并且SetParent上的第二个参数应该可以解决定位问题。

Though your existing code should at least change the hierarchy, I suspect the issue there is your collision code - if you enter a collision with Tag:Wall then you're going to exit a collision with Tag:Wall not Tag:Null. 尽管您现有的代码至少应该更改层次结构,但是我怀疑问题出在您的冲突代码-如果您与Tag:Wall输入冲突,那么您将退出与Tag:Wall而不是Tag:Null的冲突。

However @limoragni is right, editing your hierarchy is going to have no effect on the possible movement of your character unless you've specifically coded that in yourself. 但是@limoragni是正确的,除非您自己专门编写了代码,否则编辑层次结构不会影响角色的可能移动。 You could just as easily set a boolean and use your code with that. 您可以轻松地设置一个布尔值,然后将其用于代码。

Ref: http://docs.unity3d.com/ScriptReference/Transform.SetParent.html 参考: http : //docs.unity3d.com/ScriptReference/Transform.SetParent.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM