作业内容
1、简答并用程序验证【建议做】
- 游戏对象运动的本质是什么?
游戏对象运动的本质是:游戏对象在游戏每一帧的渲染过程中Transform属性在发生变化。这里物体的Transform属性是指Position与Rotation两个属性。 - 请用三种方法以上方法,实现物体的抛物线运动。(如,修改Transform属性,使用向量Vector3的方法…)
第一种方法,直接通过改变物体的Transform的position属性让物体实现抛物线运动。代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class parabola : MonoBehaviour {
public float speed1 = 4;
public float speed2 = 0;
public float speed = 3;
// Use this for initialization
void Start () {
Debug.Log("Init start");
}
// Update is called once per frame
void Update () {
if(speed1 >= 0){
speed1 = speed1 - 10 * Time.deltaTime;
this.transform.position += Vector3.up * Time.deltaTime * speed1 / 2;
}
else{
speed2 = speed2 + 10 * Time.deltaTime;
this.transform.position += Vector3.down * Time.deltaTime * speed2 / 2;
}
this.transform.position += Vector3.left * Time.deltaTime * speed;
}
}
第二种方法,使用transform中的translate方法改变对象的位置。代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class parabola : MonoBehaviour {
public float speed1 = 4;
public float speed2 = 0;
public float speed = 3;
// Use this for initialization
void Start () {
Debug.Log("Init start");
}
// Update is called once per frame
void Update () {
if(speed1 >= 0)
{
speed1 = speed1 - 10 * Time.deltaTime;
transform.Translate(Vector3.up * Time.deltaTime * speed1 / 2, Space.World);
}
else
{
speed2 = speed2 + 10 * Time.deltaTime;
transform.Translate(Vector3.down * Time.deltaTime * speed2 / 2, Space.World);
}
transform.Translate(Vector3.left * Time.deltaTime * speed);
}
}
第三种方法,新建一个vector3对象,改变这个对象的值,然后直接赋给物体。代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class parabola : MonoBehaviour {
public float speed1 = 4;
public float speed2 = 0;
public float speed = 3;
// Use this for initialization
void Start () {
Debug.Log("Init start");
}
// Update is called once per frame