hellooo all ... just a simple physic qustion : is there any relation between the force and the speed ? i used this line of code to move an object : Code (csharp): function OnCollisionEnter(collision : Collision) { switch (ricket){ case ricket1: collision.gameObject.rigidbody.velocity = Vector3.zero; collision.gameObject.rigidbody.AddRelativeForce(direction*forceAmount); speed =collision.rigidbody.velocity.magnitude; }} i make it's velocity=0 , then i give it a force to move in a specific direction, it is must have a velocity after adding the force to it, but when i print the speed it's value = 0, i need the speed of the object when the collision happened after adding force to it. what is happening here?? why the velocity of the object still = 0 after adding the force to it? and in the play mode it's moving under the relative force and that's means it got a velocity, any help of how to calculate the speed from the force?
The force is applied, but doesn't take effect until after the FixedUpdate() calls for that frame. Essentially, you've provided the force, but it hasn't converted the force into velocity. If you waited one frame and then checked, you'd get the magnitude.
that's make sense, but what is the solution here? where i must get the speed value exactly when the force happened? i used OnCollisionEnter function here to add the force, any help plz?
Well, you can use LateUpdate() if you figure out a good way to call it right after applying the force, and you can put a yield; in front of the speed check. It'll give you the resulting speed at the start of the next frame.
Code (csharp): function LateUpdate () { if(ricketHi== true){ speed =ball.rigidbody.velocity.magnitude; print(speed); ricketHit = false;} } function OnCollisionEnter(collision : Collision) { switch (ricket){ case ricket1: collision.gameObject.rigidbody.velocity = Vector3.zero; collision.gameObject.rigidbody.AddRelativeForce(direction*forceAmount); ricketHit = true; }} i used this code like what u suggested but the speed value still = 0 as it is in the past code!!! i donno what is happening here !!!
Yes, of course: velocity = (force / mass) * time (derived from standard physics formulas: f = ma, v = at) You cannot query how much force is acting on a rigidbody, so knowing the formula doesn't give you much here. As suggested, reading out the velocity in Update() or LateUpdate() seems easiest.
that's not important cause i declare the variable as ricketHi !! .. it didn't give me any error in the console . i do it but it give me a wrong result, it must have a velocity after giving it force but the velocity still = 0 when i print it ... i'm stopping at this problem and can't get out of it ... any help plz?
That's weird, as you're setting ricketHit = false and ricketHit = true inside the code, but you're checking to see if ricketHi == true in the if statement. Ah well. It should work, so there's got to be something going on that we can't see from here.
I've just done a little test to investigate this. It doesn't work properly when you set the velocity value by assignment, but it seems to work OK when you use the VelocityChange mode with AddForce. So, you could do something like:- Code (csharp): collision.gameObject.rigidbody.AddForce(Vector3.zero, ForceMode.VelocityChange); collision.gameObject.rigidbody.AddRelativeForce(direction*forceAmount); ...to get the result you want.
are you sure that it will give me the right velocity here?!! because when i use the same script with the Yield statement i got another value for the velocity ! which one is the true one? the code is : Code (csharp): function OnCollisionEnter(collision : Collision) { switch (ricket){ case ricket1: collision.gameObject.rigidbody.velocity = Vector3.zero; collision.gameObject.rigidbody.AddRelativeForce(direction*forceAmount); yield WaitForSeconds (0.02); speed =collision.rigidbody.velocity.magnitude; print(speed); }} and also the two result didn't give me the expecting speed that i want!! that's lead me to another question : is the velocity.magnitude give me the speed of the ball?? is it right to get the speed of the ball like this in unity? or i can get it in another way??? is the velocity.magnitude in unity is the same as the velocity in the real physics world??? i need help of that plz if anyone can help??