3D游戏编程与设计3——空间与运动

1、简答并用程序验证

  • 游戏对象运动的本质是什么?
    游戏对象运动的本质是游戏对象的空间属性的改变,包括游戏对象 Transform 属性中 Position 和 Rotation 属性的改变。
public class ExampleClass : MonoBehaviour
{
   
    void Update()
    {
   
        this.transform.position += Vector3.left * Time.deltaTime;
    }
}
  • 请用三种以上方法,实现物体的抛物线运动。(如,修改 Transform 属性,使用向量 Vector3 的方法…)
    假设该物体的抛物线运动是 x 轴正方向速度保持不变,y 轴负方向保持一定加速度的运动。

① 第一种方法是通过修改 Transform 属性来实现抛物线运动。 代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class parabolaBehavior1 : MonoBehaviour
{
   
    private float xSpeed = 5f;
    private float ySpeed = 0;
    private float gravity = 9.8f;

    void Start()
    {
   
        Debug.Log("start!");
    }

    void Update()
    {
   
        this.transform.position += Vector3.right * Time.deltaTime * xSpeed;
        this.transform.position += Vector3.down * Time.deltaTime * ySpeed;
        ySpeed += gravity * Time.deltaTime;
    }
}

② 第二种方法是通过使用向量 Vector3 的方法来实现抛物线运动。代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class parabolaBehavior2 : MonoBehaviour
{
   
    private float xSpeed = 5f;
    private float ySpeed = 0;
    private float gravity = 9.8f;

    void Start()
    {
   
        Debug.Log("start!");
    }

    void Update()
    {
   
        Vector3 trans = new Vector3(xSpeed * Time.deltaTime, -ySpeed * Time.deltaTime, 0);
        this.transform.position += trans;
        ySpeed += gravity * Time.deltaTime;
    }
}

③ 第三种方法是通过使用 Translate 方法来实现抛物线运动。代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class parabolaBehavior3 : MonoBehaviour
{
   
    private float xSpeed = 5f;
    private float ySpeed = 0;
    private float gravity = 9.8f;

    void Start()
    {
   
        Debug.Log("start!");
    }

    void Update()
    {<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值