1、简答并用程序验证
游戏对象运动的本质是什么?
游戏对象运动的本质,其实是游戏对象跟随每一帧的变化,空间地变化。这里的空间变化包括了游戏对象的transform属性中的position跟rotation两个属性。一个是绝对或者相对位置的改变,一个是所处位置的角度的旋转变化。
请用三种方法以上方法,实现物体的抛物线运动。(如,修改Transform属性,使用向量Vector3的方法…)
- 第一种方法是利用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++;
}
}
- 第二种方法是直接声明创建一个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++;
}
}
- 第三种方法其实与第二种方法类似,区别在于第二种方法直接是利用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++;
}
}
写一个程序,实现一个完整的太阳系, 其他星球围绕太阳的转速必须不一样,且不在一个法平面上。
添加各个星球的对象,修改其位置和大小并设置贴图。
编写脚本Move
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour
{
public Transform center;
public float revolution;
public float rotation;
public float ry, rz;
// Start is called before the first frame update
void Start()
{
}
void FixedUpdate()
{
Vector3 axis = new Vector3(0, ry, rz);
this.transform.RotateAround(center.position, axis, revolution * Time.deltaTime);
this.transform.Rotate(Vector3.up * rotation * Time.deltaTime);
}
}
将该脚本添加到各个对象上,并设置公转自转速度,设置运动的方向以保证其不在同一法平面内。
2、编程实践
列出游戏中提及的事物
对象:牧师 恶魔 小船 此岸 彼岸
用表格列出玩家动作表
动作 | 条件 |
---|---|
上船 | 船在相应岸边,岸上存在目标对象且船上有空位 |
下船 | 船在相应岸边,船上存在对象 |
开船 | 船在相应岸边,船上存在对象 |