NOOB HERE - Switch Player Object (single) to Object with Multiple Children

Real weird one…
I’m making a 2D thing where you control a shape (starts as a square), that can turn into other shapes.

The idea is that when you slap the spacebar, you turn into the next shape in the queue (square, circle, goo)

I got it working when I change from a square into a ball, and back again.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum Shapes { Square, Circle };

public class PlayerController : MonoBehaviour {

    public float moveSpeed;
    public float jumpForce;

    public GameObject square;
    public GameObject circle;

    private Rigidbody2D myRigidBody;
    private GameObject shape;

    private Collider2D myCollider;

    Shapes playerShape;

    public void SwitchShapeo()
    {
        if (playerShape == Shapes.Square)
        {
            playerShape = Shapes.Circle;
        } else if (playerShape == Shapes.Circle)
        {
            playerShape = Shapes.Square;
        }
    }

    // Start is called before the first frame update
    void Start()
    {
        playerShape = Shapes.Square;
        shape = square;
        myRigidBody = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {

        if (playerShape == Shapes.Square)
        {
            circle.SetActive(false);
            square.SetActive(true);
            shape = square;
            myCollider = transform.Find("SquareShape").GetComponent<BoxCollider2D>();
        }
        else if (playerShape == Shapes.Circle)
        {
            square.SetActive(false);
            circle.SetActive(true);
            shape = circle;
            myCollider = transform.Find("CircleShape").GetComponent<CircleCollider2D>();
        }

        if (Input.GetKeyDown(KeyCode.W))
        {
            myRigidBody.velocity = new Vector2(myRigidBody.velocity.x, jumpForce);
        }
        if (Input.GetKey(KeyCode.D))
        {
            myRigidBody.velocity = new Vector2(moveSpeed, myRigidBody.velocity.y);
        }
        if (Input.GetKey(KeyCode.A))
        {
            myRigidBody.velocity = new Vector2(-moveSpeed, myRigidBody.velocity.y);
        }
        if (Input.GetKeyDown(KeyCode.Space))
        {
            SwitchShapeo();
        }

      
    }
}

I added a goo in, and got it moving how I wanted (using that youtube vid).

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.U2D;

public class softBody : MonoBehaviour
{
    #region Constants
    private const float splineOffset = 0.5f;
    #endregion

    #region Fields
    [SerializeField]
    public SpriteShapeController spriteShape;
    [SerializeField]
    public Transform[] points;
    #endregion

    #region MonoBehaviour Callbacks
    private void Awake()
    {
        UpdateVerticies();
    }
    private void Update()
    {
        UpdateVerticies();
    }
    #endregion

    #region privateMethods
    private void UpdateVerticies()
    {
        for (int i = 0; i < points.Length - 1; i++)
        {
            Vector2 _vertex = points[i].localPosition;

            Vector2 _towardsCentre = (Vector2.zero - _vertex).normalized;

            float _colliderRadius = points[i].gameObject.GetComponent<CircleCollider2D>().radius;
            try
            {
                spriteShape.spline.SetPosition(i, (_vertex - _towardsCentre * _colliderRadius));
            }
            catch
            {
                Debug.Log("Spline points are too close to each other.. recalculate");
                spriteShape.spline.SetPosition(i, (_vertex - _towardsCentre * (_colliderRadius + splineOffset)));
            }

            Vector2 _lt = spriteShape.spline.GetLeftTangent(i);

            Vector2 _newRt = Vector2.Perpendicular(_towardsCentre) * _lt.magnitude;
            Vector2 _newLt = Vector2.zero - (_newRt);

            spriteShape.spline.SetRightTangent(i, _newRt);
            spriteShape.spline.SetLeftTangent(i, _newLt);
        }
    }
    #endregion
}

BUT, when I make the goo the child of the player object, shit goes whack

When I shapeshift into the goo, it comes out in some whacky shape and jitters all over the place, then won’t reactivate any shapes when I try to shapeshift again.

Code when I try to add the goo as a shape:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum Shapes { Square, Circle, Goo };

public class PlayerController : MonoBehaviour {

    public float moveSpeed;
    public float jumpForce;

    public GameObject square;
    public GameObject circle;
    public GameObject goo;

    private Rigidbody2D myRigidBody;
    private GameObject shape;

    private Collider2D myCollider;

    Shapes playerShape;

    public void SwitchShapeo()
    {
        if (playerShape == Shapes.Square)
        {
            playerShape = Shapes.Circle;
        } else if (playerShape == Shapes.Circle)
        {
            playerShape = Shapes.Goo;
        } else if (playerShape == Shapes.Goo)
        {
            playerShape = Shapes.Square;
        }
    }

    // Start is called before the first frame update
    void Start()
    {
        playerShape = Shapes.Square;
        shape = square;
        myRigidBody = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {

        if (playerShape == Shapes.Square)
        {
            goo.SetActive(false);
            circle.SetActive(false);
            square.SetActive(true);
            shape = square;
            myCollider = transform.Find("SquareShape").GetComponent<BoxCollider2D>();
        }
        else if (playerShape == Shapes.Circle)
        {
            goo.SetActive(false);
            square.SetActive(false);
            circle.SetActive(true);
            shape = circle;
            myCollider = transform.Find("CircleShape").GetComponent<CircleCollider2D>();
        }
        else if (playerShape == Shapes.Goo)
        {
            square.SetActive(false);
            circle.SetActive(false);
            goo.SetActive(true);
            shape = goo;
            myCollider = transform.Find("goo").GetComponent<CircleCollider2D>();
        }

        if (Input.GetKeyDown(KeyCode.W))
        {
            myRigidBody.velocity = new Vector2(myRigidBody.velocity.x, jumpForce);
        }
        if (Input.GetKey(KeyCode.D))
        {
            myRigidBody.velocity = new Vector2(moveSpeed, myRigidBody.velocity.y);
        }
        if (Input.GetKey(KeyCode.A))
        {
            myRigidBody.velocity = new Vector2(-moveSpeed, myRigidBody.velocity.y);
        }
        if (Input.GetKeyDown(KeyCode.Space))
        {
            SwitchShapeo();
        }

      
    }
}

Here is a video if it in action

I have a feeling it has to do with vertices.
The square and circle are both just standard shapes, whereas the goo uses 16 vertices and a centrepoint to do its thing, but I don’t know how to relate the goo’s bits and pieces with the orientation/location of the other shapes and vice versa

maybe something to do with the goo’s centrepoint and wherever the centrepoints of the other shapes are??

OK, I think the weird shape comes from it spawning inside the other shapes at the beginning, but the jitters happen regardless of where it spawns.

video here:

DESIRED GOO BEHAVIOUR:
https://www.youtube.com/watch?v=dnw1Y0gvs8g

thanks for any help

8376591–1104270–video_of_jitters.rar (2.38 MB)

I didn’t look at the video, most people here won’t download a random file on the internet. If you think it would help, you could upload it to Youtube and post it here.

I also didn’t take the most thorough look through the code, but I saw this line here, I’m guessing that’s not what you want?

        else if (playerShape == Shapes.Goo)
        {
            square.SetActive(false);
            circle.SetActive(false);
            goo.SetActive(true);
            shape = circle; // <== shouldn't this be goo?
            myCollider = transform.Find("goo").GetComponent<CircleCollider2D>();
        }

thanks. fixed that, but it doesn’t change the result

video added above