1.简答并用程序验证
游戏对象运动的本质是什么
游戏对象运动的本质游戏对象随着每一帧的变化,所处空间的变化,即空间坐标的变换。(包括position和rotation的变化,对应绝对位置或相对位置的变化和旋转角度的变化)
用三种以上方法实现物体的抛物线
方法一:修改transform,直接对position叠加
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class transform : MonoBehaviour {
//初始化x方向速度1,g为9.8
public float vx = 1;
public float vy = 0;
public float g = 9.8;
void Start () {
}
// Update is called once per frame
void Update () {
vy+=g*Time.deltaTime;
this.transform.position += Vector3.down * Time.deltaTime * vy;
this.transform.position += Vector3.right * Time.deltaTime * vx;
}
}
方法二:创建一个Vector对象
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Vector : MonoBehaviour {
//初始化x方向速度1,g为9.8
public float vx = 1;
public float vy = 0;
public float g = 9.8;
void Start () {
}
// Update is called once per frame
void Update () {
vy+=g*Time.deltaTime;
Vector3 v = new Vector3(vx*Time.deltaTime,-vy*Time.deltaTime,0);
this.transform.position += v;
}
}
方法三:使用transform.Translate方法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Vector : MonoBehaviour {
//初始化x方向速度1,g为9.8
public float vx = 1;
public float vy = 0;
public float g = 9.8;
void Start () {
}
// Update is called once per frame
void Update () {
vy+=g*Time.deltaTime;
Vector3 v = new Vector3(vx*Time.deltaTime,-vy*Time.deltaTime,0);
this.transform.translate(v);
}
}
方法四:使用Vector3中的Lerp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Vector : MonoBehaviour {
//初始化x方向速度1,g为9.8
public float vx = 1;
public float vy = 0;
public float g = 9.8;
void Start () {
}
// Update is called once per frame
void Update () {
vy+=g*Time.deltaTime;
Vector3 v = new Vector3(vx*Time.deltaTime,-vy*Time.deltaTime,0);
this.transform.translate(v);
this.transform.position = Vector3.Lerp (transform.position, transform.position + v, 1);
}
}
实现一个完整的太阳系,其他星球围绕太阳转速不一致,且不在一个法平面上
1.新建一个Sphere命名为Sun,以Sun为父对象,创建八个行星,从网络上寻找贴图加入到Assets中,拖到球上。
2.编写脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SolarSystem : MonoBehaviour
{
//用于和各行星对应
public Transform Sun;
public Transform Earth;
public Transform Mars;
public Transform Mercury;
public Transform Venus;
public Transform Jupiter;
public Transform Saturn;
public Transform Uranus;
public Transform Neptune;
// Start is called before the first frame update
void Start()
{
//初始化位置
Sun.position = new Vector3(0, 0, 0);
Earth.position = new Vector3(-6, 0, 0);
Mars.position = new Vector3(9, 0, 0);
Mercury.position = new Vector3(3, 0, 0);
Venus.position = new Vector3(5, 0, 0);
Jupiter.position = new Vector3(-11, 0, 0);
Saturn.position = new Vector3(14, 0, 0);
Uranus.position = new Vector3(-20, 0, 0);
Neptune.position = new Vector3(22, 0, 0);
}
// Update is called once per frame
void Update()
{
//public void RotateAround(Vector3 point, Vector3 axis, float angle);//公转
//Rotate函数表示自转
Earth.RotateAround(Sun.position, Vector3.up, 14 * Time.deltaTime);
Earth.Rotate(Vector3.up * 34 * Time.deltaTime);
Mars.RotateAround(Sun.position, new Vector3(0, 14, 5), 12 * Time.deltaTime);
Mars.Rotate(new Vector3(0, 13, 6) * 45 * Time.deltaTime);
Mercury.RotateAround(Sun.position, new Vector3(0, 4, 1), 16 * Time.deltaTime);
Mercury.Rotate(new Vector3(0, 5, 1) * 5 * Time.deltaTime);
Venus.RotateAround(Sun.position, new Vector3(0, 2, 1), 17 * Time.deltaTime);
Venus.Rotate(new Vector3(0, 2, 1) * Time.deltaTime);
Jupiter.RotateAround(Sun.position, new Vector3(0, 9, 4), 11 * Time.deltaTime);
Jupiter.Rotate(new Vector3(0, 10, 3) * 32 * Time.deltaTime);
Saturn.RotateAround(Sun.position, new Vector3(0, 2, 1), 10 * Time.deltaTime);
Saturn.Rotate(new Vector3(0, 3, 1) * 21 * Time.deltaTime);
Uranus.RotateAround(Sun.position, new Vector3(0, 10, 1), 8 * Time.deltaTime);
Uranus.Rotate(new Vector3(0, 10, 1) * 23 * Time.deltaTime);
Neptune.RotateAround(Sun.position, new Vector3(0, 8, 2), 6 * Time.deltaTime);
Neptune.Rotate(new Vector3(0, 9, 1) * 30 * Time.deltaTime);
}
}
3.新建一个空对象(GameObject),把脚本挂载到GameObject上,然后将各个行星拖入
4.最后效果如下
2.编程实践
阅读以下脚本
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!
列出游戏中提及的事物(Objects)
牧师(Priest)、魔鬼(Devil)、船(Boat)、河流(River)、左右河岸(Side)。
用表格列出玩家动作表(规则表)
动作 | 结果 |
---|---|
点击go | 船移到对岸 |
点击角色 | 牧师死亡/角色移动/游戏胜利 |
将游戏中对象做成预制
在场景控制器 LoadResources 方法中加载并初始化 长方形、正方形、球 及其色彩代表游戏中的对象
//加载场景
public void LoadResources()
{
left_side = new Side("left");
right_side = new Side("right");
boat = new Boat();
role_array = new List<Role>();
river = Instantiate(Resources.Load("Prefabs/Water",typeof(GameObject)),new Vector3(0, -0.5F, -0.49F), Quaternion.identity) as GameObject;
for (int i = 0; i < 3; i++)
{
Role role = new Role("priest");
role.SetName("priest" + i);
role.SetPosition(left_side.GetEmptyPosition());
role.GoSide(left_side);
left_side.AddRole(role);
role_array.Add(role);
}
for (int i = 0; i < 3; i++)
{
Role role = new Role("devil");
role.SetName("devil" + i);
role.SetPosition(left_side.GetEmptyPosition());
role.GoSide(left_side);
left_side.AddRole(role);
role_array.Add(role);
}
}
整个游戏仅 主摄像机 和 一个 Empty 对象![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d34e7dbb999ae8968bed606ca06c2ccd.png#pic_center)
FirstController挂载在空对象GameObject上
MVC架构
分别为模型(Model)、控制器(Controller)和界面(View)
最终效果