Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

389
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!