Stacked Objects Swaying: Is there a script for this?

Basically, I would like to create this type of movement with the stacking game I’m creating. Is there a script I can use to pull this off?

Watch how the tower blox stack and then begin swaying as the tower becomes taller.

Copy paste this script into a C# file named ‘SwayScript’. Cleaned it up in Mono so it looks nice and formatted now :slight_smile:

using UnityEngine;
using System.Collections;

public class SwayScript : MonoBehaviour {
	public static int TotalStacked;
	public float SwaySpeed;
	public float MaxAngle; //In degrees
	public int Swayer; //This is for finding the first cake off camera (I.e. if you display 5 cakes, set this to 6)
	int StackPlacement;
	bool RotateRight;
	void FixedUpdate()
	{
		if (StackPlacement == TotalStacked - Swayer)
		{
			if (RotateRight)
			{
				transform.Rotate (Vector3.right * SwaySpeed * Time.fixedDeltaTime);
			}
			else{
				transform.Rotate (-Vector3.right * SwaySpeed * Time.fixedDeltaTime);
			}
			if (Mathf.Abs(transform.eulerAngles.x) > 30)
			{
				RotateRight = !RotateRight;
			}
		}
	}
	bool Sticked;
	void OnTriggerEnter(Collider col)
	{
		if (Collider.gameObject.tag == gameObject.tag)
		{
			if (!Sticked)
			{
				Vector3 Direction = transform.position - Collider.transform.position;
				
				if (Direction.y < (Collider.bounds.extents.y + collider.bounds.extents.y))
				{
					return;
				}
				else{
					transform.parent = Collider.transform;
					Sticked = true;
					TotalStacked++;
					StackPlacement = TotalStacked;
				}
			}
		}
	}
}

Hi,
have u finished this project?
I am making the similar game, and I am also struggling for the swinging effect of stacked pieces.
Do u have any advise?