Arrow stab into tree (and stay)

Hello :) I need help, if you would, to create a script that once the arrow has been launched from the bow, if it hits an object it would stab itself in and stay there even if the object, say a person, moves. I have it launching and everything, but I just need help so it knows once its hit and sticks. Thank you :)


private var hitTarget : boolean = false; //did it hit the target variable

function Update () {
    if (hitTarget == false) { //check to see it its already stuck in something
        var rot: Quaternion = transform.rotation;
        rot.SetLookRotation (rigidbody.velocity);
        transform.rotation = rot;
        print("not hit"); //for debug

function OnCollisionEnter(theTarget:Collision){
   gameObject.transform.parent = theTarget.gameObject;
   hitTarget = true;
   print("hit"); //for debug

You do not give much information, so I don't feel like giving much either ;) But a quick answer could be to use OnCollisionEnter() to detect collision and then makes the arrow a child Transform of the "target" GameObject. In JS, it could look like

function OnCollisionEnter(theTarget:Collision){
   gameObject.transform.parent = theTarget.gameObject**.transform**;

that, off the top of my head. Should make the impacting object 'stick' to whatever it hits, however it hit.

EDIT: corrected the code, we have to reach for theTarget.gameObject.TRANSFORM. Final code works on my side of the internet ;)

Add this script to your arrow and everything will work. I don’t need to explain it because it’s very simple!

var projectileCopy : GameObject;
static var initialSpeed = 0.0;
function OnCollisionEnter (other : Collision)
  var instantiatedProjectile : GameObject = Instantiate (projectileCopy,
   transform.position, transform.rotation);