3D游戏编程 homework 2

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/sodifferent/article/details/100806632

简答题

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

在游戏中一个对象的运动都是相对于某一个坐标系的运动,实质上就是坐标的改变,通过坐标改变的速度以及方法实现各种运动。

请用三种方法以上方法,实现物体的抛物线运动

方法一:使用向量计算出每个时刻球应该在的位置,再把这个向量的坐标赋给球的位置,通过translate函数实现

public class move : MonoBehaviour
{
    private float vx = 5f, vy = 0f;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 po = new Vector3(Time.deltaTime * vx, Time.deltaTime * vy, 0);
        this.transform.Translate(po);
        vy = vy - 10 * Time.deltaTime;
    }
}

方法二:对物体的position进行直接更改

public class move : MonoBehaviour
{
    private float vx = 5f, vy = 0f;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 po = new Vector3(Time.deltaTime * vx, Time.deltaTime * vy, 0);
        this.transform.position += po;
        vy = vy - 10 * Time.deltaTime;
    }
}

方法三:使用vector3的差值函数实现

public class move : MonoBehaviour
{
    private float vx = 5f, vy = 0f;
    // Use this for initialization
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 po = new Vector3(Time.deltaTime * vx, Time.deltaTime * vy, 0);
        transform.position = Vector3.Lerp(transform.position, transform.position + po, 1);
        vy -= 10 * Time.deltaTime;
    }
}

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

主要是用到了RotateAround函数来实现各个行星绕太阳的旋转。然后对于法平面的控制主要由Vector3向量来实现,而速度则是RotateAround的第三个参数。代码如下:

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

public class move : MonoBehaviour
{
    
    // Use this for initialization
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        GameObject.Find("水星").transform.RotateAround(Vector3.zero, new Vector3(0, 1, 0), 50 * Time.deltaTime);
        GameObject.Find("金星").transform.RotateAround(Vector3.zero, new Vector3(1, 1, 0), 55 * Time.deltaTime);
        GameObject.Find("地球").transform.RotateAround(Vector3.zero, new Vector3(2, 0, 0), 60 * Time.deltaTime);
        GameObject.Find("月亮").transform.RotateAround(GameObject.Find("地球").transform.position, Vector3.up, 300 * Time.deltaTime);
        GameObject.Find("火星").transform.RotateAround(Vector3.zero, new Vector3(1, 2, 0), 65 * Time.deltaTime);
        GameObject.Find("木星").transform.RotateAround(Vector3.zero, new Vector3(0.2f, 1, 0), 70 * Time.deltaTime);
        GameObject.Find("土星").transform.RotateAround(Vector3.zero, new Vector3(0.3f, 1, 0), 75 * Time.deltaTime);
        GameObject.Find("天王星").transform.RotateAround(Vector3.zero, new Vector3(0.4f, 1, 0), 85 * Time.deltaTime);
        GameObject.Find("海王星").transform.RotateAround(Vector3.zero, new Vector3(-0.7f, 1, 0), 80 * Time.deltaTime);
    }
}

搭建的模型如下:
在这里插入图片描述
运行过程截图如下:
在这里插入图片描述
在这里插入图片描述

编程实践

阅读以下游戏脚本

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 结构程序
注意细节,例如:船未靠岸,牧师与魔鬼上下船运动中,均不能接受用户事件!

  1. 游戏中的事物
    游戏中主要有牧师、魔鬼、小船、河岸、河水。

  2. 玩家动作表

动作 条件
上岸 船里有人,船在岸边
上船 船上人数小于2,人和船在相同的岸边
下船 船上有人,船在岸边
开船 船上有人,船在岸边
成功 牧师和魔鬼都在左岸
失败 有至少一个牧师死亡
牧师被吃 一边的岸上加船上(如果传靠岸)牧师数量小于魔鬼数量

为了使用MVC模型,所以需要将控制与图像物体区分开来编写主要采用了往届师兄的代码。
首先为了达到各个脚本文件之间的通信,所以使用到命名空间,这个命名空间放到Base.cs文件中,在这个文件中还定义了之后需要使用到的各种函数。主要的有Director导演,实例化唯一的一个导演来对游戏进行控制,还有对船的相关操作的定义控制,对系统需要响应的动作的控制,对岸边的控制,其中河岸与船实际上都可以使用实例化来解决。
然后是对view的控制,使用userGUI与clickGUI来分别定义用户的操作,(例如开始与重来)与点击事件(上船,下船,船过河)
详细的过程可以看代码的注释部分。
游戏运行过程如下
在这里插入图片描述
恶魔与牧师
运行视频

猜你喜欢

转载自blog.csdn.net/sodifferent/article/details/100806632