Unity3d HW2-空间与运动


一、简答并用程序验证

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

答:游戏对象运动的本质是对象在空间中每一帧不同位置的链接。我们在unity中对物体运动的描述其实就是让物体在一个个离散的时间点在空间中的位置不同,例如:

this.transform.position += Vector3.left * Time.deltaTime;
2.请用三种方法以上方法,实现物体的抛物线运动。(如,修改Transform属性,使用向量Vector3的方法…)

答:
方法一:
修改Transform属性:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
    public float speed_y;
    // Start is called before the first frame update
    void Start()
    {
        speed_y = 0.98f;
    }

    // Update is called once per frame

    // Update is called once per frame
    void Update()
    {
        this.transform.position += Vector3.right * Time.deltaTime * 0.98f;
        speed_y -= 0.98f * Time.deltaTime;
        this.transform.position += Vector3.up * Time.deltaTime * speed_y;
    }
}

此方法通过修改y方向移动的速度而固定x方向的速度来实现抛物线运动,给x轴初始速度0.98,y初始速度0.98,y轴有负向加速度0.98,通过修改position实现。
方法二:
使用向量Vector3

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

public class NewBehaviourScript : MonoBehaviour
{
    public float speed_y;
    // Start is called before the first frame update
    void Start()
    {
        speed_y = 0.98f;
    }

    // Update is called once per frame

    // Update is called once per frame
    void Update()
    {
        speed_y -= 0.98f * Time.deltaTime;
        Vector3 v = new Vector3(0.98f * Time.deltaTime,speed_y*Time.deltaTime,0);
        this.transform.position += v;
    }
}
原理与修改方法一类似,不过是通过Vector3作为中间介质。

方法三:
利用平移函数Translate

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

public class NewBehaviourScript : MonoBehaviour
{
    public float speed_y;
    // Start is called before the first frame update
    void Start()
    {
        speed_y = 0.98f;
    }

    // Update is called once per frame

    // Update is called once per frame
    void Update()
    {
        speed_y -= 0.98f * Time.deltaTime;
        this.transform.Translate(0.98f * Time.deltaTime, speed_y * Time.deltaTime, 0);
    }
}

直接通过translate函数修改x,y方向的坐标。


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

答:根据Rotate和RotateAround分别设置自转及公转即可
代码如下:

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

public class NewBehaviourScript : MonoBehaviour
{
    public Transform Sun;
    public Transform Mercury;//水星
    public Transform Venus;//金星
    public Transform Earth;//地球
    public Transform Moon; //月亮
    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()
    {
        Sun.position = new Vector3(0,0,30);
        Mercury.position = new Vector3(2,2,30);
        Venus.position = new Vector3(2,4,30);
        Earth.position = new Vector3(6,3,30);
        Mars.position = new Vector3(10,1,30);
        Jupiter.position = new Vector3(-5,12,30);
        Saturn.position = new Vector3(15,11,30);
        Uranus.position = new Vector3(-25,10,30);
        Neptune.position = new Vector3(-30,9,30);

        Moon.position = new Vector3(1.5f,0,0);
    }

    // Update is called once per frame

    // Update is called once per frame
    void Update()
    {
        Sun.Rotate(Vector3.up*Time.deltaTime*30);

        Mercury.Rotate(Vector3.up*Time.deltaTime*30);
        Venus.Rotate(Vector3.up * Time.deltaTime * 30);
        Earth.Rotate(Vector3.up * Time.deltaTime * 30);
        Mars.Rotate(Vector3.up * Time.deltaTime * 30);
        Jupiter.Rotate(Vector3.up * Time.deltaTime * 30);
        Saturn.Rotate(Vector3.up * Time.deltaTime * 30);
        Uranus.Rotate(Vector3.up * Time.deltaTime * 30);
        Neptune.Rotate(Vector3.up * Time.deltaTime * 30);

        Mercury.RotateAround(Sun.position, new Vector3(-2,2,0),Time.deltaTime * 1);
        Venus.RotateAround(Sun.position, new Vector3(-4,2,0), Time.deltaTime * 2);
        Earth.RotateAround(Sun.position, new Vector3(-3,6,0), Time.deltaTime * 1.5f);
        Mars.RotateAround(Sun.position, new Vector3(-1,10,0), Time.deltaTime * 1.7f);
        Jupiter.RotateAround(Sun.position, new Vector3(-12,5,0), Time.deltaTime * 3);
        Saturn.RotateAround(Sun.position, new Vector3(-11,15,0), Time.deltaTime * 0.8f);
        Uranus.RotateAround(Sun.position, new Vector3(-10,25,0), Time.deltaTime * 0.6f);
        Neptune.RotateAround(Sun.position, new Vector3(-9,30,0), Time.deltaTime * 0.5f);

        Moon.Rotate(Vector3.up * Time.deltaTime * 30);
        Moon.RotateAround(Sun.position, new Vector3(0,-1.5f,0), Time.deltaTime * 10);
    }
}

完成后如下所示:
1
演示视频链接:传送门


二、编程实践

阅读以下游戏脚本

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 )
1.列出游戏中提及的事物(Objects)
2.用表格列出玩家动作表(规则表),注意,动作越少越好
3.请将游戏中对象做成预制
4.在 GenGameObjects 中创建 长方形、正方形、球 及其色彩代表游戏中的对象。
5.使用 C# 集合类型 有效组织对象
整个游戏仅 主摄像机 和 一个 Empty 对象, 其他对象必须代码动态生成!!! 。 整个游戏不许出现 Find 游戏对象, SendMessage 这类突破程序结构的 通讯耦合 语句。 违背本条准则,不给分
6.请使用课件架构图编程,不接受非 MVC 结构程序
*注意细节,例如:船未靠岸,牧师与魔鬼上下船运动中,均不能接受用户事件!
答:
MVC架构
MVC是界面人机交互程序设计的一种架构模式。它把程序分为三个部分:
模型(Model):数据对象及关系
游戏对象、空间关系
控制器(Controller):接受用户事件,控制模型的变化
一个场景一个主控制器
至少实现与玩家交互的接口(IPlayerAction)
实现或管理运动
界面(View):显示模型,将人机交互事件交给控制器处理
处收 Input 事件
渲染 GUI ,接收事件
如下图所示:
MVC

根据MVC架构,我们将需求完成的项目目录进行划分:
目录
接下来一步步完成需求的内容。

1.列出游戏中提及的事物(Objects)
Objects描述
Boat工具、最大载人2、装载牧师和魔鬼、往来两个河岸
Devil人物魔鬼、当河岸留下的魔鬼>牧师时游戏结束
Priest人物牧师、当河岸留下的魔鬼>牧师时游戏结束
River场景、船不能驶离河流
Coast场景、人物停留
2.用表格列出玩家动作表(规则表),注意,动作越少越好
动作描述
点击人物人物上船\下船
点击船船上有人时开向对岸
3.请将游戏中对象做成预制

如下图所示:
预制

4.在 GenGameObjects 中创建 长方形、正方形、球 及其色彩代表游戏中的对象。

上一问已完成

下面是最终代码的结构,遵循5、6要求:

代码结构
在完成作业过程中,由于没有接触过架构编程,很多内容不会,这里参考了很多师兄的项目:
传送门1
传送门2
希望能够在以后的学习里将这些内容真正理解并使用。
运行视频:牧师与魔鬼

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值