unity3d打砖块游戏代码.docx
- 文档编号:30703063
- 上传时间:2023-08-19
- 格式:DOCX
- 页数:17
- 大小:494.52KB
unity3d打砖块游戏代码.docx
《unity3d打砖块游戏代码.docx》由会员分享,可在线阅读,更多相关《unity3d打砖块游戏代码.docx(17页珍藏版)》请在冰豆网上搜索。
unity3d打砖块游戏代码
BrickBreakerGame
Step1:
CreateNewProject“BrickBreaker”
File->NewProject(checkStandardAssets(Mobile).unityPackage)
Thensavethescenefollowingbelowinstruction.Changenameofsceneto“MainScene”.
File->SaveScene
Step2:
SetMainCamera
Theposition,rotationandscaleofMainCameraareshowedfollowingfigure.
Changebackgroundcolorwhateveryouwant.
Changeprojectionperspective->orthographic
Size=20
ClippingPlanes:
Near=0.3Far=25
Step3:
CreateDirectionalLight
GameObject->CreateOther->DirectionalLight
Step4:
CreateWalls
Createnewcubefollowingthisinstruction:
GameObject->CreateOther->Cube
ChangeCubeNameto“WallLeft”.
“WallLeft”position,rotationandscalearefollowingbelowfigure
Change“MainColor”.
Creatematerialfromthe“Project”section.
Create->Material
NameMaterial“wallMat”andchangecolor.Thendrag“wallMat”totheWallLeft.
Createnewcubefollowingthisinstruction:
GameObject->CreateOther->Cube
ChangeCubeNameto“WallRight”.
“WallRight”position,rotationandscalearefollowingbelowfigure
Change‘MainColor”
Drag“wallMat”totheWallRight.
Createnewcubefollowingthisinstruction:
GameObject->CreateOther->Cube
ChangeCubeNameto“WallTop”.
“WallTop”position,rotationandscalearefollowingbelowfigure
Change‘MainColor”
Thendrag“wallMat”totheWallTop.
Step5:
CreateBricks
GameObject->CreateOther->Cube
Changecubenameto“brick”.
“brick”position,rotationandscalearefollowingbelowfigure.
Createnewmaterialfrom“Project”sectionandname“brickMat”andchangecolor.
Thendragto“brick”.
Thencopy“brick”12timesfollowingbelowfigure.
Step6:
CreateBall
Windows->AssetStore
Typeballpackandenter.
Thenclickdownloadbuttonandimportbuttontoimporttoprojectthismodel.
Thenchoose“eyeball”fromtheballpackanddragto“scene”.
“EyeBall”position,rotationandscalearefollowingbelowfigure.
AddRigidbodyto“EyeBall”followingbelowinstruction.
Component->Physics->Rigidbody
Mass=0.01
Uncheck“UseGravity”
CheckFreezePosition->Z
AddSphereColliderto“EyeBall”followingbelowfigure.
Component->Physics->SphereCollider.
ThenselectMaterialNone->Bouncy
Step7:
CreatePaddle
Window->AssetStore
TypeHoverboardandclickdownloadbuttonthenclickimportbuttontoimporttoproject.
Draghoverboardto“Scene”.Namehoverboardto“Paddle”.
Hoverboardposition,rotationandscalearefollowingbelowfigure.
AddBoxCollidertoPaddlefollowingbelowinstruction
Component->Physics->BoxCollider
Don’tchangeanything.
Step8:
CreateScoreText
GameObject->CreateOther->GUIText
NameGUITexttoScore.
ChangeTextto“Score:
”
Step9:
CreatePaddleController
Createnewjavascriptfrom“Project“sectionfollowingbelowinstruction
Create->JavaScript
Thendragto“Paddle”onHierarchy
PaddleController.js
functionUpdate()
{
//paddledirectionisjustmovedtoxpositionrightandleft.
transform.position.x=Camera.main.ScreenToWorldPoint(Input.mousePosition).x;
//xdirectionlimitisfrom-19to11.
transform.position.x=Mathf.Clamp(transform.position.x,-19,11);
}
functionOnCollisionEnter(collision:
Collision)
{
//collisionoverallspeed
varvelo=collision.rigidbody.velocity.magnitude;
//collisionspeedischangingtoxposition
collision.rigidbody.velocity.x=(collision.transform.position.x-transform.position.x)*8;
if(collision.rigidbody.velocity.magnitude { collision.rigidbody.velocity*=velo/collision.rigidbody.velocity.magnitude; }} Step10: CreateBalldirection Createnewjavascriptfromprojectsectionfollowingbelowinstruction Create->JavaScript Thendragto“Eyeball”onHierarchy BallDirection.js //initialvalueofspeedmultiplyby20. varinitialSpeed: float=20; functionStart() { //xDirisxpositionrangefrom-1.0to1.0ofBall. varxDir: float=Random.Range(-1.0,1.0); //y=-1whengamestartsballrandomlyfallingdown. rigidbody.AddForce(Vector3(xDir,-1,0)*initialSpeed); } Step11: CreateBrickDestroy Createnewjavascriptfromprojectsectionfollowingbelowinstruction Create->JavaScript Thendragto“Eyeball”onHierarchy BrickDestroy.js functionOnCollisionEnter(myCol: Collision){ if(myCol.gameObject.name=="brick"){ //whenballcollidewithgameObject“brick”,scorewillbeaddedby1. Score.score++; //whenballcollidewithgameObject“brick”,brickwillbedestroyed. Destroy(myCol.gameObject); } } Step12: CreateGameOver Createnewscenefollowingbelowinstruction File->Scene TheCreatenewGUITextfollowingbelowinstruction GameObject->CreateOther->GUIText ChangeTextto“GameOver” ChangeAnchorto“middlecenter” ChangeFontsizeto“71” Thenbuild2scenesfollowingbelowinstruction. File->BuildSettings. Becauseafter1scenetheanotherscenehastowork. Createnewjavascriptfromprojectsectionfollowingbelowinstruction Create->JavaScript Thendragto“Eyeball”onHierarchy GameOver.js functionUpdate(){ //ifypositionofballislowerthan-20,“GameOver”scenewillbeappeared. if(rigidbody.position.y<-20){ Application.LoadLevel("gameOver"); } } Step13: CreateScore Createnewjavascriptfromprojectsectionfollowingbelowinstruction Create->JavaScript Thendragto“MainCamera”onHierarchy Score.js staticvarscore: float=0; varscoreText: GUIText; functionUpdate(){ //showingScoretext scoreText.text="Score: "+score; } ThenDrag“Score”onHierarchytoScoreTextof“MainCamera”. Step14: CreateRestart Createnewjavascriptfromprojectsectionfollowingbelowinstruction Create->JavaScript ThenOpen“GameOver”scene Thendragto“MainCamera”onHierarchy Restart.js //ifclickmousebuttononthescene,“MainScene”willbeappeared. functionUpdate(){ if(Input.GetMouseButtonDown(0)) Application.LoadLevel("MainScene"); }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- unity3d 砖块 游戏 代码