unity3D-H3

作业内容

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

  • 游戏对象运动的本质是什么?
  • 》游戏对象的运动,在本质上是在离散的时间点上,物体按照物理引擎说运算出的,呈现出不同的位置。游戏对象跟随每一帧的变化,空间地变化。这里的空间变化包括了游戏对象的transform属性中的position跟rotation两个属性。一个是绝对或者相对位置的改变,一个是所处位置的角度的旋转变化。
  • 请用三种方法以上方法,实现物体的抛物线运动。(如,修改Transform属性,使用向量Vector3的方法…)

 

1,利用position的改变来实现抛物线运动,水平方向的移动是匀速进行,竖直方向是有一定的加速度变化的,按照物理的规律来看,两个方向的运动矢量相加即可实现抛物线运动,代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class move1 : MonoBehaviour {
 
    public float speed = 1;
    // Use this for initialization
    void Start () {
        Debug.Log("start!");
    }
    
    // Update is called once per frame
    void Update () {
 
        this.transform.position += Vector3.down * Time.deltaTime * (speed/10);
        this.transform.position += Vector3.right * Time.deltaTime * 5;
        speed++;
    }
}

2,直接声明创建一个Vector3变量,同时定义该变量的值,也是竖直方向上是一个均匀增加的数值,水平方向是一个保持不变的数值,然后将游戏对象原本的position属性与该向量相加即可实现抛物线运动,代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class move2 : MonoBehaviour {
 
    public float speed = 1;
    // Use this for initialization
    void Start () {
        Debug.Log("start!");
    }
    
    // Update is called once per frame
    void Update () {
 
        Vector3 change = new Vector3( Time.deltaTime*5, -Time.deltaTime*(speed/10), 0);
        ;
        this.transform.position += change;
        speed++;
    }
}
3,与第二种方法类似,区别在于第二种方法直接是利用Vector3的矢量相加,而第三种方法则是利用transform中的translate函数来进行改变position,传入参数也需要是一个Vector3向量,才可以实现position的改变,代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class move3 : MonoBehaviour {
 
    public float speed = 1;
    // Use this for initialization
    void Start () {
        Debug.Log("start!");
    }
    
    // Update is called once per frame
    void Update () {
 
        Vector3 change = new Vector3(Time.deltaTime * 5, -Time.deltaTime * (speed / 10), 0);
 
        transform.Translate(change);
        speed++;
    }
}

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

public class RoundSun : 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;
    // Use this for initialization
    Vector3[] a = new Vector3[9];
    float speed = 40;
    float speed1 = 10;
    float y, z;
    void Start()
    {
        int i = 0;
        for (i = 0; i < 9; i++)
        {
            y = Random.Range(1, 360); // 随机设置角度
            z = Random.Range(1, 360); // 随机设置角度
            a[i] = new Vector3(0, y, z); // 以上是为了制造不同的运动法平面,修改y和z可以使得绕不同的轴转
        }
    }

    // Update is called once per frame
    void Update()
    { // 每个星球的旋转动作,用到了初始化的a[i]
        mercury.RotateAround(sun.position, a[0], 2 * speed * Time.deltaTime);
        mercury.Rotate(Vector3.up * speed * Time.deltaTime);
        venus.RotateAround(sun.position, a[1], 3 * speed * Time.deltaTime);
        venus.Rotate(Vector3.up * speed * Time.deltaTime);
        earth.RotateAround(sun.position, a[2], 2 * speed1 * Time.deltaTime);
        earth.Rotate(Vector3.up * speed * Time.deltaTime);
        mars.RotateAround(sun.position, a[3], 4 * speed * Time.deltaTime);
        mars.Rotate(Vector3.up * speed * Time.deltaTime);
        jupiter.RotateAround(sun.position, a[4], 5 * speed1 * Time.deltaTime);
        jupiter.Rotate(Vector3.up * speed * Time.deltaTime);
        saturn.RotateAround(sun.position, a[5], 5 * speed * Time.deltaTime);
        saturn.Rotate(Vector3.up * speed * Time.deltaTime);
        uranus.RotateAround(sun.position, a[6], 7 * speed1 * Time.deltaTime);
        uranus.Rotate(Vector3.up * speed * Time.deltaTime);
        neptune.RotateAround(sun.position, a[7], 6 * speed1 * Time.deltaTime);
        neptune.Rotate(Vector3.up * speed * Time.deltaTime);
        moon.RotateAround(earth.position, Vector3.right, 400 * Time.deltaTime);
    }
}

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)
  • 》牧师,恶魔,船,河,两岸
  • 用表格列出玩家动作表(规则表),注意,动作越少越好
  • 玩家动作表(规则表)如下:

       所以用表格表示就是:

动作描述和条件
上下船船和岸之间的转移
开船开船到对岸
杀人恶魔多于牧师时,牧师被杀游戏结束
  • 请将游戏中对象做成预制
  • 》用白色正方体代表牧师,黑球代表恶魔,各有三个,以及用棕色长方体代表小船,绿色代表河岸,蓝色代表河水

如图所示:

  • 在 GenGameObjects 中创建 长方形、正方形、球 及其色彩代表游戏中的对象。
  • 使用 C# 集合类型 有效组织对象
  • 整个游戏仅 主摄像机 和 一个 Empty 对象, 其他对象必须代码动态生成!!! 。 整个游戏不许出现 Find 游戏对象, SendMessage 这类突破程序结构的 通讯耦合 语句。 违背本条准则,不给分
  • 请使用课件架构图编程,不接受非 MVC 结构程序
  • 注意细节,例如:船未靠岸,牧师与魔鬼上下船运动中,均不能接受用户事件!
  • 》游戏视频:牧师与恶魔
  • 》项目:牧师与恶魔
  • 》UserGUI
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class UserGUI : MonoBehaviour {
    
    	private IUserAction action;
    
    	void Start () {
    		action = SSDirector.getInstance ().currentSceneController as IUserAction;
    	}
    
    	void OnGUI() {  
    		float width = Screen.width / 6;  
    		float height = Screen.height / 12; 
    
    		if (GUI.Button(new Rect(0, 0, width, height), "Game Over!")) {  
    			action.GameOver();  
    		} 
    
    		string paused_title = SSDirector.getInstance ().Paused ? "Resume" : "Pause!"; 
    		if (GUI.Button(new Rect(width, 0, width, height), paused_title)) { 
    			SSDirector.getInstance ().Paused = !SSDirector.getInstance ().Paused;
    		} 
    	}
    
    
    }
    

     

  • 》SSDirector

  • using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class SSDirector : System.Object {
    	// singlton instance
    	private static SSDirector _instance;
    
    	public ISceneController currentSceneController { get; set;}
    
    	// https://blog.csdn.net/qiaoquan3/article/details/51339242
    	public bool Paused { get { return Time.timeScale == 0; } set {Time.timeScale = value?0:1;} } 
    
    	// get instance anytime anywhare!
    	public static SSDirector getInstance() {
    		if (_instance == null) {
    			_instance = new SSDirector ();
    		}
    		return _instance;
    	}
    
    	public int getFPS() {
    		return Application.targetFrameRate;
    	}
    
    	public void setFPS(int fps) {
    		Application.targetFrameRate = fps;
    	}
    
    	public void NextScene(){
    		Debug.Log ("Waiting next Scene now...");
    		#if UNITY_EDITOR  
    		UnityEditor.EditorApplication.isPlaying = false;
    		//UnityEditor.EditorApplication.Exit(0);
    		#else  
    		Application.Quit();  
    		#endif  
    	}
    }
    

    》IUserAction

  • using System;
    
    
    public interface IUserAction
    {
    	void GameOver();
    }
    

    》ISceneController

  • using System;
    
    public interface ISceneController
    {
    	void LoadResources();
    }
    
    
    

    FirstController

  • using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    /// <summary>
    /// Scene Controller
    /// Usage: host on a gameobject in the scene   
    /// responsiablities:
    ///   acted as a scene manager for scheduling actors.log something ...
    ///   interact with the director and players
    /// </summary>
    public class FirstController : MonoBehaviour, ISceneController, IUserAction {
    
    	// the first scripts
    	void Awake () {
    		SSDirector director = SSDirector.getInstance ();
    		director.setFPS (60);
    		director.currentSceneController = this;
    		director.currentSceneController.LoadResources ();
    	}
    	 
    	// loading resources for the first scence
    	public void LoadResources () {
    		GameObject sunset = Instantiate<GameObject> (
    			                    Resources.Load <GameObject> ("prefabs/sun"),
    			                    Vector3.zero, Quaternion.identity);
    		sunset.name = "sunset";
    		Debug.Log ("load sunset ...\n");
    	}
    
    	public void Pause ()
    	{
    		throw new System.NotImplementedException ();
    	}
    
    	public void Resume ()
    	{
    		throw new System.NotImplementedException ();
    	}
    
    	#region IUserAction implementation
    	public void GameOver ()
    	{
    		SSDirector.getInstance ().NextScene ();
    	}
    	#endregion
    
    
    	// Use this for initialization
    	void Start () {
    		//give advice first
    	}
    	
    	// Update is called once per frame
    	void Update () {
    		//give advice first
    	}
    
    }
    

    test

  • using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class test : MonoBehaviour {
    
    	// Use this for initialization
    	void Start () {
    		// ???
    		this.transform.rotation = Quaternion.AngleAxis(30, Vector3.up);
    	}
    	
    	// Update is called once per frame
    	void Update () {
    		// ???
    		this.transform.rotation *= Quaternion.AngleAxis(30 * Time.deltaTime, Vector3.up);
    	}
    }
    

     

3、思考题【选做】

  • 使用向量与变换,实现并扩展 Tranform 提供的方法,如 Rotate、RotateAround 等

作业提交要求

  • 仅能用博客或在线文档提交作业,建议使用 Github 提交代码和作业。不能使用docx、pdf等需要下载阅读的格式
  • deadline 问课程 TA
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值