using System.Collections; using System.Collections.Generic; using UnityEngine;
public class test : MonoBehaviour { // x=0 // z=v*t // y=4-a*t*t private float x; private float y; private float z; private float v; private float a; private float t; // Start is called before the first frame update void Start() { x = 0; y = 4; z = 0; v = (float)1.6; a = (float)0.3; t = 0; }
// Update is called once per frame void Update() { this.transform.position = new Vector3(x, y, z); t += Time.deltaTime; z = v * t; y = 4 - a * t * t; } }
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class test : MonoBehaviour { private float vx; private float vy; private float a; // Start is called before the first frame update void Start() { vx = (float)1.6; a = (float)0.3; vy = 0; }
// Update is called once per frame void Update() { vy -= a * Time.deltaTime; this.transform.Translate(Vector3.forward * vx * Time.deltaTime); this.transform.Translate(Vector3.up * vy * Time.deltaTime); } }
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class test : MonoBehaviour { Vector3 v1 = new Vector3(0, 0, 2); Vector3 v2 = new Vector3(0, 0, 0);
// Start is called before the first frame update void Start() { } // Update is called once per frame void Update () { Vector3 p = transform.position + v1 * Time.deltaTime - v2 * Time.deltaTime; v2.y += 1 * (Time.deltaTime); p.y -= 0.5F * 1 * (Time.deltaTime) * (Time.deltaTime); this.transform.position = Vector3.Slerp(transform.position, p, 1); } }
方法四:Rigidbody
将Object定义为刚体,赋予重力和初速度属性。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class test : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { Rigidbody r = this.gameObject.AddComponent<Rigidbody>(); r.useGravity = true; r.velocity = Vector3.left * 2; } }
Question 1.3
写一个程序,实现一个完整的太阳系, 其他星球围绕太阳的转速必须不一样,且不在一个法平面上。
首先创建出九个星体,即太阳、月亮、地球即其他七个行星,设置好其初始位置和大小,完成结果如下图所示:
其次是设置星体的自转,自转速度是随机的,代码编写完成后将脚本挂载到行星上:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
using System.Collections; using System.Collections.Generic; using UnityEngine; // 完成自转过程 public class Rotation : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { // 自转速度随机 this.transform.RotateAround(this.transform.position, Vector3.up, Random.Range(1, 2)); } }
第三,是设置太阳系八大行星的公转,设定每个行星的公转法平面为(0,ry,rz),且 ry,rz 以及公转速度 v 是随机取定的,需把旋转中心设置为太阳;除此之外,还需添加、设置TrailRenderer,使星体的运动轨迹更为清晰:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Moon_revolution : MonoBehaviour { // 设定月球公转法平面为(0,ry,rz) // ry,rz,公转速度v 随机取定 public Transform center; public float v; float ry, rz; // Use this for initialization void Start() { v = Random.Range(100, 120); ry = Random.Range(15, 25); rz = Random.Range(15, 25); } // Update is called once per frame void Update() { this.transform.RotateAround(center.position, new Vector3(0, ry, rz), v * Time.deltaTime); } }
Priests and Devils is a puzzle game in which you will help the Priests and Devils to cross the river within the time limit. There are 3 priests and 3 devils at one side of the river. They all want to get to the other side of this river, but there is only one boat and this boat can only carry two persons each time. And there must be one person steering the boat from one side to the other side. In the flash game, you can click on them to move them and click the go button to move the boat to the other direction. If the priests are out numbered by the devils on either side of the river, they get killed and the game is over. You can try it in many > ways. Keep all priests alive! Good luck!
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class RotateAround : MonoBehaviour { // Start is called before the first frame update void Start() { Vector3 v3 = Vector3(55,5,5); float a = 10f; }
// Update is called once per frame void Update() { Quaternion T = Quaternion.LookRotation(v3 - transform.position, Vector3.up); transform.rotation = Quaternion.Slerp(transform.rotation, T, Time.deltaTime * a); } }