首页 > 代码库 > unity 开发入门

unity 开发入门

---恢复内容开始---

 使用Unity开发一个打飞机的初级入门小游戏。

     实现功能:

         一.界面UI:3个界面:1开始界面,2游戏界面,3解释界面。

              1.开始界面:

 技术分享

 

主要代码:

   

using UnityEngine;
using System.Collections;

public class projectile : MonoBehaviour {
private Transform m_projectile;
private Rigidbody goprojectile;
void Start () {
m_projectile=gameObject.GetComponent<Transform> ();
goprojectile=gameObject.GetComponent<Rigidbody> ();
}

// Update is called once per frame
void Update () {

goprojectile.MovePosition (goprojectile.position+Vector3.forward*0.3f);

if (m_projectile.position.z > 19)
Destroy (gameObject);

}
void OnTrrigerEnter(Collider coll)
{
if(coll.tag== "enemy")
Destroy (coll.gameObject);
}

 

}

using UnityEngine;
using System.Collections;

public class player : MonoBehaviour {
public GameObject projectile;
private Rigidbody players;
private Transform m_transform ;
// Use this for initialization
void Start () {
m_transform = gameObject.GetComponent<Transform>();
players = gameObject.GetComponent<Rigidbody> ();


}

void Update () {
move1 ();
shot ();
}
public void shot()//发射炮弹
{

if(Input.GetKeyDown(KeyCode.J))
GameObject.Instantiate(projectile,transform.position,Quaternion.identity);

}
public void move()
{
if(Input.GetKey(KeyCode.W)) players.MovePosition(players.position+Vector3.forward*0.1f);
if(Input.GetKey(KeyCode.A)) players.MovePosition(players.position+Vector3.left*0.1f);
if(Input.GetKey(KeyCode.S)) players.MovePosition(players.position+Vector3.back*0.1f);
if(Input.GetKey(KeyCode.D)) players.MovePosition(players.position+Vector3.right*0.1f);
}

public void move1()//控制边界
{


if(transform.position.x<12&&transform.position.x>-12)// 左右边界-12,12;
{
move();

}
if (transform.position.x > 12) transform.position=new Vector3(-11.99f,transform.position.y,transform.position.z);
if (transform.position.x < -12) transform.position=new Vector3(11.99f,transform.position.y,transform.position.z);

if(transform.position.y<0&&transform.position.x>18)// 上下边界
{
move();

}
if (transform.position.y > 18) transform.position=new Vector3(transform.position.z,transform.position.y,18);
if (transform.position.y <0 ) transform.position=new Vector3(transform.position.z,transform.position.y,0);

 

 


}
void OnConllisionEnter(Collision coll)
{
Debug.Log ("entre:" + coll.gameObject.name);
}


}

using UnityEngine;
using System.Collections;

public class enemys: MonoBehaviour {
public GameObject enemy;
public GameObject boom;
void Start () {
InvokeRepeating ("Createnemy", 2.0f,3.0f);


}

void Update () {

}


void Createnemy()

{
Vector3 position = new Vector3 (Random.Range (-8, 8), 1, Random.Range (12, 16));

GameObject.Instantiate (enemy, position, Quaternion.identity);



}

}

using UnityEngine;
using System.Collections;
public class background : MonoBehaviour {
private Transform m_background;
// Use this for initialization
void Start () {
m_background=gameObject.GetComponent<Transform> ();
}

// Update is called once per frame
void Update () {
if (m_background.position.z > -12)
m_background.Translate (Vector3.back * 0.01f, Space.Self);
else
m_background.position = new Vector3 (0, 0, 36);

}
}

 

                        

---恢复内容结束---

unity 开发入门