作业3-空间与运动

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

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

游戏对象随帧的改变,进行空间属性的改变。游戏对象的空间属性包括transform属性的position、rotation、scale参数。

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

代码传送门

抛物线运动可以拆分成两个方向的运动,一个方向上的匀速运动和另一个方向的加速运动。

  1. 直接改变对象的position。
    public float speed = 1; 
    
    void Update()
    {
        this.transform.position += Vector3.right * Time.deltaTime * 5;
        this.transform.position += Vector3.down * Time.deltaTime * (speed / 10);
        speed++;         
    }
  1. 创建一个Vector3变量,将这个变量加到position,从而完成对象的抛物线运动。
public float speed = 1; 
   
    void Update()
    {
        Vector3 v = new Vector3(Time.deltaTime * 5, - Time.deltaTime * (speed / 10), 0);
        this.transform.position += v;
        speed++;    
    }
  1. 创建一个Vector3变量,利用transform中的translate函数来进行改变position,从而完成对象的抛物线运动。
public float speed = 1;  
   
    void Update()
    {
        Vector3 v = new Vector3(Time.deltaTime * 5, - Time.deltaTime * (speed / 10), 0);
        this.transform.Translate(v);    
        speed++;
    }

效果如下图:
图片

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

项目传送门

结合课程网站上的太阳、地球、月球的例子,首先搜集太阳、八大行星的图,将这些图拖入Assets,创建十个球形对象,定义好他们的大小和位置,将图拉到对象上,最后将月球拉到地球上,使之成为地球的子对象。代码如下,将cs文件拖到main camera上,然后将各个对象拖到组件的对应位置。

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

public class solarsys : MonoBehaviour
{
    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;
    public Transform moon;
    Vector3 []a = new Vector3[8];
    float []v = new float[8];
    float speed = 40;
    // Start is called before the first frame update
    void Start()
    {
        int i;
        for(i = 0; i < 8; i++){
            a[i] = new Vector3(0, Random.Range(1, 360), Random.Range(1, 360)); //随机角度
            v[i] = speed + Random.Range(0, 30); //随机速度
        }
    }

    // Update is called once per frame
    void Update()
    {
        mercury.RotateAround(sun.position, a[0], v[0] * Time.deltaTime);
        mercury.Rotate(Vector3.up * speed * Time.deltaTime);
        venus.RotateAround(sun.position, a[1], v[1] * Time.deltaTime);
        venus.Rotate(Vector3.up * speed * Time.deltaTime);
        earth.RotateAround(sun.position, a[2], v[2] * Time.deltaTime);
        earth.Rotate(Vector3.up * speed * Time.deltaTime);
        moon.RotateAround(earth.position, Vector3.up, 359 * Time.deltaTime);    
        mars.RotateAround(sun.position, a[3], v[3] * Time.deltaTime);
        mars.Rotate(Vector3.up * speed * Time.deltaTime);
        jupiter.RotateAround(sun.position, a[4], v[4] * Time.deltaTime);
        jupiter.Rotate(Vector3.up * speed * Time.deltaTime);
        saturn.RotateAround(sun.position, a[5], v[5] * Time.deltaTime);
        saturn.Rotate(Vector3.up * speed * Time.deltaTime);
        uranus.RotateAround(sun.position, a[6], v[6] * Time.deltaTime);
        uranus.Rotate(Vector3.up * speed * Time.deltaTime);
        neptune.RotateAround(sun.position, a[7], v[7] * Time.deltaTime);
        neptune.Rotate(Vector3.up * speed * Time.deltaTime);  
    }
}

为了更好的观察轨迹,给除了太阳以外的球形对象加上组件trail renderer,得到下图。
图片

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 这类突破程序结构的 通讯耦合 语句。 违背本条准则,不给分
  • 请使用课件架构图编程,不接受非 MVC 结构程序
  • 注意细节,例如:船未靠岸,牧师与魔鬼上下船运动中,均不能接受用户事件!
  1. 游戏中提及的事物:

河岸、船、牧师、魔鬼、水

  1. 玩家动作表
玩家动作执行条件执行结果
点击河岸上的牧师点击的牧师和船在河岸的同一侧牧师从河岸移动到船上
点击河岸上的魔鬼点击的恶魔和船在河岸的同一侧恶魔从河岸移动到船上
点击船上的牧师无牧师从船上移动到靠近的河岸上
点击船上的魔鬼无恶魔从船上移动到靠近的河岸上
点击船船上有至少一人船移动到另一侧
点击restart恢复初始状态,船靠右岸,牧师和魔鬼都在右岸
  1. 具体实现:使用MVC模式,将程序分为3个部分,分别是模型(Model)、控制器(Controller)、界面(View)。
    1. 模型(Model):数据对象及关系。代码位于Models.cs。包括接口ISceneController(用于加载资源),接口IUserAction(用于处理操作,比如移动角色、移动船、检查游戏是否结束、重启游戏)。还包括类SSDirector(导演类),类LandModel(定义河岸及相关函数),类BoatModel(定义船及相关函数),类RoleModel(定义牧师与魔鬼及相关函数),类Move(定义了游戏对象的移动),类Click(定义了点击船和角色后的处理方法)。
    2. 控制器(Controller):接受用户事件,控制模型的变化。代码位于Controllor.cs。包括对Model.cs中两个接口的函数的具体实现。
    3. 界面(View):显示模型。代码位于UserGUI.cs。

具体步骤是:先创建prefab,包括河岸、水、牧师、魔鬼、船,放在Resources/Prefabs目录下,material放在Materials文件夹下,然后创建Models.cs、Controllor.cs、UserGUI.cs,放在Scripts文件夹下。最后新建一个空对象,将Controllor.cs拖到空对象上。代码如下。

在游戏说明的基础上增加了倒计时的功能,界面显示剩余秒数,超时算作游戏失败。具体的代码见下方的项目传送门。

项目传送门

效果如下图:
图片

3、思考题【选做】

  • 使用向量与变换,实现并扩展 Tranform 提供的方法,如 Rotate、RotateAround 等
  1. 直接改变对象的位置
void Update () {
		float delta = Mathf.Atan (this.transform.position.y / this.transform.position.x);
		float temp_x = this.transform.position.x;
		float temp_y = this.transform.position.y;
		this.transform.position.x = Mathf.Cos (delta) * temp_x - Mathf.Sin (delta) * temp_y;
		this.transform.position.y = Mathf.Sin (delta) * temp_x + Mathf.Cos (delta) * temp_y;
}
  1. 通过Vector3向量来实现
public int angle;
void Update () {
        float delta = Mathf.Atan (this.transform.position.y / this.transform.position.x);
		angle -= delta;
		transform.Translate(new Vector3(Time.deltaTime * 10*Mathf.Cos(angle), Time.deltaTime * 10*Mathf.Sin(angle), 0));  
}

c#自学

IList,列表抽象行为接口

List,泛型列表类

ArrayList,动态任意类型数组类

HashSet 类

Hashtable 类

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值