How to create piston-like object which change scale over time and are connected to other objects with Joints?
Hello, so I have GameObjects which look something like this. (consisting of primitive spheres and cylinders)
I want the cylinders, which connect the spheres, to change in Y-Scale over time. in order to make it look like the cylinders is some kind of pistons (so the cylinders should continuously grow and then shrink again). The top and bottom side of the cylinders should never be visible and always hidden inside the sphere. So the spheres should move attached to the ends of the cylinder.
I tried to achieve this by writing a simple script that changes the Y-scale over time and connecting the spheres to the cylinders with joints, but the result wasn't satisfying at all. (I tried FixedJoints, CharacterJoints, SpringJoints and HingeJoints, but none worked). Currently in this attempt, I am using FixedJoints, each sphere has a Rigidbody (Use Gravity and Is Kinematic both set to false) and a SphereCollider, and each Cylinder has also a Rigidbody with Use Gravity set to true, and Is Kinematic set to false, but no Collider.
I don't know a lot about joints, I looked at the manual and watched two videos (including the one from Unity itself), but am not really smarter than before. The SpringJoint has Connected Anchor values. One way to solve my problem would probably be to calculate that Anchor every frame anew (would it?), but that seemed like a lot of effort for such a task. Isn't there a more convenient way?
I also thought about hiding a second cylinder with the same dimension as the first one, inside the first one, and only change the position of this second cylinder, but that also seemed like a weird solution. Furthermore, the cylinder might be more than twice as long in its extended state than when contracted.
Thank you for reading my question!
EDIT: I made an animation how I want the result to look, to make it more clear. (Except for the buggy string things on the top right corner of the thing, didn't know how to remove that, sorry)
What's the final purpose of this object? Until now it sounds like you could forget about joints, colliders and everything and just calculate everything yourself. (You have a triangle with three known sides)
I want to create such "creatures" procedurally and let them race against each other. I think I'll use the solution I wrote as an answer. But thanks a lot for responding!
Answer by flohallo · Sep 09, 2016 at 08:13 AM
Alright, so a quick update: I asked in another forum, and the solution I got was to just "fake" the cylinders. So the cylinders have only a mesh renderer, and the spheres are directly connected with each other with spring joints. Seems way more convenient than my proposed way!
Your answer
Follow this Question
Related Questions
How to make gameobject appear in center of camera irrespective of it's side?? 0 Answers
NullReferenceException when attempting to call a GameObject from within PointerEventData 1 Answer
Is it safe to instantiate a GameObject and then change it's components? 1 Answer
how to assign different Gameobject to a single variable during run time using c#? 1 Answer