简答题
- 游戏对象运动的本质是什么?
- 请用三种方法以上方法,实现物体的抛物线运动。(如,修改Transform属性,使用向量Vector3的方法…)
- 写一个程序,实现一个完整的太阳系, 其他星球围绕太阳的转速必须不一样,且不在一个法平面上。
游戏对象运动的本质
游戏对象的运动实际上是通过不断更改位置(position)属性和旋转(rotation)属性的值,以微小差异的离散值变化,在快速刷新图像的条件下,使人视觉上产生运动的感觉。
// 以一定速度向左移动
this.transform.position += speed * Vector3.left * Time.deltaTime;
// 以一定速度向e方向旋转
Quaternion q = Quaternion.AngleAxis(speed * Time.deltaTime, e);
this.transform.localRotation *= q;
物体抛物线运动
抛物线运动可以分解为水平方向运动以及垂直方向运动,水平方向速度一定,垂直方向存在加速度。但是由于显示画面刷新快且时间间隔一定,所以可以认为在每一次 Update
函数执行时速度是不变的,存在公式ds=dv*dt
,关键是垂直方向在此时也可以认为速度不变。
(1) 第一种方法:在原position上增值
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
public float v = 1;
public float a = 1;
// Start is called before the first frame update
void Start()
{
Debug.Log("Start!");
}
// Update is called once per frame
void Update()
{
Debug.Log("Update!");
this.transform.position += Vector3.down * Time.deltaTime * v; // 竖直方向和水平方向都有 ds=dv*dt
this.transform.position += Vector3.right * Time.deltaTime * 2;
v += a; // 存在加速度,v的值不断改变
}
}
(2) 第二种方法:用新的position取代原position
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
public float v = 1; //垂直方向速度
public float a = 1;
// Start is called before the first frame update
void Start()
{
Debug.Log("Start!");
}
// Update is called once per frame
void Update()
{
Debug.Log("Update!");
this.transform.position = new Vector3(this.transform.position.x + v, this.transform.position.y, this.transform.position.z + 1);
v += a; // 存在加速度,v的值不断改变
}
}
(3) 第三种方法:使用translate函数
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
public float v = 1; // 垂直方向速度
public float a = 1;
// Start is called before the first frame update
void Start()
{
Debug.Log("Start!");
}
// Update is called once per frame
void Update()
{
Debug.Log("Update!");
Vector3 run = new Vector3(Time.deltaTime * 2, -Time.deltaTime * v, 0); // 和速度向量
this.transform.Translate(run);
v += a; // 存在加速度,v的值不断改变
}
}
完整太阳系
在unity中建立模型(红色为太阳,蓝色为地球):
Moon需要放在Earth下,作为子对象,以保证在Earth自转时,轴位置的改变不会影响到Moon的公转。
在空游戏对象中绑定脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript1 : MonoBehaviour
{
GameObject Sun, Mer, Venus, Mars, Jupiter, Saturn, Uranus, Neptune, Earth, Moon;
// Start is called before the first frame update
void Start()
{
Sun = GameObject.Find("Sun");
Mer= GameObject.Find("Mer");
Venus= GameObject.Find("Venus");
Mars= GameObject.Find("Mars");
Jupiter= GameObject.Find("Jupiter");
Saturn= GameObject.Find("Saturn");
Uranus= GameObject.Find("Uranus");
Neptune= GameObject.Find("Neptune");
Earth= GameObject.Find("Earth");
Moon= GameObject.Find("Moon");
}
// Update is called once per frame
void Update()
{
Earth.transform.RotateAround(Sun.transform.position, Vector3.up, 50 * Time.deltaTime);
Earth.transform.Rotate(Vector3.up * 30 * Time.deltaTime);
Moon.transform.RotateAround(Earth.transform.position, Vector3.up, 359 * Time.deltaTime);
Moon.transform.Rotate(Vector3.up * 30 * Time.deltaTime);
Mer.transform.RotateAround(Sun.transform.position, new Vector3(1, 5, 2), 45 * Time.deltaTime);
Mer.transform.Rotate(Vector3.up * 30 * Time.deltaTime);
Venus.transform.RotateAround(Sun.transform.position, new Vector3(1, 2, 0), 42 * Time.deltaTime);
Venus.transform.Rotate(Vector3.up * 30 * Time.deltaTime);
Mars.transform.RotateAround(Sun.transform.position, new Vector3(2, 1, 2), 40 * Time.deltaTime);
Mars.transform.Rotate(Vector3.up * 30 * Time.deltaTime);
Jupiter.transform.RotateAround(Sun.transform.position, new Vector3(1, 2, 1), 38 * Time.deltaTime);
Jupiter.t