Search Unity

fly away in hit direction

Discussion in 'Editor & General Support' started by yogeez, Oct 23, 2009.

  1. yogeez

    yogeez

    Joined:
    Mar 4, 2009
    Posts:
    129
    hello all,
    working on character hit to an object.
    I searched the thing i need in forums but still couldn't get the right idea about that.

    Now in here the character is having punch animation. And in the punch i have added the collider. Now a rigidbody cube is there on the floor. So when reaching towards cube and punching the cube is moved and reacting properly regarding the place its got hit and the direction its thrown too.

    But than again needed the force (like thrown away with speed) to work on it in the same way its got hit. And i have attached script for detection of the punch on the cube itself.
    like finding true or false for hit(for work under fixed update other wise the cube will directly start moving by itself).
    So,if it gets hit than the cube should move in that direction like thrown away as in the direction its hit but with full speed.

    This is the script on the cube for detection and applying force

    forceCheck.js

    Code (csharp):
    1.  
    2. var hitted : boolean;
    3.  
    4. function FixedUpdate()
    5. {
    6.    
    7.     if(hitted == true)
    8.     {
    9.         var dir = transform.TransformDirection(Vector3.forward);       
    10.         constantForce.relativeForce = dir * 500;       
    11.     }
    12. }
    13.  
    14. function OnCollisionEnter(colliInfo : Collision)
    15. {
    16.     if(colliInfo.gameObject.tag == "punchHit")
    17.     {
    18.         hitted = true;
    19.     }
    20. }
    21.  
    22.  
    Also i have used freeze rotation on cube.
    And so the above code the cube is thrown away fast but in only single direction, no matter where or on which side the cube gets hit.

    If i don't use freeze rotation than the cube is floating everywhere or if the forward pull is towards the downward direction than it acts like a magnet being stuck on the floor.

    i think its may be somewhere near the problem. Or there is some other clear method.
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    If you know the position and direction of the punch force, then you can use Rigidbody.AddForceAtPosition to make the cube move realistically:-
    Code (csharp):
    1. cube.rigidbody.AddForceAtPosition(punchPosition, punchForceVector);
     
  3. yogeez

    yogeez

    Joined:
    Mar 4, 2009
    Posts:
    129
    Actually, want the cube to perform the action according to the position and velocity of force it has got it.
    And should move in the direction as per the force of hit get effect on it.
     
  4. yogeez

    yogeez

    Joined:
    Mar 4, 2009
    Posts:
    129
    It didn't worked.
    As per my way i done i think its somewhere near what i had done, but still looking for a right manner to achieve the effect.
     
  5. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Well, the line
    Code (csharp):
    1. var dir = transform.TransformDirection(Vector3.forward);
    ..calculates the direction of the force as the object's forward direction. However, if you have freezeRotation enabled, then the object can't rotate and so its forward direction will always be the same. If you don't want the effect of rigidbody.AddForceAtPosition, then rigidbody.AddForce may be what you need here.