计算机游戏程序设计 实验指导书 实验二.docx

上传人:b****3 文档编号:4224574 上传时间:2022-11-28 格式:DOCX 页数:10 大小:124.08KB
下载 相关 举报
计算机游戏程序设计 实验指导书 实验二.docx_第1页
第1页 / 共10页
计算机游戏程序设计 实验指导书 实验二.docx_第2页
第2页 / 共10页
计算机游戏程序设计 实验指导书 实验二.docx_第3页
第3页 / 共10页
计算机游戏程序设计 实验指导书 实验二.docx_第4页
第4页 / 共10页
计算机游戏程序设计 实验指导书 实验二.docx_第5页
第5页 / 共10页
点击查看更多>>
下载资源
资源描述

计算机游戏程序设计 实验指导书 实验二.docx

《计算机游戏程序设计 实验指导书 实验二.docx》由会员分享,可在线阅读,更多相关《计算机游戏程序设计 实验指导书 实验二.docx(10页珍藏版)》请在冰豆网上搜索。

计算机游戏程序设计 实验指导书 实验二.docx

计算机游戏程序设计实验指导书实验二

实验二Unity游戏脚本

一、实验目的与要求

1.熟悉及掌握MonoDevelop脚本编辑器的使用方法。

2.Unity脚本的生命周期。

3.熟练使用脚本来操作游戏对象。

二、实验内容及步骤

1.熟悉MonoDevelop脚本编辑器的使用方法,联系实现脚本调试。

2.编程实现创建游戏对象(立方体,球体),给游戏对象命名,改变颜色,添加刚体组件。

3.分别通过名称、标签获得游戏对象,通过标签获得多个游戏对象。

4.通过脚本控制游戏对象,改变游戏对象的位置,旋转游戏对象,缩放游戏对象。

三、实验仪器与软件

1.PC计算机

2.Unity3D软件

四、实验报告要求

1.熟悉MonoDevelop脚本编辑器的使用方法,联系实现脚本调试。

创建C#脚本:

在脚本中编辑代码

2.编程实现创建游戏对象(立方体,球体),给游戏对象命名,改变颜色,添加刚体组件。

usingUnityEngine;

usingSystem.Collections;

publicclassNewBehaviourScript:

MonoBehaviour{

 

//Usethisforinitialization

voidStart(){

}

//Updateiscalledonceperframe

voidUpdate(){}

voidOnGUI(){

if(GUI.Button(newRect(0,0,100,100),"创建立方体")){

GameObjectobjCube=GameObject.CreatePrimitive(PrimitiveType.Cube);

objCube.AddComponent("Rigidbody");

objCube.name="Cube";

objCube.renderer.material.color=Color.blue;

}

if(GUI.Button(newRect(100,0,100,100),"创建球体")){

GameObjectobjSphere=GameObject.CreatePrimitive(PrimitiveType.Sphere);

objSphere.AddComponent("Rigidbody");

objSphere.name="Sphere";

objSphere.renderer.material.color=Color.green;

}

}

}

受到重力降落:

3.分别通过名称、标签获得游戏对象,通过标签获得多个游戏对象。

通过名称获得游戏对象:

usingUnityEngine;

usingSystem.Collections;

publicclassNewBehaviourScript1:

MonoBehaviour{

privateGameObjectobjCube;

privateGameObjectobjSphere;

privateboolisCubeRoate=false;

privateboolisSphereRoate=false;

privatestringCubeInfo="旋转立方体";

privatestringSphereInfo="旋转球体";

//Usethisforinitialization

voidStart(){

objCube=GameObject.Find("Cube");

objSphere=GameObject.Find("Sphere");

}

//Updateiscalledonceperframe

voidUpdate(){

if(isCubeRoate){

if(objCube){//档立方体不为null时旋转

objCube.transform.Rotate(0.0f,Time.deltaTime*200,0.0f);

}

}

if(isSphereRoate){

if(objSphere){//档立方体不为null时旋转

objSphere.transform.Rotate(0.0f,Time.deltaTime*200,0.0f);

}

}

}

voidOnGUI(){

if(GUI.Button(newRect(0,0,100,100),CubeInfo)){

isCubeRoate=true;

CubeInfo="停止旋转";

}else{

isCubeRoate=false;

CubeInfo="旋转立方体";

}

if(GUI.Button(newRect(0,100,100,100),SphereInfo)){

isSphereRoate=true;

SphereInfo="停止旋转";

}else{

isSphereRoate=false;

SphereInfo="旋转球体";

}

}

}

通过标签来获取游戏对象

给Cube添加一个标签代码改为:

voidStart(){

objCube=GameObject.FindWithTag("tag1");

objSphere=GameObject.Find("Sphere");

}

通过标签获取多个游戏对象:

voidStart(){

objCube=GameObject.FindGameObjectsWithTag("tag1");

objSphere=GameObject.Find("Sphere");

}

4.通过脚本控制游戏对象,改变游戏对象的位置,旋转游戏对象,缩放游戏对象。

改变游戏对象的位置:

usingUnityEngine;

usingSystem.Collections;

publicclassNewBehaviourScript2:

MonoBehaviour{

privatefloatx=0.0f;

privatefloaty=0.0f;

privatefloatz=0.0f;

privateGameObjectobj;

//Usethisforinitialization

voidStart(){

obj=GameObject.Find("Cube");

}

//Updateiscalledonceperframe

voidUpdate(){

}

voidOnGUI(){

GUI.Box(newRect(0,0,100,100),"移动立方体x轴");

x=GUILayout.HorizontalSlider(x,-10.0f,10.0f,GUILayout.Width(400));

GUI.Box(newRect(0,100,100,100),"移动立方体y轴");

y=GUILayout.HorizontalSlider(y,-10.0f,10.0f,GUILayout.Width(400));

GUI.Box(newRect(0,200,100,100),"移动立方体z轴");

z=GUILayout.HorizontalSlider(z,-10.0f,10.0f,GUILayout.Width(400));

obj.transform.position=newVector3(x,y,z);

//GUI.Label(newRect(300,300),(string)obj.transform.position);

}

}

旋转游戏对象:

usingUnityEngine;

usingSystem.Collections;

publicclassNewBehaviourScript2:

MonoBehaviour{

//立方体对象

privateGameObjectobjCube;

//圆柱体对象

privateGameObjectobjCylinder;

//旋转速度

privateintspeed=100;

//Usethisforinitialization

voidStart(){

//获得对象

objCube=GameObject.Find("Cube");

objCylinder=GameObject.Find("Cylinder");

}

//Updateiscalledonceperframe

voidUpdate(){

}

voidOnGUI()

{

if(GUILayout.RepeatButton("立方体沿x轴旋转",GUILayout.Height(50)))

{

objCube.transform.Rotate(Vector3.right*Time.deltaTime*speed,Space.Self);

}

if(GUILayout.RepeatButton("立方体沿y轴旋转",GUILayout.Height(50)))

{

objCube.transform.Rotate(Vector3.up*Time.deltaTime*speed,Space.World);

}

if(GUILayout.RepeatButton("立方体沿z轴旋转",GUILayout.Height(50)))

{

objCube.transform.Rotate(Vector3.forward*Time.deltaTime*speed,Space.World);

}

if(GUILayout.RepeatButton("立方体围绕圆柱体旋转",GUILayout.Height(50)))

{

objCube.transform.RotateAround(objCylinder.transform.position,Vector3.up,Time.deltaTime*speed);

}

}

}

缩放游戏对象:

usingUnityEngine;

usingSystem.Collections;

publicclassNewBehaviourScript2:

MonoBehaviour{

privatefloatx=0.0f;

privatefloaty=0.0f;

privatefloatz=0.0f;

privateGameObjectobj;

//Usethisforinitialization

voidStart(){

obj=GameObject.Find("Cube");

}

//Updateiscalledonceperframe

voidUpdate(){

}

voidOnGUI(){

GUILayout.Label("X轴缩放");

x=GUILayout.HorizontalSlider(x,0.1f,2.0f,GUILayout.Width(100));

GUILayout.Label("Y轴缩放");

y=GUILayout.HorizontalSlider(y,0.1f,2.0f,GUILayout.Width(100));

GUILayout.Label("Z轴缩放");

z=GUILayout.HorizontalSlider(z,0.1f,2.0f,GUILayout.Width(100));

//重新计算缩放比例

obj.transform.localScale=newVector3(x,y,z);

GUILayout.Label("立方体的缩放比例:

"+obj.transform.localScale);

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 幼儿教育 > 家庭教育

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1