这里写自定义目录标题
3D游戏设计 homework_3
一、
1.游戏对象运动但是本质是什么/
是游戏对象跟随每一帧的变化,空间上发生变化。这里的空间变化包括了游戏对象的transform组件中的position和rotation两个属性,前者是绝对或者相对位置的改变,后者是所处位置的角度的旋转变化。
2.请用三种以上的方法,实现物体的抛物线运动。
(1)在每一帧更新物体的position属性
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 * 10.0f;
}
}
(2)同时改变Vector3的x和y
void Update () {
Vector3 move = new Vector3(Time.deltaTime * 5, Time.deltaTime * speed, 0);
this.transform.position += move;
speed -= Time.deltaTime * 10.0f;
}
(3)利用了transform的Translate()函数
void Update()
{
Vector3 move = new Vector3(Time.deltaTime * 5, Time.deltaTime * speed, 0);
this.transform.Translate(move);
speed -= Time.deltaTime * 10.0f;
}
3、写一个程序,实现一个完整的太阳系。
(1)写行为脚本 将除月亮外的所有星球的转动行为都写在一个脚本,并将该脚本作为sun的组件,代码如下:
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);
}
}
二、编程实践(Priests ande Devils)
牧师与恶魔
牧师与魔鬼是一款益智游戏,您将帮助牧师与魔鬼在规定时间内过河。河边有三个牧师和三个魔鬼。他们都想去河的另一边,但这里只有一艘船,这艘船每次只能载两个人。而且必须有一个人把船从一边开到另一边。在游戏中,您可以单击它们移动它们,然后单击船移动到另一个方向。如果牧师的人数少于恶魔人数他们就会被杀死,游戏就结束了。目标是让所有牧师都活着。
(1)文件结构
(2)简单介绍
① 鼠标点击角色的逻辑代码
public void characterIsClicked(MyCharacterController characterCtrl) {
if (userGUI.status != 0 ) return;
if (characterCtrl.isOnBoat ()) {
CoastController whichCoast;
if (boat.get_to_or_from () == -1) { // to->-1; from->1
whichCoast = toCoast;
} else {
whichCoast = fromCoast;
}
boat.GetOffBoat (characterCtrl.getName());
characterCtrl.moveToPosition (whichCoast.getEmptyPosition ());
characterCtrl.getOnCoast (whichCoast);
whichCoast.getOnCoast (characterCtrl);
} else { // character on coast
CoastController whichCoast = characterCtrl.getCoastController ();
if (boat.getEmptyIndex () == -1) { // boat is full
return;
}
if (whichCoast.get_to_or_from () != boat.get_to_or_from ()) // boat is not on the side of character
return;
whichCoast.getOffCoast(characterCtrl.getName());
characterCtrl.moveToPosition (boat.getEmptyPosition());
characterCtrl.getOnBoat (boat);
boat.GetOnBoat (characterCtrl);
}
userGUI.status = check_game_over ();
}
②
判断游戏结束条件
int check_game_over() { // 0->not finish, 1->lose, 2->win
int from_priest = 0;
int from_devil = 0;
int to_priest = 0;
int to_devil = 0;
int[] fromCount = fromCoast.getCharacterNum ();
from_priest += fromCount[0];
from_devil += fromCount[1];
int[] toCount = toCoast.getCharacterNum ();
to_priest += toCount[0];
to_devil += toCount[1];
if (to_priest + to_devil == 6) // win
return 2;
int[] boatCount = boat.getCharacterNum ();
if (boat.get_to_or_from () == -1) { // boat at toCoast
to_priest += boatCount[0];
to_devil += boatCount[1];
} else { // boat at fromCoast
from_priest += boatCount[0];
from_devil += boatCount[1];
}
if (from_priest < from_devil && from_priest > 0) { // lose
return 1;
}
if (to_priest < to_devil && to_priest > 0) {
return 1;
}
return 0; // not finish
}