Search Unity

Attach to collision parent!

Discussion in 'Scripting' started by draxi, Nov 10, 2009.

  1. draxi

    draxi

    Joined:
    Nov 10, 2009
    Posts:
    23
    Hi!
    I've been trying to attach a clone projectile to the gameobject it hits, but I can't seem to get it right..
    Anyone got any tips?
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Hi, welcome to the forum!

    Can you describe what you have tried so far?
     
  3. draxi

    draxi

    Joined:
    Nov 10, 2009
    Posts:
    23
    Yeh sure!
    I tried like:
    transform = collision.parent - and stuff like that.
    But that didn't work, I think I have to get the name of the gameobject it collide with some how.

    I found the Script Reference for parent, http://unity3d.com/support/documentation/ScriptReference/Transform-parent.html, and tried to change it to work the way I wanted it, but I'm not sure if I did it right, or if it's just that you can't use the Collision to name the other object.


    This is my code so far, it's not working because I get some error messages, but I hope you see what I'm trying to do..


    function OnCollisionEnter (collision : Collision) {

    // stop the arrow
    rigidbody.useGravity = false;
    rigidbody.Sleep();
    rigidbody.drag = 10000;
    rotation = false;
    rigidbody.freezeRotation = true;


    var CollObject = Collision.gameObject;
    this.parent = CollObject;



    }
     
  4. draxi

    draxi

    Joined:
    Nov 10, 2009
    Posts:
    23
    Ok, so I think I know what I have to do..
    I have to make the target instantiate a clone of the arrow that hits, and destroy the original arrow..


    So, I'm wondering, what have I done wrong here?
    Or in other words, what should I do to make it work?

    Code (csharp):
    1. var arrow : Rigidbody;
    2. var arrowClone : Rigidbody;
    3.  
    4.  
    5. function OnCollisionEnter (collision : Rigidbody)  {   
    6.     if (collision==arrow) {
    7.         var clone = Instantiate(arrowClone,arrow.transform.position,arrow.transform.rotation);
    8.         clone.parent = transform;
    9.     }
    10. }
     
  5. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Which object is this script attached to? I imagine it would have to be attached to the arrow to get the clone in the right place, but you refer to the arrow with a variable as if it were another object.
     
  6. draxi

    draxi

    Joined:
    Nov 10, 2009
    Posts:
    23
    I tried attaching the script to the target, because it seems like I need to to make the arrow a child of the target, so that it moves with it!
    Like, if the target tiped over after the arrow hit, it will look weird if the arrow stays in mid-air!