Unity3D基础教程4-1:在运行时初始化预制品

2014-08-14 19:47:13|?次阅读|上传:huigezrx【已有?条评论】发表评论

关键词:Unity3D, 游戏, 虚拟现实|来源:唯设编程网

一个更好的办法是立即删除整个角色并替换它为一个破坏的预制品实例。这使你具有很大的灵活性。你可以使用死亡角色的不同的材料,附加完全不同的脚本,产生一个含有模拟一个支离破碎的敌人的预制品,或者仅仅是实例化一个含有小块的角色的预制品。

Any of these options can be achieved with a single call to Instantiate(), you just have to hook it up to the right prefab and you're set!

任何这些问题可以通过一个调用Instantiate()取得,你只要把它挂到右侧预制品上和你设定的选项上!

The important part to remember is that the wreck which you Instantiate() can be made of completely different objects than the original. For example, if you have an airplane, you would model two versions. One where the plane consists of a single GameObject with Mesh Renderer and scripts for airplane physics. By keeping the model in just one GameObject, your game will run faster since you will be able to make the model with less triangles and since it consists of fewer objects it will render faster than using many small parts. Also while your plane is happily flying around there is no reason to have it in separate parts.

最重要的部分要记住的是那个你Instantiate()由完全不同的对象组成的失事的飞船。例如,如果你有一架飞机,你会创建两个版本。那架飞机的一个组成一个网渲染的独立GameObject的飞机及一个物理脚本飞机。通过保持在只有一个GameObject的模式,因为你将会能够制造与较少的三角形的模型而且自从它之后它将会描绘的较少的物件,你的游戏将会运行得快速,超过使用许多小的部份。也当你的飞机在附近快乐飞的时候,没有理由要在分开的部份中有它。

To build a wrecked airplane Prefab, the typical steps are:

建立一架被破坏的飞机的预制品,典型的步骤是:

  1. Model your airplane with lots of different parts in your favorite modeler  在你喜欢的模型里,利用不同的部分模拟的你飞机。
  2. Create an empty Scene  建立一个空白场景
  3. Drag the model into the empty Scene  拖动模型到这个空白场景
  4. Add Rigidbodies to all parts, by selecting all the parts and choosing  Component->Physics->Rigidbody

添加刚体到所有部分,通过选取所有部分和选择Component->Physics->Rigidbody

  1. Add Box Colliders to all parts by selecting all the parts and choosing Component->Physics->Box Collider

添加碰撞器盒到所有选取的部分和选择Component->Physics->Box Collider

  1. For an extra special effect, add a smoke-like Particle System as a child GameObject to each of the parts  对于一个额外的效果,添加一个烟雾模拟粒子系统作为子GameObject到每个部分。
  2. Now you have an airplane with multiple exploded parts, they fall to the ground by physics and will create a Particle trail due to the attached particle system. Hit Play to preview how your model reacts and do any necessary tweaks.

现在你拥有了一个含有多个爆破部分的飞机,它们堕落物理的地上而且附加的粒子系统将产生一个粒子痕迹。

  1. Choose Assets->Create Prefab  选择Assets->Create Prefab
  2. Drag the root GameObject containing all the airplane parts into the Prefab

拖动包含所有飞机部件根的GameObject到预制品里。

var wreck : GameObject;

// As an example, we turn the game object into a wreck after 3 seconds automatically

function Start () {

    yield WaitForSeconds(3);

    KillSelf();

}

// Calls the fire method when holding down ctrl or mouse

function KillSelf () {

    // Instantiate the wreck game object at the same position we are at

    var wreckClone = Instantiate(wreck, transform.position, transform.rotation);

    // Sometimes we need to carry over some variables from this object

    // to the wreck

    wreckClone.GetComponent(MyScript).someVariable = GetComponent(MyScript).someVariable;

    // Kill ourselves

    Destroy(gameObject);

}

The First Person Shooter tutorial explains how to replace a character with a ragdoll version and also synchronize limbs with the last state of the animation. You can find that tutorial on the Tutorials page.

发表评论0条 】
网友评论(共?条评论)..
Unity3D基础教程4-1:在运行时初始化预制品