Character controller rocket jumping (adding force, while still maintaining control of the character)
Hi,
I have a movement script using a character controller component, and I want to add rocket jumping to it, the affect I'm after is similar to AddExplosionForce to a rigidbody.
I have found a similar answer here: https://answers.unity.com/questions/242648/force-on-character-controller-knockback.html
It's for a first person shooter game, and the issue I have with this is that my player does not have full control after rocket jumping, I can't move or jump until the knockback effect has completely finished, I believe this is due to the Vector3.Lerp method, so the player is moving between 2 vector3 locations
Does anyone know an alternative way to add force to the character controller while still maintaining full control? Thanks in advance!
Here is the code I'm using for Knockback at the moment:
// Player script
public float knockbackSpeed = 4f;
public float playerMass = 3f;
public Vector3 knockbackImpact = Vector3.zero;
private void Update()
{
// This is to move the player if they are affected by a rocket explosion
if (knockbackImpact.magnitude > 0.2f) playerCC.Move(knockbackImpact * Time.deltaTime);
// moves the character between two vector3 locations per second
knockbackImpact = Vector3.Lerp(knockbackImpact, Vector3.zero, knockbackSpeed * Time.deltaTime);
}
// function to apply force to the player when hit by a rocket explosion
public void Knockback(Vector3 direction, float force)
{
direction.Normalize();
if (direction.y < 0) direction.y = -direction.y; // reflect down force on the ground
knockbackImpact += direction.normalized * force / playerMass;
}
// Rocket explosion script
private Vector3 explosionOrigin;
public float explosionRadius = 3f;
public float explosionForce = 70f;
public float explosionUpwardsForce = 1f;
private IEnumerator Start()
{
explosionOrigin = transform.position;
// Wait one frame before starting, to ensure all objects are affected
yield return null;
Collider[] objectsInRange = Physics.OverlapSphere(explosionOrigin, explosionRadius);
foreach (Collider collision in objectsInRange)
{
foreach (Collider collision in objectsInRange)
{
Player player = collision.GetComponent<Player>();
if (player != null)
{
// Apply knockback force to player if they are in explosion radius
player.Knockback(player.transform.position - explosionOrigin, explosionForce);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612150348im_/https://answers.unity.com/themes/thub/images/avi.jpg)