Hello,
I’m currently trying to program a spear, with which you can stab on the one hand, but at the same time have the possibility to throw it. When you throw the spear, it should be connected to a magic rope, which adapts to the throwing distance. After the spear has landed you should be able to pull it back with this rope, does anyone have an idea how to implement this?
There are multiple ways to go about this, and they can be both quite complex mechanics to make and I don’t know what you have planned, but I hope the following ideas can help you out:
Make a new Enumeration class called “CharacterStates”. Add three Enumerators called “Normal”, “StabAttack” and “SpearThrow”.
Stab Attack
-
To make a stab-attack, add an input action called “InputStabAttack” and set it to your desired key or controller button. When pressed, set the state to “StabAttack”. In your AnimBP, you can check the state and then play the appropriate animation.
-
Add a socket to your skeletal mesh’s hand. That will be the spot where you can attach and detach your spear mesh dynamically in Blueprint.
-
Create a new actor for the spear. Add a capsule component surrounding it. You can use that to check any overlaps with the spear and drive the gameplay effects from that. Attach it to the previously made socket within your character’s Blueprint.
-
When the animation is done, set the state to “Normal” and return to your idle/walk/etc. animations.
SpearThrow
-
To make a spear throw, add an input action called “InputSpearThrow” and set it to your desired key or controller button. Like before, when pressed, set the state to “SpearThrow”. In your AnimBP, you can check the state and then play the appropriate animation.
-
When pressed, use DetachFromActor to detach the spear actor and set it to simulate physics. Apply an impulse to make it throw.
-
I assume the spear will hit something or land on the ground. When that happens, move it back to the player. You can use lerp (vector) and it’s alpha to make the spear go back to the player. You can either use a tick times a factor or a timeline to drive the alpha.
-
The “magic rope” can either be a beam emitter or a cable component. You can spawn it when pressed, and when fully retracked you can destroy it again.
-
When fully retracked, set the state to “Normal” and return to your idle/walk/etc. animations.
I understand that I didn’t delve to deeply into all the details, and the implementation might vary based on your game, but if there’s any more questions about specific details don’t hesitate to reply.