unity3D游戏设计 homework06
作业2:打靶游戏
游戏要求:
- 靶对象为五环,按环来计分
- 箭对象,射中后要插在靶子上
- 增强要求:射中后,箭对象产生颤抖效果,到下一次射击一秒之后
- 游戏仅有一轮,无限的trials
- 增强要求:添加一个风向和强度标志,提高难度
- 增强要求:添加一个风向和强度标志,提高难度
一、游戏效果展示
1、游戏开始界面
2、游戏进行页面
有①分数 ②风向 ③放大后的靶子
二、将动作管理相关的类改成适配物理引擎
1、重写FirstController.cs:
FirstController的主要功能函数有三个;
射箭(Shoot),产生风向(CreateWind)和更新(Update)。
这三个函数是主要实现的,还有一些成员变量,给出在下面。
- 其中风向会从东南西北四个方向吹过来,游戏用户需要通过判断风向来调整射击的方式,以获得更好的成绩。游戏刚开始的时候,风向的影响会非常小,随着游戏枫分数的升高,风向给予用户射击的难度也就越大。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FirstSceneController : MonoBehaviour, IUserAction, ISceneController {
public Camera child_camera;
public ScoreRecorder recorder;
public ArrowFactory arrow_factory;
public ArrowFlyActionManager action_manager;
public GameObject bow;
private GameObject arrow;
private GameObject target;
private int round = 0;
private bool game_over = false;
private bool game_start = false;
private string wind_name = "";
private Vector3 wind = new Vector3(0, 0, 0);
private Vector3 force;
void Start () {
SSDirector director = SSDirector.GetInstance();
arrow_factory = Singleton<ArrowFactory>.Instance;
recorder = Singleton<ScoreRecorder>.Instance;
director.CurrentScenceController = this;
action_manager = gameObject.AddComponent<ArrowFlyActionManager>() as ArrowFlyActionManager;
LoadResources();
CreateWind();
}
void Update () {
if(game_start) {
Vector3 mpos = Camera.main.ScreenPointToRay(Input.mousePosition).direction;
if (Input.GetButtonDown("Fire1")) {
Shoot(mpos * 15 );
}
if (arrow == null) {
arrow = arrow_factory.GetArrow();
arrow.transform.position = bow.transform.position;
arrow.gameObject.SetActive(true);
arrow.GetComponent<Rigidbody>().isKinematic = true;
}
bow.transform.LookAt(mpos * 30);
arrow.transform.LookAt(mpos * 30);
arrow_factory.FreeArrow();
}
}
public void LoadResources() {
bow = Instantiate(Resources.Load("Prefabs/bow", typeof(GameObject))) as GameObject;
target = Instantiate(Resources.Load("Prefabs/target", typeof(GameObject))) as GameObject;
}
public void Shoot(Vector3 force) {
if (arrow != null) {
arrow.GetComponent<Rigidbody>().isKinematic = false;
action_manager.ArrowFly(arrow, wind, force);
child_camera.GetComponent<ChildCamera>().StartShow();
arrow = null;
CreateWind();
round++;
}
}
public int GetScore() {
return recorder.score;
}
public bool GetGameover() {
return game_over;
}
public string GetWind() {
return wind_name;
}
public void CreateWind() {
float wind_directX = ((Random.Range(-10, 10) > 0) ? 1 : -1) * round;
float wind_directY = ((Random.Range(-10, 10) > 0) ? 1 : -1) * round;
Debug.Log(wind_directX);
wind = new Vector3(wind_directX, wind_directY, 0);
string Horizontal = "", Vertical = "", level = "";
if (wind_directX > 0) {
Horizontal = "西";
} else if (wind_directX <= 0) {
Horizontal = "东";
}
if (wind_directY > 0) {
Vertical = "南";
} else if (wind_directY <= 0) {
Vertical = "北";
}
level = round.ToString();
wind_name = Horizontal + Vertical + "风" + " " + level;
}
public void BeginGame() {
Cursor.visible = false;
game_start = true;
}
}
2、添加ArrowFlyAction.cs: 重要的部分在FixedUpdate函数,使用物理引擎来进行运动更新; 成员变量只需要风向和冲击力即可
public class ArrowFlyAction : SSAction {
public Vector3 _force;//冲击力
public Vector3 _wind;//风向
public static ArrowFlyAction GetSSAction(Vector3 wind, Vector3 force) {
ArrowFlyAction action = CreateInstance<ArrowFlyAction>();
action._force = force;
action._wind = wind;
return action;
}
public override void Start() {
gameobject.GetComponent<Rigidbody>().AddForce(_force, ForceMode.Impulse);
gameobject.GetComponent<Rigidbody>().AddForce(_wind);
}
//使用物理引擎
public override void FixedUpdate(){
if (transform.position.y < -30) {
this.destroy = true;
this.callback.SSActionEvent(this);
}
}
}
3、添加冲撞检测器CollisionDetection.cs: 功能是检测箭和靶子的碰撞,并计分; 主要实现的函数是OnTriggerEnter,检测并计分。 ~~~
void OnTriggerEnter(Collider arrow_head) {
Transform arrow = arrow_head.gameObject.transform.parent;
if (arrow == null) return;
arrow.GetComponent<Rigidbody>().isKinematic = true;
arrow_head.gameObject.SetActive(false);
recorder.Record(this.gameObject);
}
后续大家还可继续添加功能。