先學(xué)習(xí)一些基本的腳本實(shí)現(xiàn):
1.動(dòng)態(tài)創(chuàng)建物體.默認(rèn)位置是(0,0)位置
GameObject goNew = GameObject.CreatePrimitive(PrimitiveType.Cube);
//創(chuàng)建的位置
goNew.transform.position = new Vector3(0, 0, -2);
?goNew.AddComponent<Rigidbody>();//添加剛體組件,是一種泛型
2.判斷用戶是否按下鼠標(biāo)左鍵
if(Inut.GetMouseButtonDown(0))
3.按下鼠標(biāo)左鍵,給它一個(gè)往前的脈沖力,forward就是一個(gè)默認(rèn)長度為1的單位向量
this.gameObject.rigidbody.AddForce(Vector3.forward * 50, ForceMode.Impulse);
4.給當(dāng)前物體添加一個(gè)往鼠標(biāo)點(diǎn)擊的方向的多大的力,它就會往那個(gè)方向去走
?//點(diǎn)擊目標(biāo)然后從攝像機(jī)的位置發(fā)射出一個(gè)小球,這里要計(jì)算力的方向向量
?Vector3 targetPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 3));
Vector3 dir = targetPos - Camera.main.transform.position;
//給當(dāng)前的物體添加某個(gè)方向的力
this.gameObject.rigidbody.AddForce(dir * 5,ForceMode.Impulse);
5.點(diǎn)擊鼠標(biāo)生成對象
if (Input.GetMouseButtonDown(0))
{
? ? ?GameObject goNew = GameObject.CreatePrimitive(PrimitiveType.Sphere);
? ? ?goNew.transform.position = new Vector3(0, 0, 0);
? ? ? goNew.AddComponent<Rigidbody>();
}
6.對象銷毀回收內(nèi)存
if (Input.GetMouseButtonDown(0))
{
? ? ? GameObject s1 = GameObject.Find("Sphere");//相當(dāng)于document.getElementById();
? ? ? ?Destroy(s1,2); //延時(shí)2秒銷毀對象
}
制作游戲:
using UnityEngine;
using System.Collections;
public class gameText : MonoBehaviour {
? ? private GameObject goPlane;
// Use this for initialization
void Start () {
? ? ? ? //找到地形對象
? ? ? ? goPlane = GameObject.Find("Plane");
? ? ? ? //創(chuàng)建4*4的cube
? ? ? ? for (int i = 0; i < 4; i++)
? ? ? ? {
? ? ? ? ? ? for (int j = 0; j < 4; j++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
? ? ? ? ? ? ? ? go.transform.position = new Vector3(i, j, -1);
? ? ? ? ? ? ? ? go.AddComponent<Rigidbody>();
? ? ? ? ? ? ? ? go.AddComponent<AutoDistory>();//相當(dāng)于實(shí)例化一個(gè)腳本銷毀對象的一個(gè)類然后掛到每個(gè)對象中,讓他不可見的時(shí)候自行銷毀
? ? ? ? ? ? }
? ? ? ? }
}
// Update is called once per frame
void Update () {
? ? ? ? if (Input.GetMouseButtonDown(0))
? ? ? ? {
? ? ? ? ? ? //創(chuàng)建子彈的object
? ? ? ? ? ? GameObject goBullet = GameObject.CreatePrimitive(PrimitiveType.Sphere);
? ? ? ? ? ? goBullet.transform.position = Camera.main.transform.position;
? ? ? ? ? ? goBullet.AddComponent<Rigidbody>();
? ? ? ? ? ? //讓對象不可見的時(shí)候自行銷毀
? ? ? ? ? ? goBullet.AddComponent<AutoDistory>();
? ? ? ? ? ??
? ? ? ? ? ? //獲取到這個(gè)對象的多有資源,在發(fā)射的時(shí)候播放一個(gè)音樂
? ? ? ? ? ? goPlane.GetComponent<AudioSource>().Play();
? ? ? ? ? ? //點(diǎn)擊鼠標(biāo),從攝像機(jī)的位置開始發(fā)射小球
? ? ? ? ? ? Vector3 targetPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 3));
? ? ? ? ? ? goBullet.rigidbody.AddForce((targetPos - Camera.main.transform.position) * 20, ForceMode.Impulse);
? ? ? ? ? ??
? ? ? ? }
}
? ? void OnGUI()
? ? ? ? {
? ? ? ? ? ? string s = "作者:丁小未";
? ? ? ? ? ? GUIStyle bb = new GUIStyle();
? ? ? ? ? ? bb.normal.background = null;//設(shè)置背景
? ? ? ? ? ? bb.normal.textColor = new Color(1,0,0);//設(shè)置顏色
? ? ? ? ? ? bb.fontSize = 40;
? ? ? ? ? ? GUI.Label(new Rect(40, 10, 100, 50), s, bb);
??
? ? ? ? }
}
AutoDistory腳本:
using UnityEngine;
using System.Collections;
//當(dāng)東西不可見的時(shí)候就讓他自動(dòng)銷毀
public class AutoDistory : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
? ? void OnBecameInvisible()
? ? {
? ? ? ? Destroy(this.gameObject);
? ? }
}
其他提示:
1.天空盒的導(dǎo)入,提醒不要全部導(dǎo)入,不然文件會很大,應(yīng)用是點(diǎn)擊Edit-》Render Setting,然后導(dǎo)入天空盒
2.音頻文件是在Camera上添加Component->Audio->Audio Sourse,他自動(dòng)附帶的Audio Listenner
詳細(xì)項(xiàng)目源碼: http://download.csdn.net/my
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
