物体变成其他物体的子物体

来源:互联网 发布:淘宝口令红包 编辑:程序博客网 时间:2024/04/28 12:19

var attach : boolean;
var player : Transform;
var shootspeed : float=10;

function Start()
{
   collider.isTrigger = false;
};

function OnCollisionEnter(collision : Collision)
{
   if (collision.gameObject.tag=="Player")
     {
        attach=true;
        player=collision.transform;
        if (rigidbody)   Destroy (rigidbody);
   transform.parent=player;                                                        //物体变成其他物体的子物体
transform.localPosition=Vector3(0,-0.65,5);
     };
};

function Update () 
{
   if(Input.GetKeyDown(KeyCode.F))
     {
     if (transform.parent)
    {
       transform.parent=null;
       gameObject.AddComponent ("Rigidbody");
    rigidbody.velocity = rigidbody.velocity + player.TransformDirection(Vector3.forward)*shootspeed;
    attach=false;
    };
};
   
}