Homework_2
Solar System
简答
游戏对象运动的本质是什么?
游戏对象坐标的变换
编程
请用三种方法以上方法,实现物体的抛物线运动。
修改 transform 属性
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29private float origin = Time.deltaTime;
private bool flag = true;
// Update is called once per frame
void Update () {
if (flag) {
if (origin - 100 * Time.deltaTime < 0.00001) {
transform.position += Vector3.down * Time.deltaTime;
transform.position += Vector3.right * Time.deltaTime;
} else if (origin - 200 * Time.deltaTime < 0.00001) {
transform.position += Vector3.up * Time.deltaTime;
transform.position += Vector3.right * Time.deltaTime;
} else {
flag = false;
}
origin += Time.deltaTime;
} else {
if (origin - 100 * Time.deltaTime > 0.00001) {
transform.position += Vector3.down * Time.deltaTime;
transform.position += Vector3.left * Time.deltaTime;
} else if (origin > 0.00001) {
transform.position += Vector3.up * Time.deltaTime;
transform.position += Vector3.left * Time.deltaTime;
} else {
flag = true;
}
origin -= Time.deltaTime;
}
}transform.Translate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15// Update is called once per frame
void Update () {
if (flag) {
if (origin - 100 * Time.deltaTime < 0.00001) {
Vector3 target = Vector3.right * Time.deltaTime + Vector3.down * Time.deltaTime;
transform.Translate(target, Space.World);
} else if (origin - 200 * Time.deltaTime < 0.00001) {
Vector3 target = Vector3.right * Time.deltaTime + Vector3.up * Time.deltaTime;
transform.Translate(target, Space.World);
} else {
flag = false;
}
origin += Time.deltaTime;
} else { /*backward*/ }
}Vector3.MoveTowards
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18private float step = Time.deltaTime;
// Update is called once per frame
void Update () {
float step = Time.deltaTime;
if (flag) {
if (origin - 100 * Time.deltaTime < 0.00001) {
Vector3 target = transform.position + Vector3.right * Time.deltaTime + Vector3.down * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target, step);
} else if (origin - 200 * Time.deltaTime < 0.00001) {
Vector3 target = transform.position + Vector3.right * Time.deltaTime + Vector3.up * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target, step);
} else {
flag = false;
}
origin += Time.deltaTime;
} else { /*backward*/ }
}Vector3.Lerp
1
2
3
4
5
6
7
8
9
10
11
12
13
14void Update {
if (flag) {
if (origin - 100 * Time.deltaTime < 0.00001) {
Vector3 target = transform.position + Vector3.right * Time.deltaTime + Vector3.down * Time.deltaTime;
transform.position = Vector3.Lerp(transform.position, target, 1);
} else if (origin - 200 * Time.deltaTime < 0.00001) {
Vector3 target = transform.position + Vector3.right * Time.deltaTime + Vector3.up * Time.deltaTime;
transform.position = Vector3.Lerp(transform.position, target, 1);
} else {
flag = false;
}
origin += Time.deltaTime;
} else { /*backward*/ }
}
写一个程序,实现一个完整的太阳系,其他星球围绕太阳的转速必须不一样,且不在一个法平面上。
思路:首先创建确定行星和太阳位置的脚本文件,然后给出行星公转的脚本。 位置脚本:
公转脚本(其中,r_x 和 r_y 随机数为实现不同法平面):1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SolarSystem : MonoBehaviour {
public Transform Sun;
public Transform Mercury;
public Transform Venus;
public Transform Earth;
public Transform EarthShadow;
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(4, 0, 0);
Venus.position = new Vector3(5, 0, 0);
Earth.position = new Vector3(7, 0, 0);
Moon.position = new Vector3(7.4f, 0, 0.4f);
Mars.position = new Vector3(9, 0, 0);
Jupiter.position = new Vector3(12, 0, 0);
Saturn.position = new Vector3(16, 0, 0);
Uranus.position = new Vector3(19, 0, 0);
Neptune.position = new Vector3(21, 0, 0);
EarthShadow.position = Earth.position;
}
}*课件中提出月球绕地球公转是地球子对象,但又不应受地球自转影响而不是其子对象的问题。参考解决方案,给出了月球作为 EarthShadow 空对象的解决方案。注意让 EarthShadow 的位置每时每刻跟随地球即可。*1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotate : MonoBehaviour {
public Transform source;
public float step;
private float r_x;
private float r_y;
// Use this for initialization
void Start () {
r_x = Random.Range(0, 0.3f);
r_y = Random.Range(0, 0.3f);
while (r_y == 0) {
r_y = Random.Range(-1, 1);
}
}
// Update is called once per frame
void Update () {
Vector3 axis;
if (source.ToString() == "EarthShadow") {
axis = new Vector3(0, 1, 0);
} else {
axis = new Vector3(r_x, r_y, 0);
}
transform.RotateAround(source.position, axis, step * Time.deltaTime);
}
}