3D作业3--空间与运动

简答并用程序验证【建议做】

游戏对象运动的本质是什么?

游戏对象运动的本质是游戏对象的空间属性(Position、Rotate、Scale)的数值随时间的变化

请用三种方法以上方法,实现物体的抛物线运动。(如,修改Transform属性,使用向量Vector3的方法…)

法一 修改Transform属性
这里用到的是Vector3类的单位常数。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Parabolic : MonoBehaviour
{
    //初始化x方向速度为3(v = 3),y为0,默认重力加速度为10
    private int xspeed = 4;
    private int yspeed = 0;
    private int g = 10;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        //此时y方向的速度(v = a * t)
        yspeed += g * Time.deltaTime;
        //增加的position(s = v * t)
        this.transform.position += Vector3.right * xspeed * Time.deltaTime;
        this.transform.position += Vector3.down * yspeed * Time.deltaTime;
    }
}

法二 使用向量Vector3
这里通过创建一个Vector3类,对postion进行叠加。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Parabolic1 : MonoBehaviour
{
    //初始化x方向速度为4(v = 4),y为0,默认重力加速度为10
    private float xspeed = 4;
    private float yspeed = 0;
    private float g = 10;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        //此时y方向的速度(v = a * t)
        yspeed += g * Time.deltaTime;
        //用一个Vector3表示增加的position(s = v * t)
        Vector3 v = new Vector3(xspeed * Time.deltaTime, -yspeed * Time.deltaTime, 0);
        this.transform.position += v;
    }
}

法三 使用transform.Translate方法
将法二中对Vector3向量的叠加改为使用transform的Translate方法,其他的不做改变。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Parabolic : MonoBehaviour
{
    //初始化x方向速度为4(v = 4),y为0,默认重力加速度为10
    private float xspeed = 4;
    private float yspeed = 0;
    private float g = 10;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        //此时y方向的速度(v = a * t)
        yspeed += g * Time.deltaTime;
        //用一个Vector3表示增加的position(s = v * t)
        Vector3 v = new Vector3(xspeed * Time.deltaTime, -yspeed * Time.deltaTime, 0);
        this.transform.Translate(v);
    }
}

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

创建游戏对象
创建太阳系的八大行星游戏对象,这里我选用太阳(Sun)为父对象,其余为其子对象
在这里插入图片描述
调整游戏对象
对各个行星进行加载其背景贴图(通过搜索太阳系贴图得到),并略微参照事实调整大小
贴图
在这里插入图片描述
设置大小后的页面效果
在这里插入图片描述

编写代码
各步骤解析请看注释
其中两个函数解析:

public void RotateAround(Vector3 point, Vector3 axis, float angle);
用于表示一个物体围绕另外一个物体旋转
point:要围绕的点;
axiw:要围绕的轴,如x,y,z
angel:旋转的角度

Transform.Rotate(参数)
用于表示旋转
应用一个欧拉角的旋转角度,eulerAngles.z度围绕z轴,eulerAngles.x度围绕x轴,eulerAngles.y度围绕y轴(这样的顺序)

各行星自转和公转速度和方向(方向一致)参见太阳系八大行星自转和公转方向周期
在代码中只是大致体现:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Solar : 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);
    }

    // Update is called once per frame
    void Update()
    {
        //RotateAround函数表示公转
        //Rotate函数表示自转
        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);

    }
}

运行界面
将编写的代码添加到Sun的游戏对象里(也可以是其他对象或者空对象),然后把各个行星对照着拖进脚本里的Transform类
在这里插入图片描述
之后运行游戏
在这里插入图片描述

编程实践–牧师与魔鬼(Priests and Devils)

要求

阅读以下游戏脚本

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)
用表格列出玩家动作表(规则表),注意,动作越少越好
请将游戏中对象做成预制
在 GenGameObjects 中创建 长方形、正方形、球 及其色彩代表游戏中的对象。
使用 C# 集合类型 有效组织对象
整个游戏仅 主摄像机 和 一个 Empty 对象, 其他对象必须代码动态生成!!! 。 整个游戏不许出现 Find 游戏对象, SendMessage 这类突破程序结构的 通讯耦合 语句。 违背本条准则,不给分
请使用课件架构图编程,不接受非 MVC 结构程序
注意细节,例如:船未靠岸,牧师与魔鬼上下船运动中,均不能接受用户事件!

作答

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

玩家动作 条件 动作效果
点击船上的牧师或魔鬼 船在一侧岸边 点击对象下船
点击河岸上的牧师或魔鬼 1、船在靠近点击对象的一侧的岸边
2、船未满
点击对象上船
点击船 1、船在一侧岸边
2、船上有至少一个牧师或者魔鬼
船从一侧移动到对岸

游戏中对象预制
所用材料
在这里插入图片描述
做出预制(牧师和魔鬼用球体表示,其余用长方体表示)
在这里插入图片描述
整个游戏仅主摄像机和一个Empty对象, 其他对象由代码动态生成
在这里插入图片描述
MVC架构
MVC是界面人机交互程序设计的一种架构模式。它把程序分为三个部分:

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

代码实现
步骤分析请见GitHub代码中注释:
https://github.com/LLLSic/Unity3D/tree/master/hw3

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章