3D游戏编程与设计作业3

一、使用三种方法实现物体的抛物线运动

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值