3D游戏编程:空间与运动

简答题

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

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

游戏对象的位置由Transform部件来决定,当Transform的属性,如position, eulerAngles, scale, rotation等改变时游戏对象位置改变,而采用向量运算可以进行对位置数值上的更改.结合很快的迭代时间,使游戏对象看起来像是在运动.
2.请用三种方法以上方法,实现物体的抛物线运动。(如,修改Transform属性,使用向量Vector3的方法…)
①直接更改position实现

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

public class Move : MonoBehaviour {

    public int speed = 1;
	// Use this for initialization
	void Start () {
		
	}
	// Update is called once per frame
	void Update () {
        this.transform.position += speed * Vector3.left * Time.deltaTime;
        this.transform.position += speed * speed * Vector3.down * Time.deltaTime;
    }
}

②使用translate实现

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

public class Move2 : MonoBehaviour
{
    // Use this for initialization
    void Start()
    {
        this.transform.position = new Vector3(0, 0, 0);
    }
    // Update is called once per frame
    void Update()
    {
        //Debug.Log(this.transform.position);
        this.transform.Translate(Vector3.left * Time.deltaTime);
        this.transform.Translate(Vector3.down * ( this.transform.position.x * this.transform.position.x) * Time.deltaTime);
    }
}

③使用MoveTowards实现

returns:Vector3 The new position.
To make sure that object speed is independent of frame rate, multiply the maxDistanceDelta value by Time.deltaTime (or Time.fixedDeltaTime in a FixedUpdate loop).

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

public class Move3 : MonoBehaviour {

    public float speed = 10;
    // Use this for initialization
    void Start () {
        this.transform.position = new Vector3(0, 0, 0);
	}
	
	// Update is called once per frame
	void Update () {
        float step = speed * Time.deltaTime;
     //   var next_pos = new Vector3(this.transform.position.x+Time.deltaTime, this.transform.position.y - (3*this.transform.position.x * this.transform.position.x)*Time.deltaTime, this.transform.position.z);
        var next_pos = new Vector3(this.transform.position.x+speed/2, this.transform.position.y - (3 * this.transform.position.x * this.transform.position.x), this.transform.position.z);
        this.transform.position = Vector3.MoveTowards(this.transform.position, next_pos, step);
	}
}

3.写一个程序,实现一个完整的太阳系, 其他星球围绕太阳的转速必须不一样,且不在一个法平面上。
①先实现一下地球绕太阳转的模型

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

public class Rounds : MonoBehaviour {

    public Transform sun;
    public Transform earth;
    public Transform moon;
	// Use this for initialization
	void Start () {
        sun.position = Vector3.zero;
        earth.position = new Vector3(6, 0, 0);
        moon.position = new Vector3(8, 0, 0);
	}
	
	// Update is called once per frame
	void Update () {
        earth.RotateAround(sun.position, Vector3.up, 10 * Time.deltaTime);
        earth.Rotate(Vector3.up * 30 * Time.deltaTime);
        moon.RotateAround(earth.position, Vector3.up, 359 * Time.deltaTime);
	}
}

②按照上面的框架,增加星球,并设置各自的旋转轴以使法平面不同,可得太阳系模型
在这里插入图片描述
代码如下:

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

public class RoundSunall : MonoBehaviour {

    public Transform sun;
    public Transform mercury;
    public Transform venus;
    public Transform earth;
    public Transform moon;
    public Transform mars;
    public Transform jupiter;
    public Transform saturn;
    public Transform uranus;
    public Transform neptune;
    // Use this for initialization
    void Start () {
        sun.position = Vector3.zero;
        mercury.position = new Vector3(5, 0.2f, 0.02f);
        venus.position = new Vector3(8, 0.03f, 0.3f);
        earth.position = new Vector3(11, 0.07f, 0.7f);
        moon.position = new Vector3(12.2f, 0.07f, 0.7f);
        mars.position = new Vector3(14, 0.09f, 0.9f);
        jupiter.position = new Vector3(16, 0.8f, 0.08f);
        saturn.position = new Vector3(20, 0.06f, 0.6f);
        uranus.position = new Vector3(27, 0.3f, 0.39f);
        neptune.position = new Vector3(35, 0.7f, 0.79f);
	}

    // Update is called once per frame
    void Update()
    {
        earth.RotateAround(sun.position, Vector3.up, 10 * Time.deltaTime);
        earth.Rotate(Vector3.up * 30 * Time.deltaTime);
        moon.RotateAround(earth.position, Vector3.up, 365 * Time.deltaTime);
//        moon.RotateAround(earth.position, new Vector3(0.05f, 1, 0), 365 * Time.deltaTime);
        mercury.RotateAround(sun.position, new Vector3(0.3f, 1, 0), 10 * 365 / 87.7f * Time.deltaTime);
        venus.RotateAround(sun.position, new Vector3(0.2f, 1, 0), 10 * 365 / 224.7f * Time.deltaTime);
        mars.RotateAround(sun.position, new Vector3(0.5f, 1, 0), 10 * 365 / 686.98f * Time.deltaTime);
        jupiter.RotateAround(sun.position, new Vector3(0.5f, 1, 0), 10 * 1 / 11.8f * Time.deltaTime);
        saturn.RotateAround(sun.position, new Vector3(0.6f, 1, 0), 10 * 1 / 29.5f * Time.deltaTime);
        uranus.RotateAround(sun.position, new Vector3(0.23f, 1, 0), 10 * 1 / 80.4f * Time.deltaTime);
        neptune.RotateAround(sun.position, new Vector3(0.17f, 1, 0), 10 * 1 / 164.8f * Time.deltaTime);
    }
}

编程实践

  • 阅读以下游戏脚本
    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 )
  • 列出游戏中提及的事物(Objects)
  • 用表格列出玩家动作表(规则表),注意,动作越少越好
  • 请将游戏中对象做成预制
  • 在 GenGameObjects 中创建 长方形、正方形、球 及其色彩代表游戏中的对象。
  • 使用 C# 集合类型 有效组织对象
  • 整个游戏仅 主摄像机 和 一个 Empty 对象, 其他对象必须代码动态生成!!! 。 整个游戏不许出现 Find 游戏对象, SendMessage 这类突破程序结构的 通讯耦合 语句。 违背本条准则,不给分
  • 请使用课件架构图编程,不接受非** MVC 结构**程序
  • 注意细节,例如:船未靠岸,牧师与魔鬼上下船运动中,均不能接受用户事件!

实现代码如下:
Scripts

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值