空间与运动

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);
  }

实验效果
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)用表格列出玩家动作表(规则表),注意,动作越少越好

玩家动作条件结果
点击船上的牧师/魔鬼船在岸边且不为空牧师/魔鬼下船
点击岸上的牧师/魔鬼船在岸边且未满员牧师/魔鬼上船
点击发船按钮船在岸边且不为空发船

预置对象

预置
代码部分
运行实例因为文件过大无法上传,只有截图了
运行实例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值