• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Pixelch3f · Jan 12, 2018 at 03:33 PM · charactercontrollerforceexplosionrocket

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);
             }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

134 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rocket Landing 1 Answer

Why Rigidbody.AddForce is not acting like the correct physical force? 1 Answer

How do I get objects in front of the player to be blasted away? 1 Answer

How do I make a rocket? 2 Answers

How to make player rotate 180° and run that direction with mecanim + vice versa? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges