实验题传送门:实验题
简答并用程序验证
1. 游戏对象运动的本质是什么?
游戏对象跟随每一帧的变化,空间地变化。这里的空间变化包括了游戏对象的transform属性中的position跟rotation两个属性。一个是绝对或者相对位置的改变,一个是所处位置的角度的旋转变化。
2.请用三种方法以上方法,实现物体的抛物线运动。(如,修改Transform属性,使用向量Vector3的方法…)
1)直接改变Transform属性
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class move : MonoBehaviour
{
// Start is called before the first frame update
public float speed = -1;
void Start()
{
}
// Update is called once per frame
void Update()
{
this.transform.position += Vector3.down*Time.deltaTime*(speed/10);
this.transform.position += Vector3.left*Time.deltaTime*5;
speed++;
}
}
2)使用Vector3
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class move : MonoBehaviour
{
// Start is called before the first frame update
public float speed = -1;
void Start()
{
}
// Update is called once per frame
void Update()
{
Vector3 go = new Vector3(Time.deltaTime*5,-Time.deltaTime*(speed/10),0);
this.transform.position += go;
speed++;
}
}
3)调用其他组件的Transform属性
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class move : MonoBehaviour
{
// Start is called before the first frame update
public float speed = -1;
public Transform obj;
void Start()
{
}
// Update is called once per frame
void Update()
{
obj.position += Vector3.down*Time.deltaTime*(speed/10);
obj.position += Vector3.left*Time.deltaTime*5;
speed++;
}
}
3.写一个程序,实现一个完整的太阳系, 其他星球围绕太阳的转速必须不一样,且不在一个法平面上
贴图效果
运行脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class move : MonoBehaviour
{
// Start is called before the first frame update
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;
void Start()
{
}
// Update is called once per frame
void Update()
{
Sun.Rotate(Vector3.up * Time.deltaTime * 10000);
Earth.RotateAround(Vector3.zero, new Vector3(0, 1, 0), 30 * Time.deltaTime);//公转
Earth.Rotate(Vector3.up * Time.deltaTime * 10000);//自转
Mercury.RotateAround(Vector3.zero, new Vector3(1, 1, 0), 25 * Time.deltaTime);
Mercury.Rotate(Vector3.up * Time.deltaTime * 10000);
Venus.RotateAround(Vector3.zero, new Vector3(0, 1, 1), 20 * Time.deltaTime);
Venus.Rotate(Vector3.up * Time.deltaTime * 10000);
Mars.RotateAround(Vector3.zero, new Vector3(2, 1, 0), 45 * Time.deltaTime);
Mars.Rotate(Vector3.up * Time.deltaTime * 10000);
Jupiter.RotateAround(Vector3.zero, new Vector3(1, 2, 0), 35 * Time.deltaTime);
Jupiter.Rotate(Vector3.up * Time.deltaTime * 10000);
Saturn.RotateAround(Vector3.zero, new Vector3(0, 1, 2), 40 * Time.deltaTime);
Saturn.Rotate(Vector3.up * Time.deltaTime * 10000);
Uranus.RotateAround(Vector3.zero, new Vector3(0, 2, 1), 45 * Time.deltaTime);
Uranus.Rotate(Vector3.up * Time.deltaTime * 10000);
Neptune.RotateAround(Vector3.zero, new Vector3(1, 1, 1), 50 * Time.deltaTime);
Neptune.Rotate(Vector3.up * Time.deltaTime * 10000);
}
}
运行此脚本时需要注意配置脚本的坐标系
运行效果
最终游戏对象如图,地月间的相互运动不再具体实现