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();
}
}
}