一、陽光的收集和搜集動畫開發(fā)
1.收集陽光的思路:當鼠標點擊到陽光的時候,就可以進行收集了??梢酝ㄟ^為添加一個碰撞器來檢測Circle Collider 2D
編寫腳本:
在SunManager中寫一個增加陽光的方法
? ? //增加陽光
? ? public void AddSubSun(int Point)
? ? {
? ? ? ? sunPoint += Point;
? ? ? ? UpdataSunPointText();
? ? }
這里需要注意的是,由于地面也有碰撞器,他會優(yōu)先去檢測離相機近,如果他們一樣近陽光身上的觸發(fā)器就可能不會觸發(fā),就需要改變陽光的z坐標,讓其離相機更近。這樣就能檢測到陽光。
陽光收集的效果是,當點擊陽光后陽光移動到這個位置后銷毀陽光數(shù)量增加。
因此需要一個獲取這個組件坐標的
? ? public Vector3 GetSunPointTextPosition()
? ? {
? ? ? ? return sunPointTextPosition;
? ? }
? ? //獲取組件的坐標
? ? private void CalcSunPointTextPosition()
? ? {
? ? ? ? //將其轉(zhuǎn)換為世界坐標系
? ? ? ? Vector3 position = Camera.main.ScreenToWorldPoint(sunPointText.transform.position);
? ? ? ? position.z = 637;
? ? ? ? sunPointTextPosition = position;
? ? }
在SunFlower中寫一個鼠標點擊事件
? ? //檢測鼠標點擊到陽光
? ? public void OnMouseDown()
? ? {
? ? ? ? SunManager.Instance.AddSubSun(Point);
? ? ? ? transform.DOMove(SunManager.Instance.GetSunPointTextPosition(), moveDuration)
? ? ? ? ? ? .SetEase(Ease.OutQuad)
? ? ? ? ? ? .OnComplete(
? ? ? ? ? ?() =>
? ? ? ? ? ?{
? ? ? ? ? ? ? ?Destroy(this.gameObject);
? ? ? ? ? ?});
? ? }
當陽光移動到位置后將其銷毀。效果如下:
這樣會看到陽光被遮罩了,因此需要將Canvas的渲染模式改為屏幕空間-攝像機,這樣陽光就不會被遮蓋了。
2.陽光通過自然生成
陽光在這個范圍內(nèi)隨機生成,在這個場景的外面,生成后從上方落下來
需要一個隨機數(shù)來生成坐標,需要用到時間間隔和計時器
? ? void ProduceSun()
? ? {
? ? ? ? produceTime += Time.deltaTime;
? ? ? ? if(produceTime > produceTimer)
? ? ? ? {
? ? ? ? ? ? produceTime = 0;
? ? ? ? ? ? Vector3 position = new Vector3(Random.Range(-340, 475), 558, 636);
? ? ? ? ? ? GameObject go = GameObject.Instantiate(sunPrefab, position, Quaternion.identity);
? ? ? ? ? ? position.y = Random.Range(-260, 260);
? ? ? ? }
? ? }
一定時間間隔后就會在一個隨機位置生成一個陽光的預(yù)制體,獲取一個隨機的下路位置。生成陽光的預(yù)制體后還要讓陽光下落到隨機位置上。因此在Suncontrol腳本中需要一個用于下落的方法。
? ? public void LinearTo(Vector3 targetPodition)
? ? {
? ? ? ? transform.DOMove(targetPodition, moveDuration);
? ? }
然后調(diào)用這個方法,將這代碼補齊在ProduceSun方法的后面。
go.GetComponent<SunControl>().LinearTo(position);
代碼運行效果如下:
二.豌豆射手的開發(fā)
1.制作豌豆射手的動畫,選中豌豆射手的圖片,將其拖拽到場景中生成動畫
2.設(shè)置豌豆設(shè)置的發(fā)射功能
創(chuàng)建腳本用于控制豌豆射手,因為豌豆射手也是植物因此也要其和太陽花一樣繼承于PlantManagr
public class PesshooterConter : PlantManagr
在PlantManagr中有個EnableUpdata()方法,
? ? private void Update()
? ? {
? ? ? ? switch (plantState)
? ? ? ? {
? ? ? ? ? ? case PlantState.Disable:
? ? ? ? ? ? ? ? DisableUpdata();
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case PlantState.Enable:
? ? ? ? ? ? ? ? EnableUpdata();
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? default:
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? }
要實現(xiàn)豌豆射手的射擊功能就在EnableUpdata中進行控制。當豌豆射手被啟用的時候,去重寫EnableUpdata方法,在這個里面去控制他的射擊。
這里涉及到了,方法的重新和多態(tài)
方法重寫(Method Overriding): 子類可以提供對其父類中已定義方法的新實現(xiàn)。這就是所謂的方法重寫。EnableUpdata 方法在子類 SunFlower 中被重寫,以提供與父類中相同方法名的新實現(xiàn)
多態(tài)(Polymorphism): 多態(tài)是一種允許對象在運行時表現(xiàn)出不同形態(tài)的特性。在方法重寫的情況下,多態(tài)允許你在不知道對象具體類型的情況下調(diào)用相同的方法名,而實際上會執(zhí)行該對象實際類型的實現(xiàn)。Update 方法是多態(tài)的一個示例,因為它會根據(jù)對象的實際類型調(diào)用相應(yīng)的 EnableUpdata 或 DisableUpdata 方法。
要實現(xiàn)這個方法時間間隔和計時器,在實例化豌豆時需要一個位置,在為豌豆設(shè)置創(chuàng)建一個子物體,將其調(diào)整到合適的位置。
? ? public float shootDuration;//間隔
? ? private float shootTimer;//計時器
? ? public GameObject shootPointTransform;//子物體
重寫EnableUpdata方法,在這個里面去控制他的射擊。
? ? protected override void EnableUpdata()
? ? {
? ? ? ? shootTimer += Time.deltaTime;
? ? ? ? if(shootTimer > shootDuration)
? ? ? ? {
? ? ? ? ? ? shootTimer = 0;
? ? ? ? ? ? shoot();
? ? ? ? }
? ? }文章來源:http://www.zghlxwxcb.cn/news/detail-829370.html
當時達到時間間隔后,就會調(diào)用shoot方法進行射擊。文章來源地址http://www.zghlxwxcb.cn/news/detail-829370.html
到了這里,關(guān)于unity學(xué)習筆記----游戲練習05的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!