创建刚体与发射销毁

void Start()

{

    Rigidbody rb = GetComponent<Rigidbody>();

    float mass = rb.mass;//质量

    float drap = rb.drap;//空气阻力

    rb.angularDrag;//角阻力

    rb.useGravity;//当前游戏对象是否受到重力影响

    rb.isKinematic;//是否使用运动学

    rb.freezeRotation;//是否冻结游戏对象的旋转

}


给物体添加力

private Rigidbody r;

void Start()

{

    r = GetComponent<RIgidbody>();

}

void Update()

{

    r.AddForce(new Vector3(0f,10f,0f));//给当前对象施加一个力

    r.AddTorque(new Vector3(0f,10f,0f));//给当前对象添加力矩/扭矩,扭矩可以使物体旋转

    r.AddForceAtPosition(new Vector3(0f,10f,0f),new Vector3(0.5f,0.5f,0.5f));//给对象在指定的位置上添加一个力

    r.AddExplosionForce(1000f.Vector3.zero,4f);//在指定位置添加一个爆炸力

}

碰撞

void OnCollisionEnter(Collision other)//碰撞开始时会调用一次

void OnCollisionStay(Collision other)//碰撞持续发生时的时候调用

void OnCollisionExit(Collision other)//碰撞结束时调用一次

触发is trigger

void OnTriggerEnter(Collider other)//刚刚进入触发范围时调用一次

{

    GameObject.Destroy(gameObject,0.3f);//Desrtroy销毁

}

void OnTriggerStay(Collider other)//持续在触发范围内会一直调用

void OnTriggerExit(Collider other)//离开触发范围时会调用一次


条件

1.两者必须都有碰撞(Collider)

2.移动的物体必须有刚体;

3.被触发的物体必须勾选is trigger

4.触发脚本必须给到被触发的物体上

创建刚体并发射

public GameObject prefab;//产生的物体

public Transform pos;//默认位置

void Update()

{

    GameObject clone;//所克隆承接的一个prefab;

    clone = GameObject.Instantiate(prefab,pos.position,Quaternion.identity) as GameObject;

    //实例化   物体   位置   默认   角度   强制转换成GameObject类型

    clone.GetComponent<Rigidbody>().AddForce(pos.up * 1000 * Tiem.deltaTime);

    //获取克隆出来的物体   添加刚体   添加力   轴向   力度   time类赋予速度乘以匀速值

    //Time.deltaTime平均值 是0.02;Time.time是以0.02的值递增

}

销毁

Destroy(gameObject);//第一种方法,直接拉入物体

Destory(GameObject.Find("Cube"));//第二种方法,找到物体名称

Destory(GetComponent<Rigidbody>());//也可以直接销毁物体的各种组件

Destory(GetComponent<代码名>());//销毁物体的代码(把代码拖入物体,获取代码名)

Destory(gameObject,3);//物体3秒后消失

定时销毁

public float temer;//自定义时间

void Update()

{

    timer -= Time.deltaTime;

    if(timer <= 0)

    {

        Destory(gameObject);

    }

}

实例化10个物体,每个都间隔2各单位

public Transform prefab;

void Start()

{

    int i = 0;

    while(i < 10)

    {

        GameObject.instantiate(prefab,new Vector3(i * 2f,0,0),Quaternion.identity);

        i++;

    }

}

实例化10个物体,每个都间隔2各单位,并且每个物体X轴旋转45°

public Transform prefab;

void Start()

{

    int i = 0;

    while(i < 10)

    {

        GameObject.instantiate(prefab,new Vector3(i * 2f,0,0),Quaternion.Euler(45f,0,0));//Eular欧拉角

        i++;

    }

}

创建网格

public GameObject prefab;

public float girdx = 5f;//横向

public float girdy = 5f;//纵向

public float spacing = 2f;//物体间隔

void Start()

{

    for(int y = 0 ;y < girdy ;y++)

    {

        for(int x = 0 ;x < girdx ;x++)

        {

            Vector3 pos = new Vector3(x,0,y) * spacing;

            Instantiate(prefab,pos,Quaternion.identity);

        }

    }

}

创建5 * 5的墙

public float x1 = 5f;

public float y1 = 5f;

void Start()

{

    for(int y = 0 ;y < y1 ; y++)

    {

        for(int x = 0 ;x < x1 ;x++)

        {

            GameObject bj = GameOject.CreatePrimitive(PrimitiveType.Cube);//创建一个cube

            bj.transform.position = new Vector3(0,0 + y,0 + x);

        }

    }

}

生成一定数量的物体,形成一个圆

public GameObject prefab;

public int number = 20;//数量

public float radius = 5f;//半径

void Start()

{

    for(int i = 0 ;i < number l;i++)

    {

        float angle = i * Mathf.PI * 2 / number;//Mathf.PI数学里面的圆周率

        Vector3 pos = new Vector3(Mathf.Cos(angle),0,Mathf.Sin(angle)) * radius;

        Instantiate(prefab,pos,Quaternion.indentity);

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值