- Home /
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?
https://www.youtube.com/watch?v=CvSu53CjUl8
Watch how the tower blox stack and then begin swaying as the tower becomes taller.
Judging by the action made at 0:39, this is just using "fake" physics. Objects in the stack can be made $$anonymous$$inematic as they land, with the platform below them rotating slightly from side to side (after a certain amount of items have been added).
Thank you for your reply.
I'll send this comment to my programmer and see if he can execute what you've mentioned.
The blocks in question are actually cakes that come in three sizes. They have a box collider only as my engineer said he rather we program the actions ins$$anonymous$$d of using physics. I believe he doesn't use Unity's gravity feature, rather he has some kind of velocity control.
For the size, see the screenshot: http://puu.sh/dXsSP/10ac68c25c.png
In terms of the number of cakes shown, around 10-15, including the one held by the character.
For the camera view, it is similar to Tower Bloxx. Basically my game follows the visual conventions of Tower Bloxx.
Just edited my answer with something that should work. If you need something more like calculating the horizontal offset of the two cakes and checking that against the $$anonymous$$imum you supply (i.e. >50% means it tips over) just take Direction.x and check that against Colider.bounds.extents.x.
Answer by jpthek9 · Dec 26, 2014 at 12:33 AM
Copy paste this script into a C# file named 'SwayScript'. Cleaned it up in Mono so it looks nice and formatted now :)
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;
}
}
}
}
}
So it looks like i have to wait a while before my engineer can even help me with this project, again. Is there a script I can use to get this working? I'd like to try this on a separate computer with Unity installed and see if I can get it working myself.
Cheers
I'll write you up something but I'll need more details. 1. How do the blocks stack (i.e. like in the video with the gravity based drops), 2. How big are the blocks, and 3. How many blocks are you expecting? Oh yeah, and what will the camera view be like (I.e. the top part of the tower only)?
Thanks for providing this. I plugged your code in but the other code that's present within the build is conflicting with this new one. I'm not quite sure how to fix this. I'll send you P$$anonymous$$.
Hmm not sure how to use the message system on Answers. Guess now's a good time as any to learn. One solution that might work is to copy-paste the code into a new script and you just have a seperate one managing sway and attachment.
I did create a separate C# file and pasted your code there. This is the result: http://puu.sh/e5i0R/e97f8414e9.png
This is how I pasted the code into $$anonymous$$ono: http://puu.sh/e5ifX/d7e7c46134.png
put the code inside your $$anonymous$$onoBehaviour class, basically everything except library imports needs to be inside a class of some type.
Answer by gzrjzcx · May 30, 2019 at 06:20 PM
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?