Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

390
Visualizações
Child Objects Not Following Parent Object On Frequent Teleportation

Game Screen

Teleportation Code

Child Objects And Their Original Location

I'm new to Unity, and so after taking just some regular online courses for Unity2D I wanted to mess around with adding in different features, the first of which I decided to do was something like in Portal, where a projectile spawns two connected portals you can teleport between. However, I've run into an issue. When I'm teleporting my character, sometimes, usually when I'm teleporting too quickly but can happen at any time, the child objects to the Player game object tend to shift, and I don't understand why. **I'd like to:

  1. Know why the offset between the parent and child objects are changing through teleportation.
  2. Know how to fix this issue, preferably in code I can easily understand as a beginner to Unity. Also preferably in a way that doesn't involve me constantly appending the child objects to the parent object through transform position with the added offset, though if it's the simplest solution I'm not against trying it.**

Something worth noting is that the offset change is different as well, I have a Child Object called Feet which detect the Ground for jumping, which seems to remain at the location of the previous portal when it first breaks. However, another child object called Gun which is where the projectiles spawn from seem to only move down a little bit, meaning there's inconsistency in how they are offset when they break. It might be because the Feet has a collider, but I'm unsure, don't know enough, and only felt it was worth mentioning.

[SerializeField] GameObject otherPortal;
    Portal otherPortalComponent;
    BoxCollider2D boxCollider2D;
    bool firstEntered = true;

    // Start is called before the first frame update
    void Start()
    {
        boxCollider2D = GetComponent<BoxCollider2D>();
        otherPortalComponent = otherPortal.GetComponent<Portal>();
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (boxCollider2D.IsTouchingLayers(LayerMask.GetMask("Player")) && firstEntered)
        {
            Teleport(collision.gameObject);
            otherPortalComponent.SetFirstEnteredFalse();
        }
    }

    private void Teleport(GameObject obj)
    {
        obj.transform.position = otherPortal.transform.GetChild(0).transform.position;
    }

    public void SetFirstEnteredFalse()
    {
        this.firstEntered = false;
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        this.firstEntered = true;
    }

To simplify the question, the position of the child objects relative to the parent changes when I instantly change the parents position sometimes, why does this happen and how do I fix the issue without simply using transform.position in an Update method to constantly append the child to the parent, if possible.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I would look at your OnTriggerEnter2D method's if statement for a solution.

private void OnTriggerEnter2D(Collider2D collision)
{
    if (boxCollider2D.IsTouchingLayers(LayerMask.GetMask("Player")) && firstEntered)
    {
        Teleport(collision.gameObject);
        otherPortalComponent.SetFirstEnteredFalse();
    }
}

Because you are using OnTriggerEnter this method will only be called with your feet since your player's main collider isn't a Trigger. This means that your collision variable that you call your Teleport method on is actually your feet object, not your player's body object. So you are changing the offset of your feet from your player in your Teleport method.

I would try changing your collision method to OnCollisionEnter2D(Collision2D) which would pick up your player's base collider when it enters and not the feet collider.

void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.gameObject.GetComponent<Player>() && firstEntered)
    {
        Teleport(collision.gameObject);
        otherPortalComponent.SetFirstEnteredFalse();
    }
}

Since the Player script is on the same object as your base player collider you can do a simple GetComponent<Player>() call to check if its the player object. Though you could still use the Layer to check if you want.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda