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

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