Search Unity

Pushing Nearby Objects Directly Away

Discussion in 'Editor & General Support' started by Nomad_Artisan, Sep 30, 2013.

  1. Nomad_Artisan

    Nomad_Artisan

    Joined:
    Jun 29, 2013
    Posts:
    20
    Here's what I'm trying to do.

    Thematically, I'm trying to get a force pulsing outward from the player that grows to its max size, applying a force to any object it comes into contact with while it's growing.

    I'm currently trying to do this by having a button press instantiate a game object with a capsule collider and the following script.

    Code (csharp):
    1.  
    2. var maxSize         : float = 2.0;
    3. var growthRate      : float = 2.0;
    4. var scale           : float = 0.1;
    5.  
    6. var atMaxSize       : boolean = true;
    7.  
    8. function Update ()
    9. {
    10.     if (atMaxSize == true)
    11.     {
    12.         collider.radius = scale;
    13.         scale += growthRate * Time.deltaTime;
    14.         if(scale > maxSize) atMaxSize = false;
    15.        
    16.     }
    17.     else if (atMaxSize == false)
    18.     {
    19.         Destroy(gameObject);
    20.     }
    21. }
    22.  
    23. function OnTriggerEnter (other : Collider)
    24. {
    25.    
    26.     if(other.tag == "block")
    27.     {
    28.         print("Object hit");
    29.         other.rigidbody.AddForce(Vector3(other.transform.position.x - transform.position.x, 0, other.transform.position.y - transform.position.y) * 1500);
    30.     }
    31. }
    32.    
    33.  
    34.  
    So far, the object is instaniated in scene to the player's position by a different script. Then this script causes the object's capsule collider to grow until it reaches a radius of 2 and then is destroyed.

    What is getting me is that sometimes the print("Object hit") fires, and even less consistently will a force be added to the other collider.

    Is it possible that the radius of the collider grows past another object and so never catchs the onTriggerEnter call?

    How can I get this to consistently apply force on other colliders directly away form this object as it's collider grows into them?

    Thanks for looking!