空间与运动

Mar 30, 2018


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

变换

1
2
3
void Update () {
	transform.position += Vector3.left * Time.deltaTime;
}

请用三种方法以上方法,实现物体的抛物线运动。(如,修改Transform属性,使用向量Vector3的方法…)

  • 直接改变position
1
2
3
void Update () {
	var x = transform.position.x + Time.deltaTime;
	transform.position += new Vector3 (Time.deltaTime, -(x*x+transform.position.y), 0);
  • 使用Translate方法
1
2
3
4
void Update () {
	var x = transform.position.x + Time.deltaTime;
	transform.Translate(Time.deltaTime, -(x*x+transform.position.y), 0);
}
  • 使用Vector3.MoveTowards方法
1
2
3
4
5
void Update () {
    var x = transform.position.x + Time.deltaTime;
	var target = new Vector3 (x, -x*x, 0);
	transform.position = Vector3.MoveTowards(transform.position, target, 9999);
}

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

Image text

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class main : MonoBehaviour {
	public Transform mercury, venus, earth, mars, jupiter, saturn, uranus, neptune;
	public Vector3 mercury_v, venus_v, earth_v, mars_v, jupiter_v, saturn_v, uranus_v, neptune_v;

	// Use this for initialization
	void Start () {
		mercury_v = get (); //获得各行星旋转的角度
		venus_v = get ();
		earth_v = get ();
		mars_v = get ();
		jupiter_v = get ();
		saturn_v = get ();
		uranus_v = get ();
		neptune_v = get ();
	}

	Vector3 get() {
		return new Vector3 (0, Random.Range(1,360), Random.Range(1,360));
	}

	// Update is called once per frame
	void Update () {

		//公转
		mercury.RotateAround (Vector3.zero, mercury_v, 47 * Time.deltaTime);
		venus.RotateAround (Vector3.zero, venus_v, 35 * Time.deltaTime);
		earth.RotateAround (Vector3.zero, earth_v, 30 * Time.deltaTime);
		mars.RotateAround (Vector3.zero, mars_v, 24 * Time.deltaTime);
		jupiter.RotateAround (Vector3.zero, jupiter_v, 13 * Time.deltaTime);
		saturn.RotateAround (Vector3.zero, saturn_v, 9 * Time.deltaTime);
		uranus.RotateAround (Vector3.zero, uranus_v,  6 * Time.deltaTime);
		neptune.RotateAround (Vector3.zero, neptune_v, 5 * Time.deltaTime);

		//自转
		transform.Rotate (Vector3.up * Time.deltaTime * 50);
        earth.Rotate (Vector3.up * Time.deltaTime * 250);
        mercury.Rotate (Vector3.up * Time.deltaTime * 300);
        venus.Rotate (Vector3.up * Time.deltaTime * 280);
        mars.Rotate (Vector3.up * Time.deltaTime * 220);
        jupiter.Rotate (Vector3.up * Time.deltaTime * 180);
        saturn.Rotate (Vector3.up * Time.deltaTime * 160);
        uranus.Rotate (Vector3.up * Time.deltaTime * 150);
        neptune.Rotate (Vector3.up * Time.deltaTime * 140);
	}
}

背景是一个很大的cube,用来模拟宇宙环境

公转轨道可以通过为实体添加组件实现 Add Component -> Effects -> Trail Renderer


priests-and-devils

Action Condition
上船 船上有空位,船与目标在同侧
下船 船在目标侧
开船 船上有人
  • MVC结构
    1. DIRECTOR(导演)对象与单实例模式

      DIRECTOR类用来管理场景(Scene),用以场景之间的切换

    2. SCENECONTROLLER(场记)

      SCENECONTROLLER用来管理场景里的实体,逻辑

    3. 门面(FASÀDE)模式

      将用户GUI与具体逻辑分离开来,在UserGUI中调用action。这样,UserGUI只负责GUI,action负责逻辑实现

参考资料