No matter I do, the damn AddForce Method and also changing velocity doesn’t work consistent. What is going on? My Movement is totally consistent. Here is my code, I am trying to make a sort of Dash and normally I have a extra script, but I wanted to see, if somethings wrong with my script, but obviously it has nothing to do with each other. What is the problem, I just don’t see anything what this could cause.
(Side note: I’ve already tried to remove the material in the rigidbody2d or reset the “Force send/receive” fields in my BoxCollider2D, because I changed them. Also I’ve tried to multiply the method with Time.deltaTime orTime.fixedDeltaTime, I know in the FixedUpdate function this makes no sense, but I’m so desperate! Pls somebody help)
Know that saying AddForce method is inconsistent is no different than saying adding two Vector2 together is inconsistent which cannot be true. AddForce when set to impulse, adds that value to the current velocity (scaled by mass) and that’s it; nothing else would affect this simple addition. If it’s not an impulse then it’s added the same but to a force sum which will be added to the velocity later when the simulation runs and then reset.
Essentially it does this:
m_linearVelocity += m_invMass * impulse;
Or if your case where the mass is 1 then it’s essentially this:
m_linearVelocity += impulse;
Don’t use AddForce and do “_rb.velocity += Mathf.Pow(2, _acceleration) * Vector2.right;” and you’ll get the same thing.
Anything else is what you’re doing to make it inconsistent so you need to debug whatever you think is inconsistent. If it’s velocity then output it and the total impulse you’re applying to the console and ensure it’s what you expect it to be.
There are not more details. This is the whole code. And even when I try to change the velocity it’s not consistent. The standard deviation per dash is round about 1 unit and I can’t see why.
All information is in my code and my written text.
Although I did not mention the inheritance but the parent is only a Awake method with the player component: so not important.
Please be more specific in your replies, tried what? Replacing AddForce?
Then it’s your timing of the “dash”. You’re saying the code above is everything but it cannot be. The above is just constantly setting an impulse if “isDashInput” is true, there’s nothing stopping the dash or anything else.
What you’ve posted cannot be debugged by eye, it’s also not everything that’s happening as I’ve said.
But it is. I press “Q” and then the force/velocity is applied and the code path gets closed because of the boolean.
That’s it. I even had deleted the inputs for my movement to see if there is some problem with these.
Also I observed that the Add force method gives twice or triple the result which it should be. But if I try to look at the times the code gets execute it just shows only one time.
Nope, you’re looking at something wrong. If you add an impulse of (100,0) then the velocity from (0,0) will be (100,0) if mass is 1.
I don’t know what that means. I’m pretty sure you’re misunderstanding what adding a force (impulse) does and you’re perhaps assuming that if you stop adding a force (impulse) then it automatically stops for some reason which isn’t correct (see Newton’s Three Laws of motion).
If this isn’t the case (and maybe not because you tagged yourself as “intermediate”) then please can you explain what actually stops the “dash” i.e. what stops the rigidbody from moving (resets it velocity).
Again: Every time the Key Q is pressed I print a counter of how many times the if statement in the fixed update method was execute and it’s exactly one time. So the Add Force method should also just get one time execute. Logical, I guess? And my calculations show that the force is getting two times execute. As a example: If I press Q then my player dashes normally 1 unit but it’s randomly a output of 0-3 units.
No I know what add force does and also how to use velocity. But with both the dash is not consistent.
The force is getting stopped because of the boolean. The code is first started as I press Q and then stopped because of the boolean, even though the Force isn’t stopping right away, it shouldn’t be a very different result (where it shouldn’t be different at all).
Can you show me the line above where you set the velocity to zero i.e. stop the player?
I cannot debug your descriptions of “boolean”, “press Q” so you need to debug this yourself. You say you’ve calculated this and that but I again I cannot verify any of that so I take you word but I also cannot help you still. You’re not providing anything useful to help you unfortunately.
If you think physics is broken in such a basic way then please show how. For instance, if you add an impulse, immediately dump out the velocity after, it’ll show you the change and it should be correct. Each fixed update, dump out the position and ensure that it’s correct which it should be based upon the velocity.
It just sounds like these “calculations” you’re making are wrong perhaps.
As I asked above, instead of reiterating how you think it’s not working, please show something, anything which show what you’re doing to verify it’s not working.
Soo, after a lot of headache I had to try creating a new project, where I put the assets into. And it turns out that it workes. So it wasn’t any problem of my script or something, rather a corrupted file or maybe some configs in the physics were changed but I didn’t change anything so I guess there were for real a corrupted file.
But I’m so glad that it’s gone.