Welcome to part 2 of the Chain tutorial….Sorry it took so long to release. I switched projects and ended up not needing 3D chains for my own purposes anymore so I am creating this tutorial just to fulfill my promise of there being a pt.2. The project files for this tutorial can be found here: https://spennerino.itch.io/unitychaintutorial
If you haven’t read part 1 it can be found here: Part 1
As mentioned in part 1 I will be covering attaching chains to objects in the world, interacting with them, and applying wind to them.
Attaching Chains To Objects
The chain in part 1 is attached to the world on only one side and the other side is free to dangle. This is fine for some purposes but definitely not for all scenarios.
The other thing you may not want to change is that the first link isn’t static, it is pivoting on the world point.
Fixing this is as simple as setting the Rigidbody on the first link to kinematic or constraining the position and rotation axis based on the result you want to achieve.
Next lets attach the chain to an object on both ends. The reason that the last link in the chain is swinging free is because it is a child to the previous chain. This causes all forces from the other links to be applied to it.
To accomplish what we want we will move the final link up the hierarchy so that it is attached to the world. Then we will need to remove out auto connect script from it and manually link it to the previous link that it was attached to. Running the project now creates a fun bouncing effect where gravity forces the center of the chain downwards but it still remains attached to the blocks on the side.
If you want to adjust the chain to make it start more dangled we can use prefabs to do a little trick.
Adjust the chains end while the game is playing allows us to make edits without breaking the physics. We can save these results to a prefab and then add the prefab to the scene after we stop running the project to save new defaults for the simulation.
Interacting With Chains
You’ll notice that our chains don’t interact with anything else in the world right now except themselves.
This is because the links have no colliders on them which is great if they are just cosmetic but not so great if you want to interact with them.
This causes a weird result because our character joints have the Enable Collision flag enabled.
Enable Collision: When checked, this enables collisions between bodies connected with a joint.
Turning this off gives us the result we are after but the joints will no longer collide with themselves. Due to the chain models and box colliders overlapping with each link to visually connect this is how we have to approach it.
The chains now apply physics in a nice simulation as long as the other objects also have colliders and rigidbodies. Here are some examples.
Character Joint Wind
This section is going to require some new code and is sort of a strange implementation but…hey, it works, and that’s what matters.
Particles are a light weight component that are effected by unity’s wind zones so we can utilize this by applying wind to particle and then applying that force to the chain link rigidbodies. I originally found this code online but unfortunately wasn’t able to find the source to reference it. The magic happens in the fixed update where the chain link matches the velocity of the particle and the particle is locked to the position of the link. The SetupParticleSystem() function optimizes the particle system to only emit a single particle.
public class RigidbodyParticleWind : MonoBehaviour
particlesSystem = gameObject.GetComponent();
particles = new ParticleSystem.Particle;
rigidbody = gameObject.GetComponent();
rigidbody.velocity += particles.velocity;
particles.position = rigidbody.position;
particles.velocity = Vector3.zero;
particlesSystem.startLifetime = Mathf.Infinity;
particlesSystem.startSpeed = 0;
particlesSystem.simulationSpace = ParticleSystemSimulationSpace.World;
particlesSystem.maxParticles = 1;
particlesSystem.emissionRate = 1;
//cant set the following with code so you need to do it manually
//1 - Enable "External Forces"
//2 - Disable "Renderer"
//the below is to start the particle at the center
particles.position = Vector3.zero;
First we need to select all of the chain links that we want to be effected by wind. Second we need to add a particle system and the RigidbodyParticleWind componenet that we created. Third we need to configure the particle system to disable rendering and enable external forces so that we cant see the particles and they are effected by the wind zone we will add later.
Finally we need to add a wind zone object to the scene and play with its properties until we get the results we want.
Hopefully that covers everything!
Follow me on Twitter to get updates on my development projects and new tutorials.
Thanks for reading!