简答题
游戏对象运动的本质是什么?
游戏运动本质就是使用矩阵变换(平移、旋转、缩放)改变游戏对象的空间属性。
请用三种方法以上方法,实现物体的抛物线运动。(如,修改Transform属性,使用向量Vector3的方法…)
方法一:改变Transform的position属性
此处在XOZ平面上模拟物体的平抛运动
根据平抛运动的原理可知,物体在水平方向上速度不变,竖直方向上的速度由于重力不断增加,根据这一属性在每个时间区间内改变position即可,具体实现代码如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class test1 : MonoBehaviour
{
private float xspeed = 8;//x方向初速度
private float zspeed = 0;//z方向初速度
private float g = (float)9.8;//重力加速度
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
this.transform.position += Vector3.right * Time.deltaTime * xspeed;//横向x = v0*t
this.transform.position += Vector3.down * Time.deltaTime * zspeed;
zspeed += g*Time.deltaTime;
}
}
方法二:创建Vector3向量
第二种方法创建一个Vector3向量,每次把postion对该向量叠加并改变向量的y值,相当于对y方向速度不断增加,实现如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class test2 : MonoBehaviour
{
private float xspeed = 8;//x方向初速度
private float zspeed = 0;//z方向初速度
private float g = (float)9.8;//重力加速度
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Vector3 v = new Vector3(Time.deltaTime * xspeed,-Time.deltaTime * zspeed,0);
this.transform.position += v;
zspeed += g*Time.deltaTime;
}
}
方法三:使用transform.Translate方法
将方法二中对v的叠加改为transform的Translate方法来改变position即可,代码实现如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class test3 : MonoBehaviour
{
// Start is called before the first frame update
private float xspeed = 8;//x方向初速度
private float zspeed = 0;//z方向初速度
private float g = (float)9.8;//重力加速度
void Start()
{
}
// Update is called once per frame
void Update()
{
Vector3 v = new Vector3(Time.deltaTime * xspeed,-Time.deltaTime * zspeed,0);
this.transform.Translate(v);
zspeed += g*Time.deltaTime;
}
}
写一个程序,实现一个完整的太阳系, 其他星球围绕太阳的转速必须不一样,且不在一个法平面上。
首先添加9个球体对象,结构组织如下
对每个对象分别加载不同的背景材料,以此当作太阳及其八大行星
将行星运动全部写入同一个脚本挂载到main camera上即可
首先在代码的start()部分先对行星的初始位置进行设置
Update部分主要是行星运动的代码,主要用到两个函数:RotationAround跟Rotation,两个函数分别用于实现公转和自转
RotationAround的第一个参数是旋转的中心,设置为太阳位置,第二个参数是旋转轴,设置一个Vector3变量
Rotation函数的参数是旋转时的方向及速度,用Vector3.up让物体沿自身Y轴自转即可
实现代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Solar : 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()
{
//初始位置初始化
sun.position = Vector3.zero;
mercury.position = new Vector3 (3, 0, 0);
venus.position = new Vector3 (-3, 0, 0);
earth.position = new Vector3 (6, 0, 0);
mars.position = new Vector3 (-8, 0, 0);
jupiter.position = new Vector3 (-10, 0, 0);
saturn.position = new Vector3 (8, 0, 0);
uranus.position = new Vector3 (13, 0, 0);
neptune.position = new Vector3 (-15, 0, 0);
}
// Update is called once per frame
void Update()
{
mercury.RotateAround (sun.position, new Vector3(0, 3, 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, 13, 5), 9 * Time.deltaTime);
mars.Rotate (new Vector3(0, 12, 5) * 40 * Time.deltaTime);
jupiter.RotateAround (sun.position, new Vector3(0, 8, 3), 8 * Time.deltaTime);
jupiter.Rotate (new Vector3(0, 10, 3) * 30 * Time.deltaTime);
saturn.RotateAround (sun.position, new Vector3(0, 2, 1), 7 * Time.deltaTime);
saturn.Rotate (new Vector3(0, 3, 1) * 20 * Time.deltaTime);
uranus.RotateAround (sun.position, new Vector3(0, 9, 1), 6 * Time.deltaTime);
uranus.Rotate (new Vector3(0, 10, 1) * 20 * Time.deltaTim