12 第1页 | 共2 页下一页
返回列表 发新帖
查看: 2836|回复: 11
打印 上一主题 下一主题

实例化游戏对象(下)

[复制链接]

5552

主题

2

听众

8万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
11

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2011-12-7 15:20:15 |只看该作者 |倒序浏览
In this tutorial we will be looking at an important concept of games development; instantiation. The process of instantiation or instancing as it is also known, is where you load or create objects at runtime through scripts.


Introduction

Normally, when you want an object to appear in a scene or level you place it from the library into the scene and position it in 3d space. This is useful for static objects or objects that always appear in a specific spot. However in your average game, there are many things that do not appear in a specific spot, all the time.
If we think about what objects appear in a game that are instanced (i.e. created dynamically through code or "spawned") here are just a few examples:
■Monsters / Creatures / Enemies

■NPC characters are often instanced

■Player characters are also often instanced

■Quest items in RPG's

■Rewards - such as rings in Sonic games or chests in RPG's

■Components of puzzle games

So in summary, instantiation is simply the process of dynamically spawning an object as opposed to it being manually placed in a scene. As you can probably imagine, this topic can get very long and incredibly complicated when you consider different types of game and how the implementations of instantiation could be formed.
Therefore we will start with some very basic technical examples and then move onto more practical implementations.


The Instantiate Function (the basic, basics)

Fortunately Unity has a function built in called "Instantiate()", which makes instantiation easy to get to grips with, where things get complex is getting the finer details or specific implementations of a mechanic off the ground. For example, creating spawn areas where monsters appear and respawn on timers randomly within the defined region of a scene against specific rules such... think of how monsters appear and reappear after death in an MMORPG... it's when your implementation starts to take a practcal approach like that, when things get truely complicated.
Needless to say, we won't be doing that in this tutorial, but we will be going over the foundations of instantiation so that you can at least have an idea in your head about how you create such mechanics... and maybe even be able to implement such mechanics with a little research. We might create a tutorial on that kind of implementation but its early days for Unity Labs yet.
Anyway, back on topic... the Instantiate function itself. In its most basic form, the Instantiate function takes three parameters and looks like this:
Instantiate(object, position, rotation);
object is the prefab you want to spawn (i.e. a treasure chest)

position is the position in 3d space you want it to appear (i.e. x: 300, y: 1, z: 250)

rotation is the direction you want it to be facing
A note on the object to pass, this is done through the use of a variable which will hold the object (a public member variable). If you have done a few other tutorials, such as your Game Start Screen tutorial, you will have encountered a scenario where we created a variable at the top of the script which we used to reference a GUISkin. We reference the object to spawn in much the same way, by declaring a variable as a GameObject or a RigidBody (depending on the object we want to spawn) and assigning an object to that variable.
var treasureChest : GameObject;
Instantiate(treasureChest, position, rotation);
Instantiating a normal game object in code would look something like the example above. We define a variable (i.e. treasureChest) as a GameObject which is passed to the Instantiate function. For it to work we would have to assign an object to the variable first, but thats roughtly how it works.
var rigidBodyObject : Rigidbody;
Instantiate(rigidBodyObject, position, rotation);
The example above would set the variable to a Rigid Body type, so only Rigid Body objects could be passed to the function.
With passing the object to the function explained, lets quickly explain the position and rotation, we tell Unity where to position the object and its rotation using a Vector3(x, y, z) function, which is a mathematical functon for defining coordinates in 3D space.
var treasureChest : GameObject;
Instantiate(treasureChest, Vector3(0, 5, 10), Vector3(0, 0, 10));
In the example above we spawned a treasure chest object at position X: 0, Y: 5, Z: 10 with a rotation of X: 0, Y: 0, Z: 10.
You also have to consider local space and world space:
World Space is where you reference co-ordinates based on the entire scene (i.e. x,y,z position from the 0,0,0 point in the scene)

Local Space is where you reference co-ordinates based on another object such as the player character (i.e. x, y, z position from the character)
I hope that all makes sense, if not the technical implementations in part 2 should help.
In part 2 of our Instantiation tutorial we will be spawning objects in a simple scene, this selection of technical examples should show you various ways in which you can instance objects in your game world.
So to start off, lets create a new project called "Instantiation Tutorial 1", include the standard assets package. Then go to "GameObject -> CreateEmpty" to create an empty game object which you can see in the Hierarchy. Then rename that object to StartUp.
Now in the Project Library select "Create -> JavaScript" and call the new script "Instances". Then select the StartUp game object in the Hierarchy and go to "Component -> Scripts" and select the Instances script to assign the script to the StartUp object, causing the script to execute when we play the game automatically.
Now create a terrain, paint a grass texture and place the FPS controller from the Prefabs folder in the standard assets package into the scene so you can walk around.
Now we will start to instantiate objects in this scene.


Basic Implementation

Lets create a real basic implementation to start, we're going to create a primitive object (a cube) and drop it in the scene using code:
function Start ()

{

    var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);

}
This simple block of code in the start function will create a cube which you should see plop down on your terrain.
Lets make this one a bit more interesting:

function Start ()

{

    var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);

    cube.AddComponent(Rigidbody);

    cube.transform.position = Vector3 (42, 4, 37);
    var cubeb = GameObject.CreatePrimitive(PrimitiveType.Cube);

    cubeb.AddComponent(Rigidbody);

    cubeb.transform.position = Vector3 (42, 4, 37);
    var cubec = GameObject.CreatePrimitive(PrimitiveType.Cube);

    cubec.AddComponent(Rigidbody);

    cubec.transform.position = Vector3 (42, 4, 37);

}
What we have done above is create 3 different isntances of the cube, except this time we have given them a Rigid Body component and controlled their position so they drop on top of one another.
You might want to position your camera or player controller so that you can see 42, 4, 37 on startup, you will see the cubes fall onto eachother and bounce. They are bouncing as they have a rigid body component, which makes them react to eachother (collision detection at work there).
由 u8 发表
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2012-2-17 23:28:33 |只看该作者
水。。。
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2012-3-22 23:19:13 |只看该作者
既来之,则看之!
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

地板
发表于 2012-5-9 23:28:02 |只看该作者
好,真棒!!
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

5#
发表于 2012-5-17 23:21:34 |只看该作者
爱咋咋地!
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

6#
发表于 2012-6-4 23:24:59 |只看该作者
凡系斑竹滴话要听;凡系朋友滴帖要顶!
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

7#
发表于 2012-7-2 23:22:10 |只看该作者
不错 非常经典  实用
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

8#
发表于 2012-7-23 23:26:12 |只看该作者
路过、路过、快到鸟,列位请继续...ing
回复

使用道具 举报

3795

主题

2

听众

5万

积分

版主

Rank: 7Rank: 7Rank: 7

纳金币
53202
精华
32

活跃会员 优秀版主 推广达人 突出贡献 荣誉管理 论坛元老

9#
发表于 2012-7-24 09:29:41 |只看该作者

高手速来赐教!kismet里面怎么加一个延迟DELAY??



unity特效大家PP_武功盖世呀








【严重分享】Unity3D_3.0破解版[安装包][下载]






高手请赐教!用模型做的特效动画材质丢失








antares_universe(vizio)可视化编辑
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

10#
发表于 2012-8-5 23:38:27 |只看该作者
好可爱的字,学习了
回复

使用道具 举报

12 第1页 | 共2 页下一页
返回列表 发新帖
您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2024-11-23 20:26 , Processed in 0.094953 second(s), 33 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部