空间与运动
1、简答并用程序验证【建议做】
(1)游戏对象运动的本质是什么?
使用矩阵变换(平移、旋转、缩放)改变游戏对象的空间属性。
(2)请用三种方法以上方法,实现物体的抛物线运动。(如,修改Transform属性,使用向量Vector3的方法…)
方法一:修改Transform属性
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class parabola : MonoBehaviour
{
// Start is called before the first frame update
public GameObject Sphere;
// 重力加速度为10(单位)/s^2
private float gravity = 10.0f;
// 水平速度5(单位)/s
private float speed_x = 5.0f;
// 竖直方向初速度为0
private float speed_y = 0f;
void Start()
{
// 初始化球体
Sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
Sphere.transform.position = new Vector3(0, 60, 0);
}
// Update is called once per frame
void Update()
{
if (Sphere.transform.position.y <= 0)
{
return;
}
// 更新竖直方向的速度
speed_y += gravity * Time.deltaTime;
// 更新水平匀速运动
Sphere.transform.position += Vector3.left * speed_x * Time.deltaTime;
// 更新竖直方向的匀加速运动
Sphere.transform.position += Vector3.down * speed_y * Time.deltaTime;
}
}
方法2:使用向量Vector3做出抛物线运动的简便表达
void Update()
{
void Update()
{
if (Sphere.transform.position.y <= 0)
{
return;
}
// 更新竖直方向运动的速度
speed_y += gravity * Time.deltaTime;
// 创建抛物线运动的向量:
Vector3 parabola = new Vector3(-speed_x * Time.deltaTime, -speed_y * Time.deltaTime, 0);
// 更新位置
Sphere.transform.position += parabola;
}
方法3:使用transform.Translate方法更新位置
void Update() {
if(Sphere.transform.position.y <= 0) {
return;
}
// 更新竖直方向运动的速度
speed_y += gravity * Time.deltaTime;
// 创建抛物线运动的向量:
Vector3 parabola = new Vector3(-speed_x * Time.deltaTime, -speed_y * Time.deltaTime, 0);
// 使用transform.Translate更新位置
Sphere.transform.Translate(parabola);
}
实验效果
(3)写一个程序,实现一个完整的太阳系, 其他星球围绕太阳的转速必须不一样,且不在一个法平面上。
预置太阳系各星球
代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Solar_system : MonoBehaviour
{
// Start is called before the first frame update
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;
void Start()
{
}
// Update is called once per frame
void Update()
{
Mercury.RotateAround(Sun.position, new Vector3(0, 5, 1), 20 * Time.deltaTime);
Mercury.Rotate(new Vector3(0, 5, 1) * 5 * Time.deltaTime);
Venus.RotateAround(Sun.position, new Vector3(0, 2, 1), 15 * Time.deltaTime);
Venus.Rotate(new Vector3(0, 2, 1) * Time.deltaTime);
Earth.RotateAround(Sun.position, Vector3.up, 10 * Time.deltaTime);
Earth.Rotate(Vector3.up * 30 * Time.deltaTime);
Mars.RotateAround(Sun.position, new Vector3(0, 12, 5), 9 * Time.deltaTime);
Mars.Rotate(new Vector3(0, 12, 5) * 40 * Time.deltaTime);
Jupiter.RotateAround(Sun.position, new Vector3(0, 10, 3), 8 * Time.deltaTime);
Jupiter.Rotate(new Vector3(0, 10, 3) * 30 * Time.deltaTime);
Saturn.RotateAround(Sun.position, new Vector3(0, 3, 1), 7 * Time.deltaTime);
Saturn.Rotate(new Vector3(0, 3, 1) * 20 * Time.deltaTime);
Uranus.RotateAround(Sun.position, new Vector3(0, 10, 1), 6 * Time.deltaTime);
Uranus.Rotate(new Vector3(0, 10, 1) * 20 * Time.deltaTime);
Neptune.RotateAround(Sun.position, new Vector3(0, 8, 1), 5 * Time.deltaTime);
Neptune.Rotate(new Vector3(0, 8, 1) * 30 * Time.deltaTime);
}
}
运行结果:
2、编程实践
(1)列出游戏中提及的事物(Objects)
三个牧师、三个魔鬼、两块陆地(两岸)、一条河、一艘船、一个按钮(控制发船)
(2)用表格列出玩家动作表(规则表),注意,动作越少越好
玩家动作 | 条件 | 结果 |
---|---|---|
点击船上的牧师/魔鬼 | 船在岸边且不为空 | 牧师/魔鬼下船 |
点击岸上的牧师/魔鬼 | 船在岸边且未满员 | 牧师/魔鬼上船 |
点击发船按钮 | 船在岸边且不为空 | 发船 |
预置对象
代码部分
运行实例因为文件过大无法上传,只有截图了