1、游戏对象运动的本质是什么?
2、请用三种方法以上方法,实现物体的抛物线运动。(如,修改Transform属性,使用向量Vector3的方法…)
总体思路是:在UpDate()中实现行为,在每一帧更新物体的位置,使其在水平方向匀速运动,竖直方向匀加速运动。
以下用三种方法实现向右上方抛物的运动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class move : MonoBehaviour {
public float speed = 10;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
this.transform.position += Vector3.up * Time.deltaTime * speed;
this.transform.position += Vector3.right * Time.deltaTime * 5;
speed -= Time.deltaTime * 9.8f;
}
}
方法二 与方法一类似,只不过直接同时改变Vector3的x和y(以下仅展示关键代码)
void Update () {
Vector3 move = new Vector3(Time.deltaTime * 5, Time.deltaTime * speed, 0);
this.transform.position += move;
speed -= Time.deltaTime * 9.8f;
}
方法三 与方法二类似,不过利用了transform的Translate()函数
void Update()
{
Vector3 move = new Vector3(Time.deltaTime * 5, Time.deltaTime * speed, 0);
this.transform.Translate(move);
speed -= Time.deltaTime * 9.8f;
}
3、写一个程序,实现一个完整的太阳系, 其他星球围绕太阳的转速必须不一样,且不在一个法平面上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Sun : MonoBehaviour {
public Transform sun;
public Transform Mercury;
public Transform Venus;
public Transform Earth;
public Transform Mars;
public Transform Jupiter;
public Transform Saturn;
public Transform Uranus;
public Transform Neptune;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Mercury.Rotate(Vector3.up * Time.deltaTime * 100);
Mercury.RotateAround(sun.position, new Vector3(0, 1, 0), 100 * Time.deltaTime);
Venus.Rotate(Vector3.up * Time.deltaTime * 100);
Venus.RotateAround(sun.position, new Vector3(0.1f, 1, 0), 90 * Time.deltaTime);
Earth.Rotate(Vector3.up * Time.deltaTime * 100);
Earth.RotateAround(sun.position, new Vector3(0.2f, 1, 0), 80 * Time.deltaTime);
Mars.Rotate(Vector3.up * Time.deltaTime * 100);
Mars.RotateAround(sun.position, new Vector3(0.3f, 1, 0), 70 * Time.deltaTime);
Jupiter.Rotate(Vector3.up * Time.deltaTime * 100);
Jupiter.RotateAround(sun.position, new Vector3(0, 1, 0.1f), 60 * Time.deltaTime);
Saturn.Rotate(Vector3.up * Time.deltaTime * 100);
Saturn.RotateAround(sun.position, new Vector3(0, 1, 0.2f), 50 * Time.deltaTime);
Uranus.Rotate(Vector3.up * Time.deltaTime * 100);
Uranus.RotateAround(sun.position, new Vector3(0, 1, 0.3f), 40 * Time.deltaTime);
Neptune.Rotate(Vector3.up * Time.deltaTime * 100);
Neptune.RotateAround(sun.position, new Vector3(0.1f, 1, 0.1f), 30 * Time.deltaTime);
}
}
注:其中的public属性可以在Unity右侧栏进行赋值,如下
因为地球的公转时月球应作为其子对象,而地球自转时则不然,故因创建一个空对象作为月球的父对象,使其有跟地球一样的位置和公转行为,但没有自转,其脚本代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Vearth : MonoBehaviour {
public Transform sun;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
this.transform.RotateAround(sun.position, new Vector3(0.2f, 1, 0), 80 * Time.deltaTime);
}
}
moon的脚本代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Moon : MonoBehaviour {
public Transform Vearth;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
this.transform.RotateAround(Vearth.position, Vector3.up, 500 * Time.deltaTime);
}
}