一、使用三种方法实现物体的抛物线运动
1. 使用translate的方法
public class f1 : MonoBehaviour
{
public float xSpeed; // 水平方向初速度
public float ySpeed; // 垂直方向初速度
private float g;
// Start is called before the first frame update
void Start()
{
xSpeed = 1;
ySpeed = 0;
g = 9.8f;
}
// Update is called once per frame
void Update()
{
ySpeed = g*Time.fixedDeltaTime;
// 水平方向上
transform.Translate(Vector3.right* xSpeed * Time.fixedDeltaTime, Space.World);
transform.Translate(Vector3.down * ySpeed * Time.fixedDeltaTime, Space.World);
}
}
2. 使用新建Vector3的方法
public class f2 : MonoBehaviour
{
public int xspeed; // x方向初速度
private float g;
// Start is called before the first frame update
void Start()
{
xspeed = 1;
g = 9.8f;
}
// Update is called once per frame
void Update()
{
float x = this.transform.position.x;
float y = this.transform.position.y;
float newX = x + xspeed*Time.deltaTime;
float newY = y - g*Time.deltaTime*Time.deltaTime/2;
this.transform.position = new Vector3(newX, newY, 0);
}
}
3. 直接修改position的方法
public class f3: MonoBehaviour
{
public int xspeed;
public int yspeed;
private float g;
// Start is called before the first frame update
void Start()
{
speedx = 1;
speedy = 0;
g = 9.8f;
}
// Update is called once per frame
void Update()
{
this.transform.position += Vector3.right*xspeed;
this.transform.position += Vector3.down*(yspeed+g*Time.fixedDeltaTime);
}
}
二、模拟太阳、地球与月亮的运行轨迹
STEP1:创建三个3D精灵分别代表三个星球,并调整大小与初始距离,然后进行贴图(设置Material)以进行区分
STEP2:使用函数transform.Rotate()实现星球自转,transform.RotateAround()实现星球公转,分别编写对应地球与月球的脚本Earth.cs和Moon.cs
运行状况录屏:
太阳_地球_月亮轨迹模拟
三、代码URL
GitHub - zhenglq23/THREE: homework3
其中包括Earth.cs和Moon.cs