- 1. Short answer and program verification
- 2. Programming practice
- List things mentioned in the game (Objects)
- List player action table with table (rule table)
- Make in-game objects into prefabs
- Load and initialize rectangles, squares, balls and their colors in the LoadResources method of the scene controller to represent objects in the game
- Entire game with only the main camera and an Empty object! insert image description here
- MVC architecture
- final effect
1. Short answer and program verification
What is the nature of game object motion
The essence of game object movement The game object changes with each frame, and the space it is in changes, that is, the transformation of the space coordinates. (Including changes in position and rotation, corresponding to changes in absolute or relative positions and changes in rotation angle)
Use more than three methods to realize the parabola of the object
Method 1: Modify the transform and directly superimpose the position
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class transform : MonoBehaviour {
//初始化x方向速度1,g为9.8
public float vx = 1;
public float vy = 0;
public float g = 9.8;
void Start () {
}
// Update is called once per frame
void Update () {
vy+=g*Time.deltaTime;
this.transform.position += Vector3.down * Time.deltaTime * vy;
this.transform.position += Vector3.right * Time.deltaTime * vx;
}
}
Method 2: Create a Vector object
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Vector : MonoBehaviour {
//初始化x方向速度1,g为9.8
public float vx = 1;
public float vy = 0;
public float g = 9.8;
void Start () {
}
// Update is called once per frame
void Update () {
vy+=g*Time.deltaTime;
Vector3 v = new Vector3(vx*Time.deltaTime,-vy*Time.deltaTime,0);
this.transform.position += v;
}
}
Method 3: Use the transform.Translate method
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Vector : MonoBehaviour {
//初始化x方向速度1,g为9.8
public float vx = 1;
public float vy = 0;
public float g = 9.8;
void Start () {
}
// Update is called once per frame
void Update () {
vy+=g*Time.deltaTime;
Vector3 v = new Vector3(vx*Time.deltaTime,-vy*Time.deltaTime,0);
this.transform.translate(v);
}
}
Method 4: Use Lerp in Vector3
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Vector : MonoBehaviour {
//初始化x方向速度1,g为9.8
public float vx = 1;
public float vy = 0;
public float g = 9.8;
void Start () {
}
// Update is called once per frame
void Update () {
vy+=g*Time.deltaTime;
Vector3 v = new Vector3(vx*Time.deltaTime,-vy*Time.deltaTime,0);
this.transform.translate(v);
this.transform.position = Vector3.Lerp (transform.position, transform.position + v, 1);
}
}
Realize a complete solar system, other planets revolve around the sun at different speeds, and are not on the same normal plane
1. Create a new Sphere named Sun, take Sun as the parent object, create eight planets, find textures from the Internet, add them to Assets, and drag them to the ball.
2. Write the script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SolarSystem : MonoBehaviour
{
//用于和各行星对应
public Transform Sun;
public Transform Earth;
public Transform Mars;
public Transform Mercury;
public Transform Venus;
public Transform Jupiter;
public Transform Saturn;
public Transform Uranus;
public Transform Neptune;
// Start is called before the first frame update
void Start()
{
//初始化位置
Sun.position = new Vector3(0, 0, 0);
Earth.position = new Vector3(-6, 0, 0);
Mars.position = new Vector3(9, 0, 0);
Mercury.position = new Vector3(3, 0, 0);
Venus.position = new Vector3(5, 0, 0);
Jupiter.position = new Vector3(-11, 0, 0);
Saturn.position = new Vector3(14, 0, 0);
Uranus.position = new Vector3(-20, 0, 0);
Neptune.position = new Vector3(22, 0, 0);
}
// Update is called once per frame
void Update()
{
//public void RotateAround(Vector3 point, Vector3 axis, float angle);//公转
//Rotate函数表示自转
Earth.RotateAround(Sun.position, Vector3.up, 14 * Time.deltaTime);
Earth.Rotate(Vector3.up * 34 * Time.deltaTime);
Mars.RotateAround(Sun.position, new Vector3(0, 14, 5), 12 * Time.deltaTime);
Mars.Rotate(new Vector3(0, 13, 6) * 45 * Time.deltaTime);
Mercury.RotateAround(Sun.position, new Vector3(0, 4, 1), 16 * Time.deltaTime);
Mercury.Rotate(new Vector3(0, 5, 1) * 5 * Time.deltaTime);
Venus.RotateAround(Sun.position, new Vector3(0, 2, 1), 17 * Time.deltaTime);
Venus.Rotate(new Vector3(0, 2, 1) * Time.deltaTime);
Jupiter.RotateAround(Sun.position, new Vector3(0, 9, 4), 11 * Time.deltaTime);
Jupiter.Rotate(new Vector3(0, 10, 3) * 32 * Time.deltaTime);
Saturn.RotateAround(Sun.position, new Vector3(0, 2, 1), 10 * Time.deltaTime);
Saturn.Rotate(new Vector3(0, 3, 1) * 21 * Time.deltaTime);
Uranus.RotateAround(Sun.position, new Vector3(0, 10, 1), 8 * Time.deltaTime);
Uranus.Rotate(new Vector3(0, 10, 1) * 23 * Time.deltaTime);
Neptune.RotateAround(Sun.position, new Vector3(0, 8, 2), 6 * Time.deltaTime);
Neptune.Rotate(new Vector3(0, 9, 1) * 30 * Time.deltaTime);
}
}
3. Create a new empty object (GameObject), mount the script on the GameObject, and then drag each planet into
4. The final effect is as follows
2. Programming practice
Read the following script
Priests and Devils
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!
List things mentioned in the game (Objects)
Priest, Devil, Boat, River, Side.
List player action table with table (rule table)
action | result |
---|---|
click go | the boat moved to the other side |
click on role | Priest Death/Character Movement/Game Victory |
Make in-game objects into prefabs
Load and initialize rectangles, squares, balls and their colors in the LoadResources method of the scene controller to represent objects in the game
//加载场景
public void LoadResources()
{
left_side = new Side("left");
right_side = new Side("right");
boat = new Boat();
role_array = new List<Role>();
river = Instantiate(Resources.Load("Prefabs/Water",typeof(GameObject)),new Vector3(0, -0.5F, -0.49F), Quaternion.identity) as GameObject;
for (int i = 0; i < 3; i++)
{
Role role = new Role("priest");
role.SetName("priest" + i);
role.SetPosition(left_side.GetEmptyPosition());
role.GoSide(left_side);
left_side.AddRole(role);
role_array.Add(role);
}
for (int i = 0; i < 3; i++)
{
Role role = new Role("devil");
role.SetName("devil" + i);
role.SetPosition(left_side.GetEmptyPosition());
role.GoSide(left_side);
left_side.AddRole(role);
role_array.Add(role);
}
}
The entire game is only the main camera and an Empty object
FirstController is mounted on the empty object GameObject
MVC architecture
Respectively model (Model), controller (Controller) and interface (View)
final effect