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 /
This question was closed Jun 01, 2019 at 02:00 PM by DoctorChar for the following reason:

New question on similar topic

avatar image
0
Question by DoctorChar · May 25, 2019 at 07:07 AM · movementaddforcemovement scriptairfightingame

Addforce lasting 1 frame

Edit: (My rigid body settings are as follows - mass = 1 drag = 0 angular drag = 0.05 gravity = yes is kinematic = no interpolate = none collision detection = discrete only constraints on all rotations)

Hello,

I am trying to make a directional air dodge mechanic (like in smash bros) but the force I am adding is only lasting for one frame. This is only happening with my left and right (A and D) inputs. Here is the specific part of the script.

 else if (anim.GetBool("IsAirborn") == true && Input.GetKeyDown(KeyCode.I) && NotShielding == true && Input.GetKey(KeyCode.D) && airdodgeamount < 1 && djumping == false)
         {
             NotShielding = false;
             gravity = 0;
             anim.SetInteger("AnimParameter", 9);
             speed = 30f;
             calculatedforce = speed * (50 * mainrb.mass);
             mainrb.AddForce(new Vector3(calculatedforce, 0, 0));
             coroutine = WaitForAirdodge(0.167f);
             StartCoroutine(coroutine);
             gravity = 8;
             airdodgeamount++;
         }
         else if (anim.GetBool("IsAirborn") == true && Input.GetKeyDown(KeyCode.I) && NotShielding == true && Input.GetKey(KeyCode.A) && airdodgeamount < 1 && djumping == false)
         {
             NotShielding = false;
             gravity = 0;
             anim.SetInteger("AnimParameter", 9);
             speed = -30f;
             calculatedforce = speed * (50 * mainrb.mass);
             mainrb.AddForce(new Vector3(calculatedforce, 0, 0));
             coroutine = WaitForAirdodge(0.167f);
             StartCoroutine(coroutine);
             gravity = 8;
             airdodgeamount++;
         }
         else if (anim.GetBool("IsAirborn") == true && Input.GetKeyDown(KeyCode.I) && NotShielding == true && Input.GetKey(KeyCode.W) && airdodgeamount < 1 && djumping == false)
         {
             NotShielding = false;
             gravity = 0;
             anim.SetInteger("AnimParameter", 9);
             speed = 0.1f;
             calculatedforce = speed * (50 * mainrb.mass) - mainrb.velocity.y;
             mainrb.AddForce(new Vector3(0, calculatedforce, 0), ForceMode.Impulse);
             coroutine = WaitForAirdodge(0.167f);
             StartCoroutine(coroutine);
             gravity = 8;
             airdodgeamount++;
         }
         else if (anim.GetBool("IsAirborn") == true && Input.GetKeyDown(KeyCode.I) && NotShielding == true && Input.GetKey(KeyCode.S) && airdodgeamount < 1 && djumping == false)
         {
             NotShielding = false;
             gravity = 0;
             anim.SetInteger("AnimParameter", 9);
             speed = -0.05f;
             calculatedforce = speed * (50 * mainrb.mass) - mainrb.velocity.y;
             mainrb.AddForce(new Vector3(0, calculatedforce, 0), ForceMode.Impulse);
             coroutine = WaitForAirdodge(0.167f);
             StartCoroutine(coroutine);
             gravity = 8;
             airdodgeamount++;
         }
         else if (anim.GetBool("IsAirborn") == true && Input.GetKeyDown(KeyCode.I) && NotShielding == true && airdodgeamount < 1 && djumping == false)
         {
             gravity = 0;
             NotShielding = false;
             anim.SetInteger("AnimParameter", 9);
             calculatedforce = 0 - mainrb.velocity.y;
             mainrb.AddForce(new Vector3(0, calculatedforce, 0), ForceMode.Impulse);
             coroutine = WaitForAirdodge(0.167f);
             StartCoroutine(coroutine);
         }

Here is my entire script fixed update script as i have no idea where the problem could be.

 void FixedUpdate()
     {
         Physics.gravity = new Vector3(0, -gravity, 0);
         
         if (touching == true && jumping == false)
         {
             anim.SetBool("IsAirborn", false);
         }
         else if (touching == false)
         {
             anim.SetBool("IsAirborn", true);
         }
 
         if (anim.GetBool("IsAirborn") == false && Input.GetKeyDown(KeyCode.I) && NotShielding == true)
         {
             NotShielding = false;
             anim.SetInteger("AnimParameter", 6);
             shield.SetActive(true);
             coroutine = WaitForShield(0.167f);
             StartCoroutine(coroutine);
         }
         else if (anim.GetBool("IsAirborn") == true && Input.GetKeyDown(KeyCode.I) && NotShielding == true && Input.GetKey(KeyCode.D) && airdodgeamount < 1 && djumping == false)
         {
             NotShielding = false;
             gravity = 0;
             anim.SetInteger("AnimParameter", 9);
             speed = 30f;
             calculatedforce = speed * (50 * mainrb.mass);
             mainrb.AddForce(new Vector3(calculatedforce, 0, 0));
             coroutine = WaitForAirdodge(0.167f);
             StartCoroutine(coroutine);
             gravity = 8;
             airdodgeamount++;
         }
         else if (anim.GetBool("IsAirborn") == true && Input.GetKeyDown(KeyCode.I) && NotShielding == true && Input.GetKey(KeyCode.A) && airdodgeamount < 1 && djumping == false)
         {
             NotShielding = false;
             gravity = 0;
             anim.SetInteger("AnimParameter", 9);
             speed = -30f;
             calculatedforce = speed * (50 * mainrb.mass);
             mainrb.AddForce(new Vector3(calculatedforce, 0, 0));
             coroutine = WaitForAirdodge(0.167f);
             StartCoroutine(coroutine);
             gravity = 8;
             airdodgeamount++;
         }
         else if (anim.GetBool("IsAirborn") == true && Input.GetKeyDown(KeyCode.I) && NotShielding == true && Input.GetKey(KeyCode.W) && airdodgeamount < 1 && djumping == false)
         {
             NotShielding = false;
             gravity = 0;
             anim.SetInteger("AnimParameter", 9);
             speed = 0.1f;
             calculatedforce = speed * (50 * mainrb.mass) - mainrb.velocity.y;
             mainrb.AddForce(new Vector3(0, calculatedforce, 0), ForceMode.Impulse);
             coroutine = WaitForAirdodge(0.167f);
             StartCoroutine(coroutine);
             gravity = 8;
             airdodgeamount++;
         }
         else if (anim.GetBool("IsAirborn") == true && Input.GetKeyDown(KeyCode.I) && NotShielding == true && Input.GetKey(KeyCode.S) && airdodgeamount < 1 && djumping == false)
         {
             NotShielding = false;
             gravity = 0;
             anim.SetInteger("AnimParameter", 9);
             speed = -0.05f;
             calculatedforce = speed * (50 * mainrb.mass) - mainrb.velocity.y;
             mainrb.AddForce(new Vector3(0, calculatedforce, 0), ForceMode.Impulse);
             coroutine = WaitForAirdodge(0.167f);
             StartCoroutine(coroutine);
             gravity = 8;
             airdodgeamount++;
         }
         else if (anim.GetBool("IsAirborn") == true && Input.GetKeyDown(KeyCode.I) && NotShielding == true && airdodgeamount < 1 && djumping == false)
         {
             gravity = 0;
             NotShielding = false;
             anim.SetInteger("AnimParameter", 9);
             calculatedforce = 0 - mainrb.velocity.y;
             mainrb.AddForce(new Vector3(0, calculatedforce, 0), ForceMode.Impulse);
             coroutine = WaitForAirdodge(0.167f);
             StartCoroutine(coroutine);
         }
         else if (anim.GetBool("IsAirborn") == false && Input.GetKeyDown(KeyCode.W) && NotShielding == true)
         {
             jumping = true;
             anim.SetBool("IsAirborn", true);
             anim.SetInteger("AnimParameter", 3);
             coroutine = WaitForJump(0.3f);
             StartCoroutine(coroutine);
         }
         else if (anim.GetBool("IsAirborn") == true && Input.GetKeyDown(KeyCode.W) && doublejumpamount < 1f && NotShielding == true)
         {
             djumping = true;
             anim.SetInteger("AnimParameter", 5);
             anim.SetBool("IsAirborn", true);
             coroutine = WaitForDJump(0.2f);
             StartCoroutine(coroutine);
             doublejumpamount++;
         }
         else if (anim.GetBool("IsAirborn") == true && Input.GetKey(KeyCode.S) && NotShielding == true)
         {
             gravity = 15;
         }
         else if (Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.LeftShift) && anim.GetBool("IsAirborn") == false && NotShielding == true)
         {
             anim.SetInteger("AnimParameter", 2);
             transform.eulerAngles = new Vector3(0, 270, 0);
             speed = -1;
             calculatedforce = speed * (50 * mainrb.mass);
             mainrb.AddForce(new Vector3(calculatedforce, 0, 0));
         }
         else if (Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.LeftShift) && anim.GetBool("IsAirborn") == false && NotShielding == true)
         {
             anim.SetInteger("AnimParameter", 2);
             transform.eulerAngles = new Vector3(0, 90, 0);
             speed = 1;
             calculatedforce = speed * (50 * mainrb.mass);
             mainrb.AddForce(new Vector3(calculatedforce, 0, 0));
         }
         else if (Input.GetKeyDown(KeyCode.A) && anim.GetBool("IsAirborn") == false && NotShielding == true)
         {
             anim.SetInteger("AnimParameter", 1);
             transform.eulerAngles = new Vector3(0, 270, 0);
             speed = -10;
             calculatedforce = speed * (50 * mainrb.mass);
             mainrb.AddForce(new Vector3(calculatedforce, 0, 0));
         }
         else if (Input.GetKeyDown(KeyCode.D) && anim.GetBool("IsAirborn") == false && NotShielding == true)
         {
             anim.SetInteger("AnimParameter", 1);
             transform.eulerAngles = new Vector3(0, 90, 0);
             speed = 10;
             calculatedforce = speed * (50 * mainrb.mass);
             mainrb.AddForce(new Vector3(calculatedforce, 0, 0));
         }
         else if (Input.GetKey(KeyCode.A) && anim.GetBool("IsAirborn") == false && NotShielding == true)
         {
             anim.SetInteger("AnimParameter", 1);
             transform.eulerAngles = new Vector3(0, 270, 0);
             speed = -2;
             calculatedforce = speed * (50 * mainrb.mass);
             mainrb.AddForce(new Vector3(calculatedforce, 0, 0));
         }
         else if (Input.GetKey(KeyCode.D) && anim.GetBool("IsAirborn") == false && NotShielding == true)
         {
             anim.SetInteger("AnimParameter", 1);
             transform.eulerAngles = new Vector3(0, 90, 0);
             speed = 2;
             calculatedforce = speed * (50 * mainrb.mass);
             mainrb.AddForce(new Vector3(calculatedforce, 0, 0));
         }
         else if (Input.GetKey(KeyCode.A) && anim.GetBool("IsAirborn") == true && NotShielding == true)
         {
             speed = -2;
             calculatedforce = speed * (50 * mainrb.mass);
             mainrb.AddForce(new Vector3(calculatedforce, 0, 0));
         }
         else if (Input.GetKey(KeyCode.D) && anim.GetBool("IsAirborn") == true && NotShielding == true)
         {
             speed = 2;
             calculatedforce = speed * (50 * mainrb.mass);
             mainrb.AddForce(new Vector3(calculatedforce, 0, 0));
         }
         else if (Input.GetKey(KeyCode.S) && NotShielding == false)
         {
             anim.SetInteger("AnimParameter", 8);
             shield.SetActive(false);
             coroutine = WaitForSpotdodge(0.2f);
             StartCoroutine(coroutine);
         }
         else if (NotShielding == true)
         {
             anim.SetInteger("AnimParameter", 0);
             gravity = 8;
         }
         else
         {
             
         }
 
         if (wanttojump == true)
         {
             if (Input.GetKey(KeyCode.W))
             {
                 jumping = false;
                 jumpspeed = 1f;
                 calculatedforce = jumpspeed * (5 * mainrb.mass) - (mainrb.velocity.y * 1);
                 mainrb.AddForce(new Vector3(0, calculatedforce, 0), ForceMode.Impulse);
             }
             else if (!Input.GetKey(KeyCode.W))
             {
                 jumping = false;
                 jumpspeed = 0.7f;
                 calculatedforce = jumpspeed * (5 * mainrb.mass) - (mainrb.velocity.y * 1);
                 mainrb.AddForce(new Vector3(0, calculatedforce, 0), ForceMode.Impulse);
             }
 
             anim.SetInteger("AnimParameter", 4);
             anim.SetBool("IsAirborn", true);
             wanttojump = false;
         }
         if (resetgravity == true)
         {
             gravity = 8;
             resetgravity = false;
         }
         if (wanttodjump == true)
         {
             gravity = 0;
             jumpspeed = 1;
             calculatedforce = jumpspeed * (5 * mainrb.mass) - (mainrb.velocity.y * 1);
             mainrb.AddForce(new Vector3(0, calculatedforce, 0), ForceMode.Impulse);
             anim.SetInteger("AnimParameter", 4);
             wanttodjump = false;
             coroutine = GravityDelay(0.1f);
             StartCoroutine(coroutine);
             djumping = false;
         }
 
         touching = false;
     }


Thank you in advance.

Comment
Add comment · Show 2
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 highpockets · May 25, 2019 at 08:52 AM 0
Share

Not sure if this is the only issue, but you are setting the force just once after you set NotShielding to false. Not sure what else is happening in the coroutine because I don't see it here, but I imagine that it is just waiting for the given amount of time and then perhaps setting the notShielding to true again. I would suggest adding the force in the coroutine if you want to do it every fixed update (WaitForFixedUpdate());

Cheers

avatar image Captain_Pineapple · May 25, 2019 at 09:49 AM 0
Share

Hey there,

please rework that code.... This might also make you discover that bug...


For example:

When you have a if ... elseif... elseif and in EVERY statement you check for example for NotShielding == true this is horrible. For once it's difficult to understand, but also performance suffers from this. Divide those statements. Have rather some deeper if/else trees than one if/elseif that is soo long. This will also make it possible that you can delete half of that code. Whenever you have an if/elseif statement where for 2 or more conditions the exact same code is executed this can be done better!

              calculatedforce = speed * (50 * mainrb.mass);
              mainrb.AddForce(new Vector3(calculatedforce, 0, 0));

Those 2 lines alone are in 10 of those statements. Rework it and people will be more likely to debug this.

1 Reply

  • Sort: 
avatar image
1

Answer by Meishin · May 25, 2019 at 01:56 PM

Hello @DoctorChar,

I'm sorry i'm not going to spot your specific issue but if an action only last 1 step it's usually because you overwrite it the next step (or within the same step).

That being said, in my humble opinion, you need to clean your code first cause it is hardly readable, for examples: - Segment your code more : handle NotShielding == false specific treatment appart from the rest so you don't write it 15 times in the if statements - Factorise your code and give meaning to your code:

for example lines 105 to 164 could be rewrote like below and read must clearer:

 else if (NotShielding == true &&
             (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.A) || 
              Input.GetKeyDown(KeyCode.D) ||Input.GetKeyDown(KeyCode.A)))
 {
    handleADMovement();
 }
          
 void handleADMovement()
 {
     float initialSpeedBoost = 5f;
         
     // Handle specific Airborn movement
     if (anim.GetBool("IsAirborn"))
     {
         // Invert Speed depending on A/D key
         speed = 2 * (Input.GetKey(KeyCode.A) || Input.GetKeyDown(KeyCode.A)? -1:1);
     }
     else
     {
         // Now everything under is not Airborn related
 
         // D Control - to be described
         if (Input.GetKey(KeyCode.D) || Input.GetKeyDown(KeyCode.D))
         {
             transform.eulerAngles = new Vector3(0, 90, 0);
             // Add an initial speed boost in case key just pressed
             speed = 2 * (Input.GetKeyDown(KeyCode.D) ? initialSpeedBoost : 1f); 
          }
 
          // A Control - to be described
          // Takes precedence over D Key (cf else if statements)
          // So if A & D key are pressed simultaneously, only A key will be taken into account
          if (Input.GetKey(KeyCode.A) || Input.GetKeyDown(KeyCode.A))
          {
                 transform.eulerAngles = new Vector3(0, 270, 0);
                 speed = -2 * (Input.GetKeyDown(KeyCode.A) ? initialSpeedBoost : 1f);
          }
 
          // Handle Grounded animation and Shift control
          // Shift Key control - Set anim 2 and reduce speed to 1/-1
          if (Input.GetKey(KeyCode.LeftShift))
          {
              anim.SetInteger("AnimParameter", 2);
              speed = speed / speed;
          }
          // Set anim 1 - Prob Grounded animation
          else
          {
              anim.SetInteger("AnimParameter", 1);
          }
     }
         
     // Apply force to rigidbody
     calculatedforce = speed * (50 * mainrb.mass);
     mainrb.AddForce(new Vector3(calculatedforce, 0, 0));
 }
Comment
Add comment · Show 3 · 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 Captain_Pineapple · May 25, 2019 at 02:38 PM 0
Share

That is some effort man, big respect here! Though i think people should do that on their own to actually have a learning experience from this.

avatar image Meishin Captain_Pineapple · May 26, 2019 at 05:06 PM 0
Share

haha yeah i agree tho he/she still has plenty of opportunities to learn ^^

avatar image DoctorChar · May 27, 2019 at 06:03 AM 0
Share

Thank you so much, I often struggle with formatting so I will reorganise my code and then try and find the problem again. This looks like it would have taken a lot of effort, so that was really kind of you to do!

Follow this Question

Answers Answers and Comments

167 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 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

Why can't i addforce to a rigidbody? 2 Answers

How to move Character with AddForce 1 Answer

AddForce/ForceMode.Impulse 1 Answer

Slowly move a GameObject on 1 axis, then destroy it. 1 Answer

In air movement troubles. 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