Hi there, I’m clueless How I should implement Rope Swinging with a character.
I want to have following movements on a rope:
Going Up/Down a Rope, rotating on a rope and actual swinging on a rope.
I thought of a custom Movement Mode, where I set the Location of the Character to the Location of the Rope.
How Do I implement the actual swinging/rotating/up/down on this rope?Do you think the Cable Component is a good choice for the rope mesh?
You’ll have to use a lot of math for this.
Making the rope swing by the correct amount of force, to the correct direction, will be much easier if you calculate the dot product from the character’s contact point to the rope’s base. Then just apply a force relative to that dot product value, it’s gonna be a very realistic swing simulation, the more the player swings the more impulse he gets.
And yes, the rope component is a good option, but I think it allows attaching actors only to the tip of the cable and not to the middle.
The Cable Component can’t be traced against (Both Channel and Object Traces don’t return any results and ignore the Cable).
So in the Construction Script I add a Sphere Collision for every Cable Particle.
In Tick, I update the Sphere Collision Location to the one of the Cable Particle.
I trace against the Sphere Collision and get the Position.
In my Movement Mode I update the Actor Location to the correct Position.
But I can’t add a force to the Cable. Swinging seems impossible with the Cable
Yeah, you need to use physics constraint components to swing. I made this projectin 4.12, and there have been changes to cable components since, but it seems to work in 4.14. See FirstPersonBP/Blueprints/Rig Constraint, I think it’s somewhat similar to what you’re looking to do.
Okay, so now I have a BP that consists of SphereCollisions and Phyics Constraints.
I also can add force to every constraint.
But the player can add constant force to the constraint.
I’d like to have the player press UP only to make the rope fully swing. Does someone have a basic idea to accomplish this?
Adding constant force isn’t the solution
I just tackled this same problem recently for my platformer template. I ended up using physics constraints and calculating the correct attach point for the player. I also have rope climbing on my template (spline based), though not both behaviors at the same time.
Maybe you can help, since this is some math problem:
My Rope consists of multiple Constraints.
and between these a mesh is drawn, the rope itself.
I’d like to align my Character (rotation wise) to this mesh.
I’m bad at math and don’t know how I can accomplish this.
Here’s a mona lisa to visualize what I want:
Im not sure how much help I will be as Im also not very good at math :(. But, here is my take on it:
I’m assuming you already created the rope with all the physics constraints tied together and are handling movement, so I would use a sphere trace around the character’s mid section looking for the rope object (trace by object). On hit, I would get the impact actor, impact location and impact normal of the physics constraint nearby. I would use the impact location to set actor’s world location on a timer (can be tick) and I would rotate the character to the impact normal.
This is where it gets complicated - Then I would cast to the impact actor (physics constraint) and enable physics and add impulse based on player input (W would add impulse in the X direction, etc). The issue is that while you will add impulse to that single constraint, how do you handle physics for the rest of the chain? That math is beyond me :). Maybe try Bruno’s advice when it comes to adding impulse.
I’m actually not satisfied with my solution, so after playing around for hours I decided that I would leave it as is for now. If you do figure it out, please post back your results
So if the the rope segment is between two constraints. Subtract the world location of the bottom one from the world location of the top one. Normalize this and you’ll have your desired up direction vector. Plug this into MakeRotFromZ, and SetWorldRotation on your character with tick.
If you also want your character to face in the direction that the rope is moving, then GetVelocity(CharacterActor), GetComponentVelocity(CharacterComponent) or GetPhysicsLinearVelocity(CharacterComponent). Normalize it. Use MakeRotFromXZ instead of MakeRotFromZ. Feed the normalized velocity into X and the desired up vector into Z. and SetWorldRotation with tick.
Hey fairly new here, I have been trying to create rope swing like yours and was wounding how did you do it as when I attach my third person character using the attach to component node to the rope it just goes to the top of the cable component and fall off doest stay attach, how did you make it so that the character stays attach and get to the middle of the cable and not fall off?
Very late to the party, but I’ve been banging my head to create a rope behavior similar to what OP described for months, and I finally got it to where I’m quite pleased with the result. It’s for a 2D game, but the concept should work just fine for 3D as well.
I made several prototypes with cables, physics constraints, spline mesh that all fell apart at one point or another before arriving at my current solution.
I’d be happy to break it down in detail if anyone is still looking for a way to create this kind of rope, but here’s my high-level approach:
The rope actor uses a skeletal mesh with a physics Asset as a base.
*Compared to manually creating a rope with constraints and capsules, I found a bone-chain much more stable and easier to set up.*
Skeletal mesh can be swapped out per instance
Skeletal mesh set to invisible and hidden in game
Construction script adds a spline with points at each bone socket
*Since the skeletal mesh still moved in Y even after all the axis were locked, I matched the spline to only X and Z to keep it on my 2D plane*
Spline used to add spline mesh
Spline mesh deforms much nicer than a skinned mesh and keeps its “thickness” well, especially when the rope coils in on itself
On Tick: Update spline points to match bone sockets, and update spline mesh to match spline points.
Skeletal mesh capsules use custom object channel “Rope Actor” with NO overlap events
Overlap events trigger the character movement impulse sent to physics bodies which I can’t control.
*Since I'm doing 2D, I don't want the rope to block my character, but still react a little bit when the character walks past.*
On Tick: trace for object type “Rope Actor”
Walking: Apply impulse to closest bone on rope based on values set per rope instance
Falling: Grab Rope (unless already on another rope)
Get closest point on rope spline based on character trajectory, get distance along spline as target
Set movement mode flying and ignore rope collision
Timer Function: match character location to location of “Location at distance alone Spline” determined earlier
Rotation of character is determined by a custom vector between current character location and a point “hand-distance”-away further up the spline
On Up-Input, I run a timeline that outputs a distance float value and on update, add/subtract that value from my distance along spline variable
Location-match timer function keeps matching character location/rotation to the spline at that updated distance
Get a custom forward vector based on closest bone to character and root bone pivot for a nice arc, regardless of actual bone/character rotation
Calculate swing force
There’s a lot going on here, but basically I wanted a short ease-in to full force and decrease the force as the character swings upward so I can’t just infinitely hover at an angle if I keep pressing in that direction
Rope applies force to bone closest to character AND fractional force to each bone above the target point, decreasing in strength the further away from the character the bone is. This is what really made the swing work for me - before that, the rope would always start to curve as I swung higher, and eventually become quite jerky and unstable
Obviously, lots of smaller things going on here and there, but that’s beyond the scope of a simple reply.
MickyPain I absolutely love the functionality and movement of your rope! I have tried for roughly a month now to create something similar to no avail… by the sounds of it your instructions here are pretty straight forward and for the UE4 experienced easily constructed however that is not me… so basically you lost me here…“Construction script adds a spline with points at each bone socket” is there anyway you can further dumb this down or show me your construction script logic? It would be greatly appreciated.