简答题
1. 游戏对象运动的本质是什么?
游戏对象的运动过程本质上是使用矩阵变换(平移、旋转、缩放)改变游戏对象的空间属性。
public class MoveLeft : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
this.transform.position += Vector3.left * Time.deltaTime;
}
}
如以上的脚本,它使得游戏对象每秒钟向左移动一个单位。将代码拖放到任意游戏对象上并运行游戏,可以观察到游戏对象左移。这种运动行为的实现实际上是通过改变对象的坐标实现的。
2. 请用三种方法以上方法,实现物体的抛物线运动。(如,修改Transform属性,使用向量Vector3的方法…)
实现物体抛物线运动的基本思路是,将物体运动的速度分解为x方向上和y方向上的分量。在x方向上,物体做匀速运动;在y方向上,物体做加速运动。修改相应方向上的坐标即可实现抛物线运动。
修改Transform属性的方法:
public class mv : MonoBehaviour {
int xspeed, yspeed, g = 1;
void Start () {
xspeed = 1;
yspeed = 0;
}
void Update () {
yspeed += g;
transform.position += Vector3.right * Time.deltaTime * yspeed;
transform.position += Vector3.down * Time.deltaTime * yspeed;
}
}
使用向量Vector的方法:
public class mv : MonoBehaviour {
int xspeed, yspeed, g = 1;
void Start () {
xspeed = 1;
yspeed = 0;
}
void Update () {
yspeed += g;
Vector3 v = new Vector3(Time.deltaTime * yspeed, -Time.deltaTime * yspeed, 0);
transform.position += v;
}
}
使用Translate函数来改变坐标的方法:
public class mv : MonoBehaviour {
int xspeed, yspeed, g = 1;
void Start () {
xspeed = 1;
yspeed = 0;
}
void Update () {
yspeed += g;
Vector3 v = new Vector3(Time.deltaTime * yspeed, -Time.deltaTime * yspeed, 0);
transform.Translate(v);
}
}
编程实践
阅读以下游戏脚本:
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 )