Knock back from enemy and player

Silver100

Member
Just been experimenting with the Rigid body impulse script. Is it possible to use the Impulse script to allow for bouncing off enemies? My Player and enemy colliders can get stuck thought this would be a good work around by adding force. I have experimented seems to work really well on an empty game object, but when applied to the player or enemy gets ignored. My thoughts were set active true on a Rigid body impulse script/add force) on trigger collider event with enemy. It's not that simple though.
 
The character controller is kinematic so you cannot apply forces through the rigidbody. You can instead add forces to the character locomotion component. You can do this by calling AddForce on the character locomotion component.
 
Thanks. I have also realised adding the character material to the players collider works really well. Is there a direct line of code on the locomotion script responsible for the main character movement to addforce (where I could perhaps apply if statements, i.e. player stops movement on enemy collision, then bounces back then movement is enabled again)?
 
For that I recommend using the ability system. This will allow you to have complete control over the movement of the character. You can set the amount to move with the DesiredMovement property. If you take a look at the existing abilities you'll find a lot of examples with this. You can also call AddForce from the ability.
 
Thanks, Starting to get the hang of the ability system now. Created my own knockback ability from watching the New Ability tutorial and applying my own logic using part of the jump animation as knockback and placing the object identifier on the enemy. I can get animations to play and return to idle. Adding force however is not so easy.
After following the tutorial carefully I noticed;
public class Knockback : DetectObjectAbilityBase// Allows animations and the exit event, but could not get force or on trigger events. I usually just use mono behaviour when scripting my force jump/bounce events usually around health impact and add in a force type code.
I also experimented with the existing ability 'Impact Knockback' (default ability index 13) Tried auto setting to play on start, to see an example but it wouldn't respond. (also not sure if that 'impact Knockback' is just an animation as opposed to add force. It's looking great so far, I may be trying too much too soon, but would be good to get knockback force working. I'm sure there must be a simpler solution. Basically after being hit by a boss in the game player has to receive enough force to move across the screen and land.
 
I have got force added works really well. Just need to figure a way to allow force to effect only x 1 of the players colliders?

Spotted a minor class issue on DetectObjectAbilityBase;

When switching from; 'Ability' class to 'DetectObjectAbilityBase'.Load error even though script works fine.

Tested in a new script & project just to be sure same error too.

public class Knockback : 'DetectObjectAbilityBase' Script actually works but shows ' Associated script cannot be loaded error'
(Although is working fine) - Also no custom Public fields can be accessed I assume this is normal though.

Aside from that, following the process runs perfectly the New Ability tutorial is really well done requires complete attention to not miss a single step.
 
Since your ability doesn't need to detect any objects you should inherit from the Ability class and not DetectObjectAbilityBase. There is a Damage Visualization ability that sounds similar to what you are going for so I recommend taking a look at that ability.
 
Thats worked. I also realised I was putting the 2 abilities knock back (animation) and force together not sure if thats right, so have now separated & scripted into the according class as you mentioned and I can just have force or knockback, or both combined which is working great.

I also realised that these types of class function operate fine without even being attached to the player (with no errors showing now). I've only ever used class monobehaviours where I attach scripts on each object. Definitely will take a look at the damage visualiser too.

Thanks
 
Top