使用矩阵变换(平移、旋转、缩放)改变游戏对象的空间属性。
方法一:修改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); }实验效果
预置太阳系各星球 代码:
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); } }运行结果:
三个牧师、三个魔鬼、两块陆地(两岸)、一条河、一艘船、一个按钮(控制发船)
代码部分 运行实例因为文件过大无法上传,只有截图了