1
\$\begingroup\$

The intentions of this function are to utilize the RigidBody component from a "target" object and clear its force while applying a diagonal force in the direction upwards and away from the side they are facing. I have omitted the portion in which detects the other direction for the sake of simplicity in reading the function as I've been testing this function while the user faces the right-hand direction.

Variables:

  • target refers to the other object currently in the scene
  • targBody refers to the RigidBody component of the target object
    damage refers to the current damage integer of the object.
    The damage used in this instance is from the target object
    facingRight refers to a bool in which is true whenever the current object is facing the right-hand direction.

    public void Damage() {
        targBody.velocity = Vector3.zero;
        if(facingRight) {
            Vector3 newVel = new Vector3(target.GetComponent().damage/16, target.GetComponent().damage/16, 0);
            targBody.AddForce(newVel, ForceMode.Impulse);
            print("Stuff");
        }
        target.GetComponent().damage+=3;
    }

For some reason, when I apply the force, the target object doesn't move diagonally. Instead, it moves horizontally before moving vertically; however, for some strange reason the horizontal speed is always greater than the vertical speed as it goes so fast that it nearly teleports!

I'm not sure if this error happens because of a coding problem on my part that I'm just not recognizing or if it's a problem with RigidBody in which I need to use a workaround for.
Anyone have any ideas on what can make this function work the way I want it to?

\$\endgroup\$
  • \$\begingroup\$ Unless you can create a simple example that demonstrates a bug in AddForce, I am rather inclined to assume that the bug is on your side. Recommend simplifying your use case and testing until you figure out what's causing the issue. \$\endgroup\$ – rutter Nov 18 '15 at 7:13
  • \$\begingroup\$ I did not claim that what I'm experiencing is a bug in the engine, I explicitly stated in my question that I'm not sure if it's an error in my coding or an error in my instance of Rigidbody at that point. \$\endgroup\$ – RandomAnimeGamer Nov 24 '15 at 21:11
2
\$\begingroup\$

This code in my template project runs perfect and trow object diagonally:

    if (Input.GetKeyDown(KeyCode.Space))
    {
        rigi.velocity = Vector3.zero;
        rigi.AddForce(new Vector3(30, 30, 0), ForceMode.Impulse);
    }

So, i assume some possible reasons:

  1. You have wrong Vector3 newVel initialization..
  2. Your if statement calls multiple times and overwrite force every frame.
  3. Some where in the code another script interrups movements.
\$\endgroup\$
  • \$\begingroup\$ The code provided in the question happens to be a simplified version of my code because I felt it was probably that section of the code since that's where the error occurs. I'm pretty sure it's another script altering the force in the rigidbody because the script is called in a FixedUpdate() function and the vector3 is initialized as shown in the documentation. In testing, I would set the other player to a high damage number and test the knockback by pressing the P (attack) key without any other button being pressed at the same time. Here's a pastebin of my code: pastebin.com/mKhrmuDV \$\endgroup\$ – RandomAnimeGamer Nov 24 '15 at 21:13
  • \$\begingroup\$ Okay, so the problem is definitely at Line 44 now that I've commented out most of my FixedUpdate() until the code you placed above worked (I used KeyCode.Z instead of KeyCode.Space to avoid the conflict with some functions of mine). I added a condition to the if() statement, but now there seems to be some sliding on my rigidbody that I can fix easily. Thanks! \$\endgroup\$ – RandomAnimeGamer Nov 24 '15 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.