Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by Trissykins · Aug 19, 2019 at 05:18 AM · explosionlimitforces

Limiting player addforce speed excluding when hit by explosion

Hey guys I'm pretty new to this.. I am currently creating a 3d (2d movement) game where you must shoot other players off of a platform. The problem i have encountered is through limiting the player speed in the fixedupdate, the player does not get influenced by the explosion as much as i would like. I want to find a way to limit the player addforce speed without accelerating like crazy and without hindering how much the player is influenced by the explosion. my code is below thanks in advance :)

     public void Move(Vector3 moveDirection, bool jump)
     {
         // Otherwise add force in the move direction.
         {
             m_Rigidbody.AddForce(moveDirection * m_MovePower);
         }
         // If on the ground and jump is pressed...
         if (Physics.Raycast(transform.position, -Vector3.up, k_GroundRayLength) && jump)
         {
             // ... add force in upwards.
             m_Rigidbody.AddForce(Vector3.up * m_JumpPower, ForceMode.Impulse);
         }
     }

     private void FixedUpdate()
     {
         //both of these limit our left right max movement speed while still being able to fall exponentially faster due to gravity

         if (m_Rigidbody.velocity.x > maxSpeed)
         {
         m_Rigidbody.velocity = new Vector2(maxSpeed, m_Rigidbody.velocity.y);
         }

         if (m_Rigidbody.velocity.x < -maxSpeed)
         {
         m_Rigidbody.velocity = new Vector2(-maxSpeed, m_Rigidbody.velocity.y);
         }

 void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.tag == "Damage")
     {
         Vector3 force = transform.position - other.transform.position;
         force.Normalize();
         m_Rigidbody.AddForce(force * BlastTotal);
         if (Weakness < 20)
         {
             Weakness += 1;
             transform.localScale += new Vector3(0.05F, 0.05f, 0.05f);
         }
     }
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Trissykins · Aug 28, 2019 at 05:14 AM

So I ended up figuring out a way to fix the issue :) thanks @Bunny83 for giving me the idea!

Basically what i did was separate the left and right movement so that unity can tell which way we are intending to go. I did this using

         if (Input.GetAxis("Horizontal") > 0)
         {
             if (m_Rigidbody.velocity.x <= maxSpeed)
             {
                 m_Rigidbody.AddForce(Vector3.right * m_MovePower);
             }
             else return;
         }
         if (Input.GetAxis("Horizontal") < 0)
         {
             if (m_Rigidbody.velocity.x >= -maxSpeed)
             {
                 m_Rigidbody.AddForce(-Vector3.right * m_MovePower);
             }
             else return;
         }

by doing this if a players intended direction is left, but they are already travelling fast in that direction, no addforce will be applied. (This works the same for the right). This means that the player is still able to be affected by explosives and travel at a much greater speed than the movement system would allow the player to reach on their own!

Comment
Add comment · Share
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
avatar image
0

Answer by Cornelis-de-Jager · Aug 19, 2019 at 05:43 AM

Try this simple solution:

 void FixedUpdate () {
     // Split the direction and magnitude
     var dir = m_Rigidbody.normalized;
     var mag = m_Rigidbody.magnitude;
     
     // Check if we are exeding the max speed
     if (mag > maxSpeed)
         m_Rigidbody.Veclocity = maxSpeed * dir;
 }
Comment
Add comment · Show 1 · Share
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
avatar image Trissykins · Aug 20, 2019 at 09:57 PM 0
Share

I'm not having any issues with limiting the speed. That part of the code works almost too well in this case. The problem is I'm trying to limit the speed a player can get to from their own input, while still having the player able to travel much faster when hit with an explosion. I don't know how to limit one and not the other. I'm making a game similar to brackeys "ball wars" game if that helps add to the context :)

avatar image
0

Answer by Bunny83 · Aug 21, 2019 at 10:08 AM

The only two solutions for such cases are:

  • stop adding forces in a direction if the velocity is already larger than the wanted max speed.

  • limiting the velocity but add a special case when applying explosion forces you add a bool to temporarily disable the limiting code.


Both cases have some flaws. Disable AddForce when you reached the wanted velocity can allow the player to slightly overshoot the wanted velocity. The second case might cause some glitches since you have to re-enable the velocity limiting code at some point. This can be either after a certain time or when a certain event happens (like player hits the floor or something).


I'm currently not home so I can't write any example code at the moment.

Comment
Add comment · Share
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

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.

Follow this Question

Answers Answers and Comments

108 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

Related Questions

AddExplosionForce not working 1 Answer

Making a bubble level (not a game but work tool) 1 Answer

Which is AddExplosionForce relations with rigidibody mass and distance ? 0 Answers

explosion instaniate not destroyed 1 Answer

Explosive ANimation 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges