unity

1、简答并用程序验证

  • 游戏对象运动的本质是什么?

游戏运动的本质就是使用矩阵变换(平移、旋转、缩放)改变游戏对象的空间属性。

  • 请用三种方法以上方法,实现物体的抛物线运动。(如,修改Transform属性,使用向量Vector3的方法…)

(1)修改Transform属性

抛物运动的原理就是水平方向做匀速运动,竖直方向上做加速度不变的运动,例如让一个物体在x轴和y轴组成的平面上做抛物运动,那么就是z轴的坐标不变,x轴和y轴的坐标改变。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class move1 : MonoBehaviour {  //假设斜上抛
    public float v_start = 10f;  //初始速度
    public float angle = 45f; //假设初始的角度为45°
    public float g = 10f; //重力加速度

    private float vx; //初始x轴的速度
    private float vy; //初始y轴的速度
    private float vy1; //y的顺时速度
    private float t;  //时间

    void Start() {
        vx = v_start * Mathf.Cos(angle); 
        vy = v_start * Mathf.Sin(angle);
        t = Time.deltaTime;
    }

    void Update() {
        Vector3 change = new Vector3(t * vx, vy * t + 0.5f * g * Mathf.Pow(t, 2f) , 0);
        vy1 = vy - g * t;
        this.transform.position += change;
    }
}

(2)使用向量Vector3的方法

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class move1 : MonoBehaviour {  //假设斜上抛
    public float v_start = 10f;  //初始速度
    public float angle = 45f; //假设初始的角度为45°
    public float g = 10f; //重力加速度

    private float vx; //初始x轴的速度
    private float vy; //初始y轴的速度
    private float vy1; //y的顺时速度
    private float t;  //时间

    void Start() {
        vx = v_start * Mathf.Cos(angle); 
        vy = v_start * Mathf.Sin(angle);
        t = Time.deltaTime;
    }

    void Update() {
        Vector3 change = new Vector3(t * vx, vy * t + 0.5f * g * Mathf.Pow(t, 2f) , 0);
        vy1 = vy - g * t;
        this.transform.position = Vector3.Lerp(transform.position, transform.position + change, 1);
    }
}

(3)使用transform.Translate方法

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class move1 : MonoBehaviour {  //假设斜上抛
    public float v_start = 10f;  //初始速度
    public float angle = 45f; //假设初始的角度为45°
    public float g = 10f; //重力加速度

    private float vx; //初始x轴的速度
    private float vy; //初始y轴的速度
    private float vy1; //y的顺时速度
    private float t;  //时间

    void Start() {
        vx = v_start * Mathf.Cos(angle); 
        vy = v_start * Mathf.Sin(angle);
        t = Time.deltaTime;
    }

    void Update() {
        Vector3 change = new Vector3(t * vx, vy * t + 0.5f * g * Mathf.Pow(t, 2f) , 0);
        vy1 = vy - g * t;
        this.transform.Translate(change);
    }
}
  • 写一个程序,实现一个完整的太阳系, 其他星球围绕太阳的转速必须不一样,且不在一个法平面上。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RotateAroundSun : MonoBehaviour
{
    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; //海王星
    public Transform moon;

    // Start is called before the first frame update
    void Start()
    {
        Sun.position = Vector3.zero;
    }

    // Update is called once per frame
    void Update()
    {
        Mercury.RotateAround(Sun.position, new Vector3(0,1,1), 80 * Time.deltaTime);
        Mercury.Rotate(new Vector3(0,1,1) * 5 * Time.deltaTime);

        Venus.RotateAround(Sun.position, new Vector3(0,1,2), 70 * Time.deltaTime);
        Venus.Rotate(new Vector3(0,1,2) * 10 * Time.deltaTime);

        Earth.RotateAround(Sun.position, new Vector3(0,5,1), 60 * Time.deltaTime);
        Earth.Rotate(new Vector3(0,5,1) * 15 * Time.deltaTime);

        Mars.RotateAround(Sun.position, new Vector3(0,3,1), 50 * Time.deltaTime);
        Mars.Rotate(new Vector3(0,3,1) * 20 * Time.deltaTime);

        Jupiter.RotateAround(Sun.position, new Vector3(0,10,1), 40 * Time.deltaTime);
        Jupiter.Rotate(new Vector3(0,10,1) * 25 * Time.deltaTime);

        Saturn.RotateAround(Sun.position, new Vector3(0,4,1), 30 * Time.deltaTime);
        Saturn.Rotate(new Vector3(0,4,1) * 30 * Time.deltaTime);

        Uranus.RotateAround(Sun.position, new Vector3(0,2,1), 20 * Time.deltaTime);
        Uranus.Rotate(new Vector3(0,2,1) * 35 * Time.deltaTime);

        Neptune.RotateAround(Sun.position, new Vector3(0,8,1), 10 * Time.deltaTime);
        Neptune.Rotate(new Vector3(0,8,1) * 40 * Time.deltaTime);

        moon.transform.RotateAround(Earth.position, Vector3.up, 20 * Time.deltaTime);
    }
}

视频链接:https://v.youku.com/v_show/id_XNDM2NTE2MTc2OA==.html?spm=a2h3j.8428770.3416059.1

2、编程实践

(1)游戏对象:牧师,魔鬼,船,河岸,河水

(2)玩家动作表:

条件事件
船在岸边且岸上至少有一个角色上船
船在岸边且船上至少有一个角色下船
所有牧师和魔鬼都到达对岸胜利
任意一边的岸上魔鬼数量大于牧师数量失败

(3)将游戏对象做成预制:
在这里插入图片描述

(4)根据MVC架构,将程序分为三个部分:

  • Model:

Move_model:控制魔鬼,牧师和船的移动。

Coast_model:控制角色上下船以及船的离岸和靠岸动作。

Character_model:控制牧师和魔鬼的上船、上岸等动作。

Boat_model:控制船的运动以及牧师、魔鬼的上下船动作。

  • Controller:用一个场记My_Scene_controller控制所有角色的行为以及交互,另外还有一个导演Director类实现单例模式。
  • View:与用户交互的界面,包括游戏场景,对象的生成,游戏Continue,Pause,Restart功能。两个类ClickGUI和UserGUI。

基于门面模式,有两个接口,Scene_controller和User_action。Scene_controller由Director控制

来实现游戏场景的加载,通过User_action接口来控制用户与游戏的互动。
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值