Search Unity

speed Via force

Discussion in 'Scripting' started by Hanankk, Aug 31, 2009.

  1. Hanankk

    Hanankk

    Joined:
    Sep 2, 2008
    Posts:
    103
    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):
    1. function OnCollisionEnter(collision : Collision) {
    2. switch (ricket){
    3.    case ricket1:
    4. collision.gameObject.rigidbody.velocity = Vector3.zero;
    5. collision.gameObject.rigidbody.AddRelativeForce(direction*forceAmount);
    6. speed =collision.rigidbody.velocity.magnitude;
    7. }}
    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?
     
  2. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    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.
     
  3. Hanankk

    Hanankk

    Joined:
    Sep 2, 2008
    Posts:
    103
    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?
     
  4. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    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.
     
  5. Hanankk

    Hanankk

    Joined:
    Sep 2, 2008
    Posts:
    103
    Code (csharp):
    1. function LateUpdate () {
    2.  
    3. if(ricketHi== true){
    4. speed =ball.rigidbody.velocity.magnitude;
    5. print(speed);
    6. ricketHit = false;}
    7.  
    8. }
    9.  
    10. function OnCollisionEnter(collision : Collision) {
    11. switch (ricket){
    12.    case ricket1:
    13. collision.gameObject.rigidbody.velocity = Vector3.zero;
    14. collision.gameObject.rigidbody.AddRelativeForce(direction*forceAmount);
    15. ricketHit = true;
    16. }}
    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 !!!
     
  6. Jim Offerman

    Jim Offerman

    Joined:
    Jul 17, 2009
    Posts:
    177
    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.
     
  7. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    Well, there's a typo on the LateUpdate function.

    ricketHi== true should be ricketHit == true.
     
  8. Hanankk

    Hanankk

    Joined:
    Sep 2, 2008
    Posts:
    103
    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?
     
  9. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    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.
     
  10. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    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):
    1. collision.gameObject.rigidbody.AddForce(Vector3.zero, ForceMode.VelocityChange);
    2. collision.gameObject.rigidbody.AddRelativeForce(direction*forceAmount);
    ...to get the result you want.
     
  11. Hanankk

    Hanankk

    Joined:
    Sep 2, 2008
    Posts:
    103
    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):
    1.  
    2.  function OnCollisionEnter(collision : Collision) {
    3. switch (ricket){
    4.    case ricket1:
    5. collision.gameObject.rigidbody.velocity = Vector3.zero;
    6. collision.gameObject.rigidbody.AddRelativeForce(direction*forceAmount);
    7. yield WaitForSeconds (0.02);
    8. speed =collision.rigidbody.velocity.magnitude;
    9. print(speed);
    10. }}
    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??