();//添加剛體組件,是一種泛型2.判斷用戶是否按下鼠標左鍵if(Inut.GetMouseButton" />

亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Unity3D開發類似保齡球游戲

系統 1984 0

先學習一些基本的腳本實現:

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);
? ? }
}

Unity3D開發類似保齡球游戲_第1張圖片


其他提示:

1.天空盒的導入,提醒不要全部導入,不然文件會很大,應用是點擊Edit-》Render Setting,然后導入天空盒

2.音頻文件是在Camera上添加Component->Audio->Audio Sourse,他自動附帶的Audio Listenner


詳細項目源碼: http://download.csdn.net/my

?

Unity3D開發類似保齡球游戲


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国内精品久久影视 | 日韩毛片免费 | 国产剧情一区二区 | 真实国产乱人伦在线视频播放 | 免费香蕉成视频成人网 | 国产午夜亚洲精品国产 | xxxxxx国产精品视频 | 亚洲一区中文字幕 | 日本三级带日本三级带黄首页 | 亚洲国产综合精品中文字幕 | 日本一区二区三区免费高清在线 | 国产精品亚洲综合一区在线观看 | a毛片基地 | 91资源在线播放 | 久久草在线免费 | 夜色91| 国产青青久久 | 免费网站看v片在线成人国产系列 | chinese国产人妖视频网站 | 日本中文不卡 | 在线观看成人小视频 | 波多野结衣久久一区二区 | 亚洲精品亚洲九十七页 | 久久精品一区二区影院 | 精品久久久久久久久久中文字幕 | 一级毛片私人影院老司机 | 鲁鲁狠色综合色综合网站 | 精品中文字幕一区在线 | 成人免费a视频 | 91久久澡人人爽人人添 | 五月天久久综合 | 亚洲人和日本人hd | 四虎影视免费看 | 能免费看黄的网站 | 成人午夜久久 | 亚洲精品一区二区三区不卡 | 一级午夜视频 | 天天尻逼 | 国产精品免费看久久久 | 五月天丁香六月欧美综合 | 久草免费精品视频 |