1. 游戏对象运动的本质是什么?
本质是游戏对象位置,欧拉角等空间属性的改变。
2. 请用三种方法以上方法,实现物体的抛物线运动。
首先,一个游戏对象Sphere,然后将含有以下任一代码的脚本拖放到游戏对象上,点击运行,就可以看到对象的抛物线运动轨迹。
修改Transform属性 using System.Collections; using System.Collections.Generic; using UnityEngine; public class parabola_1 : MonoBehaviour { private GameObject MySphere; private float x; private float y; private float g; // Start is called before the first frame update void Start() { MySphere = GameObject.Find("Sphere"); x = 10f; y = 0f; g = 9.8f; } // Update is called once per frame void Update() { y += g * Time.deltaTime * 0.5f; MySphere.transform.position += Vector3.right * Time.deltaTime * x; MySphere.transform.position += Vector3.down * Time.deltaTime * y; } } 使用向量Vector3 using System.Collections; using System.Collections.Generic; using UnityEngine; public class parabola_2 : MonoBehaviour { private GameObject MySphere; private Vector3 speed; private Vector3 g; // Start is called before the first frame update void Start() { MySphere = GameObject.Find("Sphere"); speed = new Vector3(10f, 0f, 0f); g = new Vector3(0f, -9.8f, 0f); } // Update is called once per frame void Update() { speed += g * Time.deltaTime * 0.5f; MySphere.transform.position += speed * Time.deltaTime; } } 使用 Transform 的 Translate 方法 using System.Collections; using System.Collections.Generic; using UnityEngine; public class parabola_3 : MonoBehaviour { private GameObject MySphere; private Vector3 speed; private Vector3 g; // Start is called before the first frame update void Start() { MySphere = GameObject.Find("Sphere"); speed = new Vector3(10f, 0f, 0f); g = new Vector3(0f, -9.8f, 0f); } // Update is called once per frame void Update() { speed += g * Time.deltaTime * 0.5f; Vector3 translation = speed * Time.deltaTime; MySphere.transform.Translate(translation); } }3.写一个程序,实现一个完整的太阳系, 其他星球围绕太阳的转速必须不一样,且不在一个法平面上。
首先,下载个太阳和八大行星的图片,导入其中,然后创建 9 个 Sphere,表示太阳和八大行星。然后将制作好的材质加入对应的星球中。
需要创建脚本,模拟公转和自转。 公转,用 Transform 的 RotateAround 方法使得行星能够围绕太阳,其中因为要求旋转速度和法平面不同,所以要设置不同的参数。 自转,使用Rotate方法,需要速度参数不同。 以地球为例:
//公转 Earth.RotateAround(Sun.position, axis3, 10 * Time.deltaTime); //自转 Earth.Rotate(Vector3.up * 30 * Time.deltaTime);最后,为每个对象添加组件Trail Renderer,记录轨迹。运行结果如下: 具体Revolution.cs文件
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!
牧师,恶魔,船,河流,两侧陆地
玩家动作表(规则表) 动作条件结果点击角色(牧师或魔鬼)游戏进行中,角色在岸上,点击的角色与船在同一岸边角色上船点击角色(牧师或魔鬼)游戏进行中,角色在船上角色上岸点击船游戏进行中,船上至少一个角色船移动到另一侧点击重新开始无游戏重新开始游戏中对象做成预制
游戏结束规则
如果任意一岸的恶魔数大于牧师数且牧师数不为零,则游戏失败。如果牧师和恶魔全部到达对岸,则游戏胜利。题目要求是预制
代码模块SSDirector
获取当前游戏的场景,掌握着游戏整体运行的节奏,管理游戏全局状态,并设定相关的配置,是游戏的整体指挥者,只能有一个。
public class SSDirector : System.Object { private static SSDirector _instance; public ISceneController currentSceneController { get; set; } public static SSDirector getInstance() { if (_instance == null) { _instance = new SSDirector(); } return _instance; } }ISceneController
Director用来向场景控制器传递要求的接口,LoadResources函数用来构建场景。
public interface ISceneController { void loadResources(); }UserAction
游戏对象响应函数的接口,对于玩家的每个动作产生相应的响应结果,触发不同的事件。
public interface UserAction { void moveBoat(); void restart(); void objectClicked(chCtrl.CharacterController ch); }CharacterControlller
加载游戏角色,控制运动。首先,通过构造函数进行初始化,并实现了一些角色的运动函数,如setPosition(), movePos(), getOnBoat()等,也提供一些角色的状态信息,以便FirstController来对角色进行总体地调度。
public class MyCharacterController{ readonly GameObject character; readonly moveable Cmove; readonly ClickGUI clickgui; readonly int Ctype;//0->priset, 1->devil private bool isOnboat; private CoastController coastcontroller; public MyCharacterController(string Myname); public int getType(); public void setName(string name); public string getName(); public void setPosition(Vector3 postion); public void moveToPosition(Vector3 _dest); public void getOnBoat(BoatController tem_boat); public void getOnCoast(CoastController coastCon); public bool _isOnBoat(); public CoastController getCoastController(); public void reset(); public void Mypause(); public void MyConti(); }CoastController/BoatController
对相应的对象进行控制。 LandController中会提供给FirstController陆地上的状态信息,上岸离岸的操作。 BoatController控制船的移动,提供船上角色信息,以及上岸上船操作。
public class CoastController{ readonly GameObject coast; readonly Vector3 from_pos = new Vector3(9,1,0); readonly Vector3 to_pos = new Vector3(-9,1,0); readonly Vector3[] postion; readonly int TFflag;//-1->to, 1->from private MyCharacterController[] passengerPlaner; public CoastController(string to_or_from); public int getTFflag(); public MyCharacterController getOffCoast(string object_name); public int getEmptyIndex(); public Vector3 getEmptyPosition(); public void getOnCoast(MyCharacterController myCharacter); public int[] getCharacterNum(); public class BoatController{ readonly GameObject boat; readonly moveable Cmove; readonly Vector3 fromPos = new Vector3 (5, 1, 0); readonly Vector3 toPos = new Vector3 (-5, 1, 0); readonly Vector3[] from_pos; readonly Vector3[] to_pos; private int TFflag;//-1->to, 1->from private MyCharacterController[] passenger = new MyCharacterController[2]; public BoatController(); public void boatMove(); public int getEmptyIndex(); public bool IfEmpty(); public Vector3 getEmptyPosition(); public GameObject getGameObject(); public int getTFflag(); public int[] getCharacterNum(); public void reset(); public void Mypause(); public void MyConti(); }FirstController
实现对场景中游戏对象的控制,同时继承并实现了ISceneController和UserAction的函数接口。
public class MySceneController: MonoBehaviour, ISceneController, UserAction { readonly Vector3 water_pos = new Vector3 (0, 0.5f, 0); UserGUI user; public CoastController fromCoast; public CoastController toCoast; public BoatController boat; private List<MyCharacterController> team; void Awake(); public void loadResources() //加载资源 public void moveBoat(); public void isClickChar (MyCharacterController tem_char) //点击角色的响应 private int checkGameOver(); //检查游戏是否结束和结束的结果 public void restart();UserGUI
完成玩家和游戏对象之间的交互,将游戏界面展现在玩家眼前。
Moveable
辅助的脚本,用来实现游戏对象boat的移动行为。
ClickGUI
检测船和角色是否被点击,被点击则触发相应接口中的动作,进入控制器进行对应的函数操作。
运行新建一个Empty对象,将FirstController的脚本挂载上去,就实现了游戏。
GitHub
使用向量与变换,实现并扩展 Tranform 提供的方法,如 Rotate、RotateAround 等
public void RotateAround(Vector3 point, Vector3 axis, float angle); point:要围绕的点; axiw:要围绕的轴,如x,y,z angel:旋转的角度
static function AngleAxis (angle : float, axis : Vector3) : Quaternion 绕axis轴旋转angle,创建一个旋转。
Quaternion是一个四元素类,具体可参考此处 这里只需要只能他和vector3之间转换相乘的意义作用就可以。
借助AngleAxis,利用向量与变换,实现RotateAround函数:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class MyRotateAround : MonoBehaviour { public Transform obj; // 被环绕对象 void Update() { Quaternion rotation = Quaternion.AngleAxis(100 * Time.deltaTime, Vector3.up); Vector3 direction = transform.position - obj.position; direction = rotation * direction; transform.position = obj.position + direction; transform.rotation *= rotation; } }与上面的同理
using System.Collections; using System.Collections.Generic; using UnityEngine; public class MyRotate : MonoBehaviour { void Update() { Quaternion rotation = Quaternion.AngleAxis(50 * Time.deltaTime, Vector3.up); transform.localRotation *= rotation ; } }