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

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