位置: 编程技术 - 正文

以游戏实例介绍Unity3d(如何写出一篇游戏活动案例)

编辑:rootadmin
Intro to UnityThis instructable is aimed at super beginners!You will need to install Unity 4.x (Free or Pro). up Unity and create a New Project. Don't worry about importing things yet!Step 1: This is UNITY!Learn about the different Unity Windows are here: set our layout to the Default layout.Go to the Menu Bar and click on Window > Layouts > DefaultMenu BarObject Manipulation Tools (Gizmo): Use this to pan the view and move, rotate, and resize objects.Pivot Setting: This changes the location of the Gizmo to either be at the object's pivot point or the object's center.Coordinate Settings: Use this to change whether to move an object in the global coordinate system or its local coordinate system (based on the object's rotation).In-Editor Game Manipulation Tools: Use these to start/stop, pause, and step the game.Layers: Change what layers are visible in your scene in case you want to work only on one set of objects.Layout Menu: Change your current window layout.Hierarchy Window: Game objects in here are part of the current scene. Manipulate them here.Scene / Game / Animator viewer: This is where you view your game. Navigate this by using right click with WASD or the mouse to pan/zoom/tilt.Gizmo: This is a Gizmo. Use this to move, rotate, and resize objects based on the selected tool.Inspector Window: This is where you change settings for a selected object.Project Window: This is where all the assets for the project are stored.Console Window: Errors, warnings, and print messages will all appear here. Use this to debug.Message Bar: Whenever you have an error, warning, or message, it will show up down here. Go to the console to fix all your bugs.Step 2: Importing PackagesWe're going to start by importing some of Unity's Standard Assets.In the Menu Bar, look under the Assets tab to Import some packages.Assets > Import Package > Terrain AssetsAssets > Import Package > Character ControllerStep 3: TerrainLet's add some mountains!In the Menu Bar go to GameObject > Create Other > TerrainSelect the terrain object by clicking on it in the Scene or in the object list in the Hierarchy window.Click the Raise/Lower Terrain button in the inspector panel.

推荐整理分享以游戏实例介绍Unity3d(如何写出一篇游戏活动案例),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:撰写一个游戏案例,游戏的例子,以游戏实例介绍自己,以游戏实例介绍自己,以游戏实例介绍一种事物,以游戏实例介绍一种事物,以游戏实例介绍自己,以游戏实例介绍中国文化,内容如对您有帮助,希望把文章链接给更多的朋友!

Left click and drag anywhere on the terrain in the Scene to raise the elevation.Hold down shift to lower.Experiment with different brush shapes and sizes.

Adding TexturesNow select the Paint texture tool in the Inspector.Click the Edit Textures button under the Textures heading and select Add Texture.

In the window that appears click select Select in the box labelled Texture.Double click Grass (Hill) in the window that appears.Click Add at the bottom of the Add Terrain texture Window.

Add a few more textures using the same method.Texture PaintingWith the terrain and Paint Texture tool still selected select a texture other than grass.Now, just as with the height modification, click and drag in the viewport to change the texture of the terrain.

Move your Main CameraFind your camera by double clicking the Main Camera object in the list of objects on the left side of the screen under Hierarchy.

Enable the movement tool by clicking on the 4-directional arrow in the upper left.

You can move objects by dragging the arrows of the tool.Move your Main Camera object so that the Camera Preview shows something interesting.Step 4: LightsGame Object > Create Other > Directional LightTry changing the Intensity of the light or play around with the direction and location.

Let's see what the player is looking at!Now you can see things!Step 5: PlayerLocate the First Person Player Controller asset in the Project window.Assets > Standard Assets > Character Controllers > First Person Controller

Add a player to the scene by dragging and dropping it into the Hierarchy window. Rename it to Player by changing the name in the Inspector Window.Note: if you do not press enter/return after renaming it, the name will not change. Try positioning the Player on top of the terrain.Make sure the Player isn't intersecting terrain, otherwise you'll fall through.Delete Main Camera by right clicking on it's name in the Hierarchy and pressing Delete.Press play. You should be able to walk around now :)Step 6: Scene ObjectsLet's get a nice building for free! in Unity. Download! Create account or log in as needed.Import all the assets.Assets > Import Package > SkyboxesCheck only the files relating to DawnDusk

Edit > Render Settings > Skybox Material

Drag and drop the prefabs of the models into your scene.Step 7: Lock/Hide CursorCreate Scripts FolderMake a new Javascript Script called "Menu."We'll start by adding the most basic Menu element for a first person shooter: locking and hiding the mouse when in game.// True if the menu is open and mouse is unlockedvarMenuOpen:boolean=false;functionStart(){ UpdateCursorLock();}functionUpdate(){ // Check whether the menu button was released if(Input.GetButtonUp("MenuOpen")){ MenuOpen=!MenuOpen; UpdateCursorLock(); }}// Called each time the Gui needs to be drawnfunctionOnGUI(){ if(!MenuOpen){ // Draw the crosshair // Center the text inside the label varcenteredStyle=GUI.skin.GetStyle("Label"); centeredStyle.alignment=TextAnchor.MiddleCenter; // Draw the label at the center of the screen GUI.Label(Rect(Screen.width/2-,Screen.height/2-,,),"&#;",centeredStyle); }}functionUpdateCursorLock(){ Screen.lockCursor=!MenuOpen; Screen.showCursor=MenuOpen;}To get the Menu on the scene, let's create an Empty Game Object (GameObject > Create Empty).Let's call this "Menu."Drag the script on to the Menu object.We'll add more functionality to this later.Let's add a Menu Open / Close buttonEdit > Project Settings > Input.Open the "Axes."Under the Size in the Input Manager, edit the size to be .Rename one of the new Inputs to be called "Menu Open" and inside it, set the "Positive Button" to be "escape"Step 8: Guns and BulletsGunLet's try out this gun: in DartGun Prefab from Compressed Gas Pistol > Prefab > DartGun to the scene.A prefab is like a Class but in 'object' form. It's the copy with good defaults that you can use for all your different levels.Put DartGun in Player > Main Camera

Position/Rotate DartGun (Player > Main Camera > DartGun) according to the values below:

Position/Rotate inner DartGun (Player > Main Camera > DartGun > DartGun) according to the values below:

Let's create and shoot some bullets!Create Sphere by going to GameObject > Create Other > SphereRename the object to BulletAdd a new RigidBody to the Bullet object.Click Add Component > Physics > RigidBody

Uncheck Use GravitySet Collision Detection to Continuous

以游戏实例介绍Unity3d(如何写出一篇游戏活动案例)

Set the scale of the sphere's transform to 0.2, 0.2, 0.2

Let's create a new javascript Script called "Bullet."// The speed the bullet movesvarSpeed:float=.4;// The number of seconds before the bullet is automatically destroyedvarSecondsUntilDestroy:float=;privatevarstartTime:float;functionStart(){ startTime=Time.time;}functionFixedUpdate(){ // Move forward this.gameObject.transform.position&#;=Speed*this.gameObject.transform.forward; // If the Bullet has existed as long as SecondsUntilDestroy, destroy it if(Time.time-startTime>=SecondsUntilDestroy){ Destroy(this.gameObject); }} functionOnCollisionEnter(collision:Collision){ // Remove the Bullet from the world Destroy(this.gameObject);}Drag and drop the Bullet script from the Project Window (Assets > Scripts) to the Bullet Object in the Hierarchy Window.Finally, let's make the Bullet into a prefab and delete it from the scene.Create a prefab of the bullet by dragging the Bullet object into the Project window to the Assets > Prefabs in the Hierarchy Window.Let's create a new javascript Script called "Gun." // This is the bullet prefab the will be instantiated when the player clicks// It must be set to an object in the editor varBullet:GameObject;// Fire a bulletfunctionFire(){ // Create a new bullet pointing in the same direction as the gun varnewBullet:GameObject=Instantiate(Bullet,transform.position,transform.rotation);}functionUpdate(){ // Fire if the left mouse button is clicked if(Input.GetButtonDown("Fire1")){ Fire(); }}Drag and drop the Gun script from the Project Window (Assets > Scripts) to the Gun Object in the Hierarchy Window.Drag and drop the Bullet Prefab into the Gun script where it says "None (Game Object)."Remember to always apply changes to the prefab by going to the scene object > inspector window > prefab > apply.Let's make sure the Player doesn't get shot!Layers > Edit Layers

Create Bullet LayerCreate Player Layer

Select the Bullet object and set its layer to Bullet.

Select the Player and set to Player layer.Say yes to changing the children.Edit > Project Settings > PhysicsUncheck Bullet/Player and Bullet/BulletStep 9: AIYou will need to download the following Zombie: yes to viewing the animation.Click DownloadClick Sign UpCreate AccountClick Download againClick CheckoutSelect FBX for Unity for Download FormatClick DownloadCreate Models Folder in the Projects window > AssetsDrag and Drop the Zombie into the Models FolderIf a warning window pops up when importing the model click Fix Now.GameObject > Create EmptyRename the GameObject to ZombieDrag the Zombie model onto the Zombie Object

Put a Character Controller on the Zombie ObjectIn the inspector set the Center Y to 1Add a Capsule Collider componentAdd Component > Physics > Capsule Collider** Make sure to copy the exact values over.

Create a new script named Zombie in your scripts folder.varVisionDistance:float=;varMovementSpeed:float=2;varHealth:int=2;functionFixedUpdate(){ // Get the Player object varplayer:GameObject=GameObject.Find("Player"); varcharacterController:CharacterController=GetComponent(CharacterController); // Get the position of the Zombie's eyes vareyePosition:Vector3=transform.position; eyePosition.y&#;=characterController.height; // Get the difference between the player and the Zombie positions // This creates a direction vector pointing in the direction of the Player. varlookDirection=player.transform.position-eyePosition; lookDirection=lookDirection.normalized; // Only look for the player or objects that are part of the scenery (terrain, buildings, etc.) varlayerMask:int=1<<LayerMask.NameToLayer("Player")|1<<LayerMask.NameToLayer("Default"); // The direction the Zombie will move, defaults to standing still varmovementDirection:Vector3=Vector3.zero; // hitInfo will contain information about what the Zombie can see. varhitInfo:RaycastHit; if(Physics.Raycast(eyePosition,lookDirection,hitInfo,VisionDistance,layerMask)){ // If the Zombie can see the Player move toward them. if(hitInfo.collider.gameObject==player){ movementDirection=lookDirection; movementDirection.y=0; movementDirection=movementDirection.normalized; } } // Face and move in the chosen direction if(movementDirection!=Vector3.zero){ transform.rotation=Quaternion.LookRotation(movementDirection,Vector3.up); } characterController.SimpleMove(movementDirection*MovementSpeed);}Put script on the Zombie.Create Zombie Layer (Layers > Edit Layers)Put Zombie into Zombie Layer so that the Zombie can 'see through' other zombies.Drag and drop the Zombie into the Assets > Prefabs folder to create a prefab of it.Step : AnimationLocate the Zombie@walking_3 asset in the Models folder.The same way you made a script, this time, make an Animator Controller

Project Window > Right Click > Create > Animator Controller

Name it Zombie Animator.Now double click on the Zombie Animator.Now in the Project Window, locate the Zombie@walking_3 asset again.Open up the asset by clicking the arrow next to it.

Now drag and drop the walking_3 animation into the Animator Window.Connect the Any State to the walking_3 state by right clicking on Any State > Make Transition and then click on walking_3.First, let's make sure that the walking 3 loops correctly by editing the Transition arrow (click on the Transition arrow).

Make sure the walking animation doesn't over lap and the arrows on the timeline are next to each other.

Next, let's make sure the animation isn't super duper slow by editing the walking_3 animation inside the Animator window.Set the speed to around 3.4. Now in your scene, go to the Zombie and drag the animator controller on to the animator object.Uncheck Apply Root Motion and set the culling mode to Always Animate.

Finally, go to Prefab > Apply and apply the settings to the prefab. Make sure that the Zombie prefab now also has an animator controller!Test it out in the scene!Step : Spawning ZombiesLet's make a Spawner!Create a new empty GameObject.Name the object Spawner.Create a new script named Spawner and add the following code to The object to be spawnedvarSpawnObject:GameObject;// in secondsvarSpawnStartDelay:float=0;varSpawnRate:float=5.0;functionStart(){ InvokeRepeating("Spawn",SpawnStartDelay,SpawnRate);}// Spawn the SpawnObjectfunctionSpawn(){ Instantiate(SpawnObject,transform.position,transform.rotation);}

Add the script to the Spawner.With the Spawner selected drag and drop the Zombie prefab into the Spawn Object property of the Spawner script.

You may notice that this script is very similar to the Gun script.Make this into a prefab.Place some in your map!

Step : Stats and End ConditionLet's give the player some health, the zombies some health and attack power, and the gun some attack power.Add a new script called Player in the scripts folder.The following code will allow the Player to be damaged, and restart the game when the player is killed.// The number of times the Player can be damaged before the game restartsvarHealth:int=;// Minimum number of seconds between the player getting hurtvarDamageInvulnerabilityDelay:float=2;// Keeps track of the last time the Player was damagedprivatevarinvulnerabilityStartTime:float;functionStart(){ // Set initial value so the Player is temporarily invulnerable at spawn invulnerabilityStartTime=Time.time;}// Attempt to damage the player by the specified number of hit pointsfunctionHurt(damage:int){ // If we have waited at least as long as DamageInvulnerabilityDelay if(Time.time-invulnerabilityStartTime>=DamageInvulnerabilityDelay){ // Damage the Player Health-=damage; // Reset the invulnerability timer invulnerabilityStartTime=Time.time; } // If the Player has no health left if(Health<=0){ // Reload the level Application.LoadLevel(Application.loadedLevelName); }}

If the zombie collides with the player, hurt the player.

Add the following function to the Zombie script.// When the Zombie collides with somethingfunctionOnTriggerStay(other:Collider){ // Get the Player that the Zombie collided with, if any varplayer=other.gameObject.GetComponent(Player); // If it collided with something other than a Player player will be null if(player!=null){ // Subtract one from the Player's health player.Hurt(1); }}

If the bullet hits a zombie, hurt the zombie.Replace the OnCollisionEnter function in the Bullet script with the following

functionOnCollisionEnter(collision:Collision){ // Get the Zombie that the Bullet collided with, if any varzombie=collision.transform.gameObject.GetComponent(Zombie); // If it collided with something other than a Zombie zombie will be null if(zombie!=null){ // Subtract one from the Zombie's health zombie.Health--; // If the zombie is out of health remove them from the game if(zombie.Health<=0){ Destroy(collision.transform.gameObject); } } // Remove the Bullet from the world Destroy(this.gameObject);}Step : HUD and Menu systemNow let's improve our Menu script!Let's add Resume, Restart, and Quit buttons as well as something telling us how much health is left.Add both of these functions to the Menu script:functionDrawHUD(){ // Get the health from the player varplayer=GameObject.Find("Player"); varhealth=player.GetComponent(Player).Health; // Position varleft=; vartop=; varwidth=; varheight=; // Make a background box GUI.Box(Rect(left,top,width,height),"Health: "&#;health); DrawCrosshair();}// Draw the menufunctionDrawMenu(){ varcenterX=Screen.width/2; varcenterY=Screen.height/2; // location of the menu varmenuLeft=centerX-; varmenuTop=centerY-; varmenuWidth=; varmenuHeight=; varbuttonX=menuLeft&#;; varbuttonWidth=; varbuttonHeight=; varbuttonDist=;// distance between each button // Make a background box GUI.Box(Rect(menuLeft,menuTop,menuWidth,menuHeight),"Menu"); // Start / Resume if(GUI.Button(Rect(buttonX,menuTop&#;1*buttonDist,buttonWidth,buttonHeight),"Resume")){ MenuOpen=false; UpdateCursorLock(); } // Restart if(GUI.Button(Rect(buttonX,menuTop&#;2*buttonDist,buttonWidth,buttonHeight),"Restart")){ Application.LoadLevel(Application.loadedLevel); } // Quit (Only works in the Build. Does not work in the eidtor!) if(GUI.Button(Rect(buttonX,menuTop&#;3*buttonDist,buttonWidth,buttonHeight),"Quit")){ Application.Quit(); }}

Now replace the OnGUI function with:

// Called each time the Gui needs to be drawnfunctionOnGUI(){ if(!MenuOpen){ DrawHUD(); }else{ DrawMenu(); } DrawCrosshair();}

Enjoy visual feedback!

Step : Play Around and Have FunMake some more interesting Terrain.Make some arms and fingers for the gun. Rotate them backwards on shoot to emulate recoil!You could try, for instance, making the Zombie run to the player and then do gangnam style by downloading some animations:

Mono2.0 对C#闭包 与 donet 不同的实现导致Unity的Bug 及解决方案 转自

【Unity3d】注意C#的字符串拼接效率问题 现在项目中用的Log输出机制,都是使用StringBuilder进行字符串拼接的,那么为什么要使用StringBuilder进行字符串拼接呢?一开始是这样的:privatestringcontent;publicvo

【Unity3d】如何在Unity中动态载入Lightmapping 用Lightmapping的好处就不用多说了(渲染速度快又出效果),下面是解决方案一.首先,美术将A.Prefab放到场景中打光,渲出Lightmapping,假设生成的的是A_Lightmapping.ex

标签: 如何写出一篇游戏活动案例

本文链接地址:https://www.jiuchutong.com/biancheng/368415.html 转载请保留说明!

上一篇:【Unity3d】如何解决Unity3d在引用外部插件时报DLLNotFoundException的问题(unity怎么bake)

下一篇:Mono2.0 对C#闭包 与 donet 不同的实现导致Unity的Bug 及解决方案(c#使用mongodb)

  • 一般纳税人哪些可以开3%的发票吗
  • 免税收入怎么做账务处理
  • 汇票签收后可以撤回吗
  • 出口退税购入的商品进项税怎么处理
  • 注册公司工贸和商贸区别
  • 经纪代理代订机票电了发票如何入账
  • 保险公司收车船使用税吗
  • 计提坏账准备怎么理解
  • 进口设备的重置成本包括( )
  • 贷款应计利息会计分录
  • 收到货物记账凭证
  • 以库存商品抵偿债务
  • 固定资产转在建工程
  • 跨年的发票能红字冲销吗?
  • 加油站的印花税计税金额
  • 种香菇会赔钱吗
  • 关于抄税的详细介绍
  • 发出商品如何做分录
  • 所得税季报营业外收入怎么填
  • 投资回报期限
  • 月底财务为什么要关账,暂时开不了发票
  • 非盈利组织稳定吗
  • igfxhk.exe是什么进程
  • 埃热泽尔斯湖面上的波纹,拉脱维亚拉特加尔地区 (© Eaglewood Films/Nimia)
  • 进项税额转出会计分录账务处理
  • wordpress建网站详细教程
  • 现金流量表相关题目
  • thinkphp curl
  • htmlcss导航栏网页
  • php-fpm运行模式
  • php 验证类
  • 使用ajax实现页面分页
  • 库存现金盘亏盘盈
  • 待抵扣进项税额的账务处理
  • 本地住宿费能报销吗
  • 《中华人民共和国治安管理处罚法》
  • 企业营业税怎么征收
  • 个体工商户需要给员工交社保吗
  • 新增项目和延续项目
  • 营业利润是负数什么原因
  • 接收商业承兑汇票有风险吗
  • 固定资产到期出售 合同
  • 事业单位哪些收入要上交国库
  • 政府补助的房子叫什么
  • 什么是存货周转天数
  • 增值税销项税率是多少
  • 涉外收入申报单怎么填
  • 税金附加科目有哪些
  • 从公司账户转给出纳备注
  • 跨年的个人所得税可以更正吗
  • 短期借款不超过几年
  • 冲销凭证如何做分录
  • 返还利润含税吗
  • 购入土地使用权以什么为计税依据
  • 税金及附加科目余额在借方还是贷方
  • 小规模纳税人进项可以抵扣吗
  • 一般户和基本户怎么使用最好
  • 怎么统计每日产量
  • sql server的主数据库是( )
  • win10应用商店下载路径更改
  • win7 64位系统安装绘声绘影8提示已安装另一个版本的解决方法
  • nmeo.exe是什么
  • 会声会影win7怎么兼容
  • gnuradio编写模块
  • win10如何恢复已删除的密钥
  • win7系统360浏览器自动打开怎么关闭
  • win10 预览版 移除 tab 栏 特性
  • javascript继承原理
  • prize draw是什么意思
  • js的isnan
  • js 仿真
  • linux监控网络请求
  • python字符串strip的作用
  • unity shader可视化编辑
  • Javascript this 关键字 详解
  • 捐赠支出的扣除标准是什么是30%吗
  • 增值税纳税申报表附列资料(一)
  • 国家税务总局查发票
  • 电子发票是什么格式的文件
  • 国税局黑龙江省
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设