Moving platform with parenting issues

Hi everyone,

This is the first time I write here, and I afraid it´s because I need some support :frowning:

I have the classic moving platform, I have done this before several times without any issue, and that what´s driving me crazy haha:

Player jumps into a moving platform, by some basic circle “raycast” it checks the moving platform and the player object became a “child” of the platform. If the scale of the player is x=1, everythings goes well, but when the player if facing left, and the scale x=-1, if the player object left the platform que x position is multiplied by -1.
So if the transform x position is 16, it will became -16 after unpareting the player object from the platform. The scale will keep the correct value of -1.
So if the player left the platform it’s just teleported to another uncharted place of the scene.

Here’s the paltform code, nothing special, I tried some variants, even moving it using the rigidboy but similar result…

public class MovingPlatform : MonoBehaviour
{

    public Transform target; //posición objetivo
    public float speed; //velocidad de la plataforma

    private Vector3 start, end;

    private void Start()
    {
        if (target != null)
        {
            target.parent = null;
            start = transform.position;
            end = target.transform.position;
        }
    }

    private void FixedUpdate()
    {
        if (target != null)
        {
            float fixedSpeed = speed * Time.deltaTime;
 
            transform.position = Vector3.MoveTowards(transform.position, target.transform.position, fixedSpeed);
        }
        if (transform.position == target.transform.position)
        {
            target.transform.position = start;
        }
        if (transform.position == start)
        {
            target.transform.position = end;
        }
    }


    private void OnDrawGizmos()
        {

            //    Gizmos.DrawLine(transform.position, target.position);
        }
 
}

Then, here are some of the , I suppose, importants lines for the player controller…

    private void CheckSurroundings()
    {

OTHER STUFF (isOnGround etc)

        isOnMovingPlatform = Physics2D.OverlapCircle(transform.position - new Vector3(0, -0.1f, 0), groundCheckRadius, whatIsMovingPlatform);


    
    }

(I have also tried using OnCollisionEnter2D, same result)

    private void IsInMovingPlatform()
    {
 
          if (isOnMovingPlatform)
             {
            Collider2D temp = Physics2D.OverlapCircle(transform.position - new Vector3(0, -0.1f, 0), groundCheckRadius, whatIsMovingPlatform);
 
            gameObject.transform.parent = temp.transform;
                }
            else
            {
            gameObject.transform.SetParent(null);
            DontDestroyOnLoad(gameObject);
             }
}
    private void TurningPlayer()
    {

        facingRight = !facingRight;

        transform.localScale = new Vector3(-1f * transform.localScale.x, 1f, 1f);


        //Dust effect when turning
        TurningEffect();
    }

I just move the player with
myRB.velocity = new Vector2(mov.x * speed, myRB.velocity.y); // Regular player movement
being “mov.x” the classic getAXis…

I dont know what can be affecting the player transform X
Anything I may be missing?

Many thanks in advance.

The usual solution is to not scale the root object of the player but only a child object that is the visible portion of the player.

An alternate way to scaling is to rotate the visible child GameObjects of the player around the Y by 180 degrees, either setting the rotation to Quaternion.identity or else Quaternion.Euler( 0, 180, 0);

Thanks for the answer,

funny is, I get the same behavior if I use “transform.Rotate(0.0f, 180.0f, 0.0f)” to turn the player, I will try with the child object, but that may require a few mods…

Yup, making the sprite and all related animations a child object of the “Player” object did the trick, but I need now to change a few things in my code to make everything work again.

I have seen this solution before, but I was hoping not having to go this way. But anyway, after losing a few days with this stupid issue, I just want to make it right.

Thanks again :slight_smile: