游戏对象运动的本质是什么
游戏对象通过脚本改变其位置,角度和大小
请用三种方法以上方法,实现物体的抛物线运动
修改Transform属性
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class move : MonoBehaviour
{
public float initSpeed = 20f;
public float angle = 45f;
public float grity = 10f;
private float xSpeed, ySpeed;
// Start is called before the first frame update
void Start(){
xSpeed = initSpeed * Mathf.Cos(angle);
ySpeed = initSpeed * Mathf.Sin(angle);
}
// Update is called once per frame
void Update() {
Vector3 mov = new Vector3(Time.deltaTime * xSpeed, Time.deltaTime * ySpeed, 0);
transform.position += mov;
ySpeed -= grity * Time.deltaTime;
}
}
使用向量Vector3方法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class move : MonoBehaviour
{
public float initSpeed = 20f;
public float angle = 45f;
public float grity = 10f;
private float xSpeed, ySpeed;
// Start is called before the first frame update
void Start(){
xSpeed = initSpeed * Mathf.Cos(angle);
ySpeed = initSpeed * Mathf.Sin(angle);
}
// Update is called once per frame
void Update() {
Vector3 mov = new Vector3(Time.deltaTime * xSpeed, Time.deltaTime * ySpeed, 0);
transform.position = Vector3.Lerp(transform.position, transform.position + mov, 1);
ySpeed -= grity * Time.deltaTime;
}
}
使用transform的translate方法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class move : MonoBehaviour
{
public float initSpeed = 20f;
public float angle = 45f;
public float grity = 10f;
private float xSpeed, ySpeed;
// Start is called before the first frame update
void Start(){
xSpeed = initSpeed * Mathf.Cos(angle);
ySpeed = initSpeed * Mathf.Sin(angle);
}
// Update is called once per frame
void Update() {
Vector3 mov = new Vector3(Time.deltaTime * xSpeed, Time.deltaTime * ySpeed, 0);
transform.Translate(mov);
ySpeed -= grity * Time.deltaTime;
}
}
写一个程序,实现一个完整的太阳系, 其他星球围绕太阳的转速必须不一样,且不在一个法平面上
步骤
- 创建九个sphere,分别命名为sun以及其他八大行星,将八大行星作为sun的子对象
- 编写脚本,代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotation : MonoBehaviour{
public float RevolutionSpeed = 1f; //公转速度
public float RotationSpeed = 30f; //自转速度
public Vector3 Axis = Vector3.up;
void Update(){
transform.RotateAround(transform.parent.position, Axis, RevolutionSpeed * Time.deltaTime);
transform.Rotate(Vector3.up * RotationSpeed * Time.deltaTime);
}
}
- 将脚本挂载到八个行星上,在UI界面中分别修改各个行星的公转速度,自转速度,角度等参数即可实现题目要求
- 为各对象添加材质包,添加背景以及轨迹
- 结果如下图
ps. 项目包含在gmae2中的SolarSystem文件夹中,由于材质包过大,删去了一部分,所以最后的效果要次于上图演示的效果
编程实践
- 阅读以下文本
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)
牧师、恶魔、船、河流、两边的河岸
用表格列出玩家动作表(规则表)
动作 | 条件 | 结果 |
---|---|---|
点击人物 | 船在当前人物所在的河岸边;如果人物在岸上,船上的人物要少于两个 | 人物上船/上岸 |
点击go按钮 | 船靠岸,且船上至少有一个人物 | 船开向对岸 |