标签:blog http io ar os sp for on div
这次的作业是二选一,我综合了自己的兴趣以及情况,我选择了做一个3D游戏来完成。在查阅了很多资料之后,我选择了Unity3d来学习,因为它自带强大的编辑器,易于我们这些新人上手,在学习了两个星期后,我决定做一个之前很火的flappy bird来练手,下面是具体完成的情况。
设计思路:利用U3D自带的物理碰撞引擎去检测碰撞事件,在每帧重画的时候去选择小鸟形态的切换,给小鸟的一个水平的速度,然后在场景上设置一个触发器,负责场景的延生,这样游戏就可以源源不断的进行下去。
主要代码如下:
using UnityEngine; using System.Collections; public class Bird : MonoBehaviour { public float timer=0; public int frameNumber= 10;// frame number one seconds public int frameCount=0;// frame counter public bool animation=false;// whether play the fly animation public bool canJump=false; void Start(){ //this.rigidbody.velocity = new Vector3(2,0,0); } // Update is called once per frame void Update () { //hard code here for test // Vector3 vel = this.rigidbody.velocity; // this.rigidbody.velocity = new Vector3(5,vel.y,vel.z); // animation if(GameManager._intance.GameState==GameManager.GAMESTATE_PLAYING){ timer+=Time.deltaTime; if(timer>=1.0f/frameNumber){ frameCount++; timer-=1.0f/frameNumber; int frameIndex = frameCount%3; // update material ‘s offset x // how to set the property of(x offset) MainTex : Main Texture this.renderer.material.SetTextureOffset("_MainTex",new Vector2(0.333333f*frameIndex,0)); //this.renderer.material.SetTextureScale("_MainText",new Vector2(1,1)); } } if(GameManager._intance.GameState==GameManager.GAMESTATE_PLAYING){ // control jump if(Input.GetMouseButton(0) ){// left mouse button down audio.Play(); Vector3 vel2 = this.rigidbody.velocity; this.rigidbody.velocity = new Vector3(vel2.x,5,vel2.z); } } } public void getLife(){ rigidbody.useGravity=true; this.rigidbody.velocity = new Vector3(2,0,0); } }
using UnityEngine; using System.Collections; public class GameManager : MonoBehaviour { // define the state of the game public static int GAMESTATE_MENU =0;//游戏菜单状态 public static int GAMESTATE_PLAYING =1;//游戏中状态 ... public static int GAMESTATE_END =2;//游戏结束状态 public Transform firstBg; public int score = 0; public int GameState = GAMESTATE_MENU; public static GameManager _intance; private GameObject bird; void Awake(){ _intance = this; bird = GameObject.FindGameObjectWithTag("Player"); } void Update(){ if(GameState==GameManager.GAMESTATE_MENU){ if(Input.GetMouseButtonDown(0)){ // transform state GameState = GAMESTATE_PLAYING; // set bird is playing // 1.set gravity 2.add velocity of x bird.SendMessage("getLife"); } } if (GameState == GameManager.GAMESTATE_END) { GameMenu._instance.gameObject.SetActive(true); GameMenu._instance.UpdateScore(score); } } }
using UnityEngine; using System.Collections; public class MoveTrigger : MonoBehaviour { public Transform currentBg; public Pipe pipe1; public Pipe pipe2; public void OnTriggerEnter(Collider other){ print("OnTriggerEnter"); if(other.tag=="Player") { // move the bg to the front of first transform //1. get the first transfrom Transform firstBg= GameManager._intance.firstBg; // 2. move currentBg.position = new Vector3( firstBg.position.x+10,currentBg.position.y,currentBg.position.z); GameManager._intance.firstBg = currentBg; // new position for pipe pipe1.RandomGeneratePosition(); pipe2.RandomGeneratePosition(); } } }
心得体会:
通过这次U3D的学习,我有机会去了解3d程序的基本概念以及开发过程,在学习的过程中,我遇到了很多问题,通过查阅资料与咨询他人将这些问题都一一解决。从此之后,我觉得自己对编程也抱有更大的热情,受益匪浅。
标签:blog http io ar os sp for on div
原文地址:http://www.cnblogs.com/ztypeer/p/4127208.html