3D游戏编程作业第三章 空间与运动

    科技2022-08-29  102

    3D游戏编程作业第三章 空间与运动

    1、简答并用程序验证2、编程实践代码文件运行结果

    1、简答并用程序验证

    游戏对象运动的本质是什么? 游戏对象运动的本质,其实是游戏对象跟随每一帧的变化,空间地变化,是空间坐标的变换。这里的空间变化包括了游戏对象的transform属性中的position跟rotation两个属性。一个是绝对或者相对位置的改变,一个是所处位置的角度的旋转变化。

    请用三种方法以上方法,实现物体的抛物线运动。(如,修改Transform属性,使用向量Vector3的方法…) 方法一:vector3 创建一个vector3类 方法二:修改transform 创建一个Vector3类,对postion进行叠加。 方法三:使用transform.Translate方法 对Vector3向量的叠加改为使用transform的Translate方法

    写一个程序,实现一个完整的太阳系, 其他星球围绕太阳的转速必须不一样,且不在一个法平面上。

    新建9个Sphere, GameObject -> 3DObject -> Sphere。Sun作为父对象,其余八大行星为Sun的子对象。 找星球的贴图,然后一个一个贴上去 新建脚本 Assets -> Creat -> C# Script 参照各大行星的公转速度 using System.Collections; using System.Collections.Generic; using UnityEngine; public class SolarSystem : MonoBehaviour { //定义public的transform变量,用于之后把各行星与此transform类进行对应 public Transform Sun; public Transform Mercury; public Transform Venus; public Transform Earth; public Transform Mars; public Transform Jupiter; public Transform Saturn; public Transform Uranus; public Transform Neptune; // Start is called before the first frame update void Start() { //在Start函数里面对各行星的位置进行初始化(大致参照实际远近) Sun.position = new Vector3(0, 0, 0); Mercury.position = new Vector3(2, 0, 0); Venus.position = new Vector3(-3, 0, 0); Earth.position = new Vector3(4, 0, 0); Mars.position = new Vector3(-5, 0, 0); Jupiter.position = new Vector3(6, 0, 0); Saturn.position = new Vector3(-7, 0, 0); Uranus.position = new Vector3(8, 0, 0); Neptune.position = new Vector3(-9, 0, 0); } void Update() { //RotateAround函数表示公转 /* public void RotateAround(Vector3 point, Vector3 axis, float angle); 用于表示一个物体围绕另外一个物体旋转 point:要围绕的点; axiw:要围绕的轴,如x,y,z angel:旋转的角度 */ //Rotate函数表示自转 /* Transform.Rotate(参数) 用于表示旋转 应用一个欧拉角的旋转角度,eulerAngles.z度围绕z轴,eulerAngles.x度围绕x轴,eulerAngles.y度围绕y轴(这样的顺序) */ //水星 Mercury.RotateAround(Sun.position, new Vector3(0, 3, 1), 15 * Time.deltaTime); Mercury.Rotate(new Vector3(0, 5, 1) * 5 * Time.deltaTime); //金星 Venus.RotateAround(Sun.position, new Vector3(0, 2, 1), 13 * Time.deltaTime); Venus.Rotate(new Vector3(0, 2, 1) * Time.deltaTime); //地球 Earth.RotateAround(Sun.position, Vector3.up, 12 * Time.deltaTime); Earth.Rotate(Vector3.up * 30 * Time.deltaTime); //火星 Mars.RotateAround(Sun.position, new Vector3(0, 13, 5), 11 * Time.deltaTime); Mars.Rotate(new Vector3(0, 12, 5) * 40 * Time.deltaTime); //木星 Jupiter.RotateAround(Sun.position, new Vector3(0, 8, 3), 10 * Time.deltaTime); Jupiter.Rotate(new Vector3(0, 10, 3) * 30 * Time.deltaTime); //土星 Saturn.RotateAround(Sun.position, new Vector3(0, 2, 1), 9 * Time.deltaTime); Saturn.Rotate(new Vector3(0, 3, 1) * 20 * Time.deltaTime); //天王星 Uranus.RotateAround(Sun.position, new Vector3(0, 9, 1), 7 * Time.deltaTime); Uranus.Rotate(new Vector3(0, 10, 1) * 20 * Time.deltaTime); //海王星 Neptune.RotateAround(Sun.position, new Vector3(0, 7, 1), 5 * Time.deltaTime); Neptune.Rotate(new Vector3(0, 8, 1) * 30 * Time.deltaTime); } } 新建一个空对象,将代码挂到空对象上,然后将各个行星拖入脚本对应的transform类 运行结果

    2、编程实践

    阅读以下游戏脚本

    Priests and Devils Priests and Devils is a puzzle game in which you will help the Priests and Devils to cross the river within the time limit. There are 3 priests and 3 devils at one side of the river. They all want to get to the other side of this river, but there is only one boat and this boat can only carry two persons each time. And there must be one person steering the boat from one side to the other side. In the flash game, you can click on them to move them and click the go button to move the boat to the other direction. If the priests are out numbered by the devils on either side of the river, they get killed and the game is over. You can try it in many > ways. Keep all priests alive! Good luck!

    程序需要满足的要求:

    play the game ( http://www.flash-game.net/game/2535/priests-and-devils.html )列出游戏中提及的事物(Objects)用表格列出玩家动作表(规则表),注意,动作越少越好将游戏中对象做成预制在场景控制器 LoadResources 方法中加载并初始化 长方形、正方形、球 及其色彩代表游戏中的对象。使用 C# 集合类型 有效组织对象整个游戏仅 主摄像机 和 一个 Empty 对象, 其他对象必须代码动态生成!!! 。 整个游戏不许出现 Find 游戏对象, SendMessage 这类突破程序结构的 通讯耦合 语句。

    游戏中提及的事物(Objects) 3个牧师(Priest)、3个魔鬼(Devil)、船(Boat)、河流(River)、两边的河岸(Side)。

    玩家动作表(规则表)

    玩家动作参数结果启动游戏游戏初始界面开船船从一个岸边到另一个岸边点击角色船的位置,状态结束/未结束点击船船上的人数结束/未结束

    将游戏中对象做成预制 在场景控制器 LoadResources 方法中加载并初始化 长方形、正方形、球 及其色彩代表游戏中的对象。

    整个游戏仅 主摄像机 和 一个 Empty 对象 新建一个空对象,将FirstController挂在空对象上

    MVC架构 MVC是界面人机交互程序设计的一种架构模式。它把程序分为三个部分:

    模型(Model):数据对象及关系 游戏对象、空间关系 控制器(Controller):接受用户事件,控制模型的变化 一个场景一个主控制器至少实现与玩家交互的接口(IPlayerAction)实现或管理运动 界面(View):显示模型,将人机交互事件交给控制器处理 处收 Input 事件渲染 GUI ,接收事件

    代码文件

    传送门

    运行结果

    Processed: 0.009, SQL: 10