Unity Game Design Frisbee Game

Source: Internet
Author: User

Draw up a shooting saucer game.

The specific requirements are as follows:

1 Suppose there is a gun in the camera position (0,1,-10), place three balls as distance markers in (0,0,0-10-20), adjust the angle of view until the ball is in the lower middle

2 Convert the plane coordinate of the mouse to the angle direction of the shot (sphere). Bullets use a physical engine with a constant initial speed. (U3d coordinate transformation: http://www.cnblogs.com/tekkaman/p/3809409.html)

Vector3 MP = Input.mouseposition; Get Screen Position

Print (MP. ToString ());

Vector3 MP1 = Cam.camera.ScreenToViewportPoint (MP);

Mp1.z = 10; 10-position elevation from the camera

MP1 = Cam.camera.ViewportToWorldPoint (MP1);

Print (MP1. ToString ());

3 games to be divided into multiple round, the number of UFO each round is n, but the color, size, launch position, speed, angle, the number of each launch according to the predetermined rules change.

4 Users Press space after 321 countdown 3 seconds, UFO fly out (physical engine control), click the mouse, bullets fly out. The UFO landed, or was hit, ready for the next shot.

5 Below are some technical requirements:

? bullets need only one, in deactive state when not in use

? The UFO is produced in a factory with a cache, and a pre-fabricated saucer object is placed in the template .

? Program Class diagram design is as follows:

Specific implementation:

Script implementation Bullet shooter

The script hangs on the camera

Bullet shooting idea: When the user clicks the mouse, from the camera to the mouse to create a ray, the direction of the beam is the direction of the bullet, the bullet is a rigid body components, so the firing of bullets only need to exert a force on the bullets. There is only one bullet, and the next time a bullet is fired, the position of the bullet must be changed (although a rigid body component is not recommended to modify the transform, there is no other way to change the position of the bullet). In order not to allow the bullet to inherit the speed of the last launch, the bullet must be reset to zero speed.

Bullet hit judgment: the use of radiation rather than the physical engine, because the physical engine in high-speed object collision often can not be hundred hundred detection.

Complete the UFO factory

Create a new namespace com.mygame, which is defined by the singleton classes Diskfactory and Scenecontroller. The purpose of the UFO factory class is to manage the UFO instances, as well as the extraction and recovery details of the UFO instance, and for other objects that require the use of a UFO, use only 3 functions provided by the factory class, namely Getdisk (), Getdiskobject (), and free ().

Using unityengine;using system.collections;using system.collections.generic;using com.mygame;namespace Com.Mygame { public class DiskFactory:System.Object {private static diskfactory _instance;private static list<gameobject>  Disklist;public gameobject diskprefab;public static diskfactory getinstance () {if (_instance = = null) {_instance = new  Diskfactory ();  Disklist = new list<gameobject> ();  } return _instance; }//get available UFO ID public int Getdisk () {for (int i = 0; i < Disklist.count; ++i) {if (!disklist[i].activeinhierarchy)   {return i;  UFO Idle}}//No free UFO, then instance new Frisbee preset Disklist.add (Gameobject.instantiate (Diskprefab) as Gameobject);  return disklist.count-1; }//get UFO Object public gameobject getdiskobject (int id) {if (id >= 0 && ID < disklist.count) {return disklis  T[id];  } return null; }//Reclaim UFO public void free (int id) {if (id >= 0 && ID < disklist.count) {//Reset UFO speed Disklist[id]. Getcomponent<rigidbody> (). Velocity =Vector3.zero;  Reset UFO size Disklist[id].transform.localscale = DiskPrefab.transform.localScale; Disklist[id].  SetActive (FALSE);  }}}}public class Diskfactorybasecode:monobehaviour {public gameobject disk;  void Awake () {//Initialize preset object Diskfactory.getinstance (). Diskprefab = disk; }  }

Complete the game scene

Scene class is the core class of the whole UFO shooting game, mainly responsible for the processing of UFO action. Refer to Senior design: the first need to Countdown function, can be completed by several integer variables and Boolean variables. In addition, the UFO launch function, through the setting function to save the launch information of the UFO, each time the countdown is completed, through the emitdisks to obtain the UFO object, and through the launch of information to initialize the UFO, and then a force on the UFO can be launched. The recovery of the UFO is completed in the update, one is the UFO was hit (the UFO is not in the scene), need to call judge to get points. The other is the UFO in the scene, but dropped to the ground, need to call judge lost points.

Using unityengine;using system.collections;using system.collections.generic;using com.mygame;public class GameModel:  Monobehaviour {public float countdown = 3f;public float timetoemit;private bool counting;private BOOL shooting;public BOOL Iscounting () {return counting;} public bool Isshooting () {return shooting;} Private list<gameobject> disks = new list<gameobject> ();p rivate list<int> diskids = new List<int&gt ; ();p rivate int diskscale;private Color diskcolor;private Vector3 emitposition;private Vector3 emitdirection;private float emitspeed;private int emitnumber;private bool emitenable;private scenecontroller scene;void Awake () {scene = SceneC Ontroller.getinstance (); Scene.setgamemodel (this);} public void setting (int scale, color color, Vector3 emitpos, Vector3 emitdir, float speed, int num) {Diskscale = Scale;dis Kcolor = Color;emitposition = Emitpos;emitdirection = Emitdir;emitspeed = Speed;emitnumber = num;} public void Preparetoemitdisk () {if (!counting &&!shooting) {timetoemit = Countdown;emitenable = true;}} void Emitdisks () {for (int i = 0; i < Emitnumber; i++) {Diskids.add (Diskfactory.getinstance (). Getdisk ());d isks. ADD (Diskfactory.getinstance (). Getdiskobject (Diskids [i]));d isks [I].transform.localscale *= diskscale;disks [i]. Getcomponent<renderer> (). Material.color = diskcolor;disks [i].transform.position = new Vector3 (emitPosition.x, EMITPOSITION.Y + i, emitposition.z);d isks [i]. SetActive (True);d isks [i]. Getcomponent<rigidbody> (). Addforce (Emitdirection * Random.range (Emitspeed * 5, Emitspeed *)/ten, Forcemode.impulse);}} void Freedisk (int i) {diskfactory.getinstance (). Free (diskids [i]);d isks. RemoveAt (i);d iskids.removeat (i); void Fixedupdate () {if (Timetoemit > 0) {counting = true;timetoemit-= Time.deltatime;} else {counting = False;if (emit Enable) {emitdisks (); emitenable = False;shooting = True;}}} Use the For Initializationvoid Start () {}//Update is called once per framevoid update () {for (int i = 0; i < disks. Count; i++) {if (!disks [i].activeinhierarchy) {Scene.getjudge (). Scoreadisk (); Freedisk (i);} else if (disks [I].transform.posi TION.Y < 0) {Scene.getjudge (). Failadisk (); Freedisk (i);}} if (disks. Count = = 0) {shooting = false;}}}

Scene Controller

The scene control class mainly implements the interface definition and saves the injected object. In addition, it has two private variables round and point, recording the game's ongoing rounds, as well as the player's current score.

Using unityengine;using system.collections;using com.mygame;namespace com.mygame {//com.mygame Add public interface iuserinterface {void Emitdisk ();} Public interface Iquerystatus {bool iscounting (); bool isshooting (); int getround (); int getpoint (); int getemittime ();} public class SceneController:System.Object, Iquerystatus, iuserinterface {private static Scenecontroller _instance; Private GameModel _gamemodel;private scenecontrollerbasecode _basecode;private int _round;private int _point;public Static Scenecontroller getinstance () {if (_instance = = null) {_instance = new Scenecontroller ();} return _instance;}  public void Setscenecontrollerbasecode (Scenecontrollerbasecode obj) {_basecode = obj;} Internal Scenecontrollerbasecode GETSCENECONTROLLERBC () {return _basecode;} public void Setgamemodel (GameModel obj) {_ GameModel = obj;} The current program or derived class can be internal GameModel Getgamemodel () {return _gamemodel;} public void Emitdisk () {_gamemodel.preparetoemitdisk ();} public bool Iscounting () {return _gamemOdel.iscounting ();} public bool Isshooting () {return _gamemodel.isshooting ();} public int Getround () {return _round;} public int GetPoint () {return _point;} public int Getemittime () {return (int) _gamemodel.timetoemit + 1;} public void setpoint (int point) {_point = point;} public void Nextround () {_point = 0;}}}  public class Scenecontrollerbasecode:monobehaviour {private color color;  Private Vector3 Emitpos;  Private Vector3 Emitdir;  private float speed;  void Awake () {scenecontroller.getinstance (). Setscenecontrollerbasecode (this);  } void Start () {color = Color.green;  Emitpos = new Vector3 ( -2.5f, 0.2f, -5f);  Emitdir = new Vector3 (24.5f, 40.0f, 67f); Speed = 4;  Scenecontroller.getinstance (). Getgamemodel (). setting (1, Color, emitpos, emitdir.normalized, speed, 1); }}

Perfect userinterface

Using unityengine;using unityengine.ui;using system.collections;using com.mygame;public class UserInterface: Monobehaviour {public text maintext;public text scoretext;public text roundtext;private int round;public gameobject Bulle T;public Particlesystem explosion;public Float firerate =. 25f;public float speed = 500f;private float Nextfiretime;privat E iuserinterface userint;private iquerystatus queryint;//Use this for initializationvoid Start () {bullet = Gameobject.in Stantiate (bullet) as Gameobject;explosion = Gameobject.instantiate (explosion) as Particlesystem;userint = Scenecontroller.getinstance () as Iuserinterface;queryint = Scenecontroller.getinstance () as IQueryStatus;} Update is called once per framevoid Update () {if (queryint.iscounting ()) {Maintext.text = ((int) queryint.getemittime ()). ToString ();} else {if (Input.getkeydown (Keycode.space)) {Userint.emitdisk ();} if (queryint.isshooting ()) {maintext.text = "";} else {maintext.text = "press Space";} if (queryint.isShooting () && input.getmousebuttondown (0) && time.time > Nextfiretime) {nextfiretime = Time.time + Fir erate; Ray Ray = Camera.main.ScreenPointToRay (input.mouseposition); bullet. Getcomponent<rigidbody> (). Velocity = Vector3.zero;bullet.transform.position = Transform.position;bullet. Getcomponent<rigidbody> (). Addforce (ray.direction * speed, forcemode.impulse); Raycasthit hit;if (Physics.raycast (ray, out hits) && Hit.collider.gameObject.tag = = "Disk") {Explosion.transform . Position = Hit.collider.gameobject.transform.position;explosion. Getcomponent<renderer> (). Material.color = Hit.collider.gameobject.getcomponent<renderer> (). Material.color;explosion. Play (); hit.collider.gameObject.SetActive (false);}}} Roundtext.text = "Round:" + queryint.getround (). ToString (); scoretext.text = "Score:" + queryint.getpoint (). ToString (); if (round! = Queryint.getround ()) {round = Queryint.getround (); maintext.text = "Round:" + round. ToString () + "!";}}} 

Supplemental Game Rules –judge Scoring System

The rules of the game stand alone as a class that facilitates future modifications. There are two rules that need to be dealt with, scoring and losing points. In addition, the score needs to be judged whether it can qualify for the next pass. You can call the interface function Nextround ().

Using Unityengine;  Using System.Collections;  Using Com.mygame;  public class Judge:monobehaviour {public  int onediskscore = ten;  public int onediskfail = ten;  public int diskstowin = 4;  Private Scenecontroller scene;  void Awake () {  scene = Scenecontroller.getinstance ();  Scene.setjudge (this);  }  void Start () {  scene.nextround ();  Default start first off  }  //Hit UFO score public  void Scoreadisk () {  scene.setpoint (scene.getpoint () + Onediskscore) ;  if (scene.getpoint () = = Diskstowin*onediskscore) {  scene.nextround ();  }  }  Drop UFO points public  void Failadisk () {  scene.setpoint (Scene.getpoint ()-Onediskfail);  }  }

Add code for the appropriate referee in the scene controller

Add public interface Ijudgeevent {void Nextround () in Com.mygame, void setpoint (int point);} The class is added internally and the class inherits Ijudgeeventprivate Judge _judge;  public void Setjudge (Judge obj) {_judge = obj;}  Internal Judge Getjudge () {return _judge;}

Call the referee scoring function in GameModel

void Update () {for (int i = 0; i < disks. Count; i++) {if (!disks [i].activeinhierarchy) {Scene.getjudge (). Scoreadisk (); Freedisk (i);} else if (disks [I].transform.posi TION.Y < 0) {Scene.getjudge (). Failadisk (); Freedisk (i);}} if (disks. Count = = 0) {shooting = false;}}

Set up levels

Add the level information to the Scenecontrollerbasecode and add Loadrounddata to complete the settings of the game object properties for each level.

public void Loadrounddata (Int. round) {  switch (round) {case  1:     //First off  color = color.green;  Emitpos = new Vector3 ( -2.5f, 0.2f, -5f);  Emitdir = new Vector3 (24.5f, 40.0f, 67f);  Speed = 4;  Scenecontroller.getinstance (). Getgamemodel (). setting (1, Color, emitpos, emitdir.normalized, speed, 1);  break;  Case 2:     //second off  color = color.red;  Emitpos = new Vector3 (2.5f, 0.2f, -5f);  Emitdir = new Vector3 ( -24.5f, 35.0f, 67f);  Speed = 4;  Scenecontroller.getinstance (). Getgamemodel (). setting (1, Color, emitpos, emitdir.normalized, speed, 2);  Break;case 3:     //second off  color = color.yellow;  Emitpos = new Vector3 (2.5f, 0.2f, -5f);  Emitdir = new Vector3 ( -24.5f, 35.0f, 67f);  Speed = 4;  Scenecontroller.getinstance (). Getgamemodel (). setting (1, Color, emitpos, emitdir.normalized, speed, 3);  break;  }  }

Game effect:

Reference Links:

http://blog.csdn.net/simba_scorpio/article/details/51051241

Unity Game Design Frisbee Game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.