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()
{<