简答题
游戏对象运动的本质
游戏对象运动的本质,就是游戏对象跟随每一帧发生变化的过程。在变化的过程中,主要是指游戏对象的transform中的rotation与position两个属性。其中,rotation是指游戏对象所处位置的角度变化,而position则是指游戏对象在坐标系中位置的改变。
请用三种方法以上方法,实现物体的抛物线运动
要实现物体的抛物线运动,我们需要将物体在水平方向和垂直方向上分别进行位移,两者叠加之后即可实现抛物运动。在水平方向上我们让其速度保持不变,在竖直方向上让其速度逐渐增加。
我们都定义水平方向的速度为vx
而竖直方向上的速度的vy
。
第一种方法,直接利用物体position的改变来进行操作:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class paraCurve1 : MonoBehaviour {
public float vx = 1;
public float vy = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
vy += 0.5f;
this.transform.position += Vector3.right * Time.deltaTime * vx;
this.transform.position += Vector3.down * Time.deltaTime * vy;
}
}
第二种方法,我们将物体的改变用一个新的Vector3向量来表示,将其和物体原本向量叠加:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class paraCurve2 : MonoBehaviour {
public float vx = 1;
public float vy = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
vy += 0.5f;
Vector3 change = new Vector3(Time.deltaTime * vx, - Time.deltaTime * vy, 0);
this.transform.position += change;
}
}
第三种方法,我们调用transform中的translate函数来改变position,同样也会利用到Vector3向量:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class paraCurve3 : MonoBehaviour {
public float vx = 1;
public float vy = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
vy += 0.5f;
Vector3 change = new Vector3(Time.deltaTime * vx, -Time.deltaTime * vy, 0);
this.transform.Translate(change);
}
}
实现一个完整的太阳系
我们首先按照太阳系行星和它们所处的位置、大小建立一个对象树:
然后为它们添加一些简单的贴图,按照位置排列在太阳的周围:
为了实现行星绕太阳旋转,我们需要利用到RotationAround
函数来进行实现,其包括参数:旋转中心、旋转轴和旋转速度。为了简便整个代码,我们将所有旋转写在一个C
#文件里面,利用GameObject.Find
函数找到每个对象。在围绕太阳旋转的同时,我们还利用Rotation
函数给每个行星添加了自转,为了简便我们令所有行星的自转速度相同。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SolarSystem : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
GameObject.Find("Mercury").transform.RotateAround(Vector3.zero, new Vector3(1, 1, 0), 25 * Time.deltaTime