?????個人主頁:@元宇宙-秩沅
????? hallo 歡迎 點贊?? 收藏? 留言?? 加關(guān)注?!
????? 本文由 秩沅 原創(chuàng)
????? 收錄于專欄:unityUI專題篇
?抽象父類繼承實現(xiàn)?
??前言
???
??(A)常用關(guān)鍵API
??(B)需求分析
??(C)行為實現(xiàn)——小地圖和相機跟隨
- Target Texture 行為渲染
——————————————
___________________________
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________項目:
//___________功能: 地圖相機跟隨
//___________創(chuàng)建者:_______秩沅_______
//_____________________________________
//-------------------------------------
public class cameraFollow : MonoBehaviour
{
public Transform Tank;
private Vector3 offset;
public float heiht;
private float OffX;
private float offZ;
private void Start()
{
heiht = transform.position.y;
}
private void LateUpdate() //注意點1:相機相關(guān)邏輯存放點
{
if (Tank == null) return; //注意點2:為空判斷
//只有相機的Z軸和X軸跟著Tank變化,y則是距離地面的高度
offset.x = Tank.position.x;
offset.z = Tank.position.z;
offset.y = heiht;
gameObject.transform.position = offset;
}
}
??(D)行為實現(xiàn)——撿起武器發(fā)射炮彈
?????? 步驟:
1.靠近武器,碰撞檢測
2.將武器位置于坦克發(fā)射位置重合
3.武器中封裝了發(fā)射方法
4.發(fā)射方法中封裝了炮彈實例化并且發(fā)射的邏輯
坦克基類的封裝
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________項目:
//___________功能: 坦克基類——集中子類相同點
//___________創(chuàng)建者:______秩沅______
//___________________________________
//-------------------------------------
public abstract class TankFather : MonoBehaviour
{
//攻擊和防御相關(guān)
public int attack;
public int defence;
public int nowBlood;
public int maxBlood;
//移動和轉(zhuǎn)速相關(guān)
public int moveSpeed;
public int RotateSpeed;
public int HeadSpeed;
public Transform Head;
//擊敗特效
public GameObject diedEffect;
//受傷行為
public virtual void Heart(TankFather other)
{
//當(dāng)攻擊力大于防御力時才生效
if (other.attack - defence > 0)
{
nowBlood -= (other.attack - defence);
}
if(nowBlood <= 0 )
{
nowBlood = 0;
Death();
}
//更新血條
GamePlane.SingleInstance.UpdataBlood(maxBlood ,nowBlood );
}
//死亡行為
public virtual void Death()
{
Destroy(this);
//將特效實例化相關(guān)邏輯
GameObject effect = Instantiate(diedEffect, this.transform.position ,this.transform.rotation);
AudioSource soudClip = GetComponent<AudioSource>();
soudClip.enabled = MusicContol.SingleMusicContol.nowMusicData.soundSwitch;
soudClip.volume = MusicContol.SingleMusicContol.nowMusicData.soundRoll ;
soudClip.mute = false;
soudClip.playOnAwake = true;
}
//開火行為
public abstract void Fire(); //子類中每一個的開火方式都不同,作為抽象方法
}
主坦克的運動邏輯的封裝
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________項目:
//___________功能: 坦克的移動和旋轉(zhuǎn)
//___________創(chuàng)建者:_______秩沅_______
//_____________________________________
//-------------------------------------
public class TankSport : TankFather
{
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________項目:
//___________功能: 坦克的移動和旋轉(zhuǎn)
//___________創(chuàng)建者:_______秩沅_______
//_____________________________________
//-------------------------------------
public class MainTank : TankFather
{
//主坦克專有特征
public Weapon nowWeapon;
private Vector3 zero = new Vector3(0,0,0);
//特征屬性初始化
private void Awake()
{
Head = transform.GetChild(1).GetChild(0);
maxBlood = 100;
nowBlood = 100;
attack = 30;
defence = 10;
moveSpeed = 5;
RotateSpeed = 50;
HeadSpeed = 500;
}
private void Update()
{
//坦克的移動 = 大小*方向
transform.Translate(Input.GetAxis("Vertical") *Vector3.forward*moveSpeed *Time.deltaTime );
//坦克的旋轉(zhuǎn) = 大小*軸向
transform.Rotate(Input.GetAxis("Horizontal") *Vector3.up *RotateSpeed *Time .deltaTime );
//頭部炮管的旋轉(zhuǎn) = 大小*軸向
Head.Rotate(Input.GetAxis ("Mouse X") *Vector3.up*HeadSpeed*Time .deltaTime );
//左鍵發(fā)射炮彈
if(Input.GetMouseButtonDown(0))
{
Fire();
}
}
//撿武器行為
public void ChangeWeapon(GameObject weapon)
{
nowWeapon = weapon.GetComponent<Weapon>();
//依附成為子對象______待優(yōu)化
weapon.gameObject.transform.position = Head.GetChild(0).transform.position ;
weapon.gameObject.transform.rotation = Head.GetChild(0).transform.rotation;
weapon.gameObject.transform.SetParent(Head.GetChild(0));
attack += nowWeapon.attack;
}
//開火行為重寫
public override void Fire()
{
nowWeapon.Shoot();
}
}
武器類的封裝
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________項目: ______________
//___________功能: 武器的邏輯相關(guān)控制
//___________創(chuàng)建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class Weapon :MonoBehaviour
{
public GameObject Tank; //掛載的坦克對象
public GameObject bullet; //子彈對象
public Transform [] bulletNum; //子彈數(shù)量及位置
public int attack; //攻擊力
public float bulletSpeed = 1000f; //速度
private AudioSource shootSound; //發(fā)射音效
private void Start()
{
//加載發(fā)射音效
shootSound = GetComponent<AudioSource>();
//同步設(shè)置中音效大小
shootSound.enabled = MusicContol.SingleMusicContol.nowMusicData.soundSwitch;
shootSound.volume = MusicContol.SingleMusicContol.nowMusicData.soundRoll;
shootSound.enabled = false;
//加載子彈發(fā)射數(shù)量
bulletNum = new Transform[transform.GetChild(0).childCount];
for (int i = 0; i < transform.GetChild (0) .childCount ; i++)
{
bulletNum[i] = transform.GetChild(0).GetChild(i);
}
}
//銷毀行為
public void Vanish()
{
Destroy(this.gameObject );
}
//射擊行為
public virtual void Shoot()
{
shootSound.enabled = true;
shootSound.Play();
//實例化炮彈,并進行發(fā)射
for (int i = 0; i < bulletNum.Length ; i++)
{
GameObject ball = Instantiate(bullet, bulletNum[i].position, bulletNum[i].rotation );
BulletMove movScript = ball.GetComponent<BulletMove>();
Rigidbody shootBall = ball.GetComponent<Rigidbody>();
shootBall.AddForce(transform.forward * bulletSpeed);
movScript.Tank = Tank; //聲明子彈是由誰打出去的
}
}
//如果碰到標簽是主坦克玩家 并且 武器沒有主人
private void OnCollisionEnter(Collision collision)
{
if (Tank == null && collision.transform.name == "Player")
{
Tank = collision.gameObject;
MainTank player = collision.gameObject.GetComponent<MainTank>();
player.ChangeWeapon(this.gameObject);
print("玩家");
}
}
}
??(E)行為實現(xiàn)——炮彈的銷毀和特效
——————————
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________項目: ______________
//___________功能: 子彈的邏輯相關(guān)
//___________創(chuàng)建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class BulletMove : MonoBehaviour
{
public GameObject Tank; //掛載在哪個坦克上
public GameObject DeidEffect;
public AudioSource distorySound; //銷毀的音效
private float endTime = 15f; //銷毀倒計時
private void Start()
{
}
//子彈的銷毀及特效
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Environment"))
{
if (DeidEffect != null)
{
GameObject eff = Instantiate(DeidEffect, transform.position, transform.rotation);
//音效控制
distorySound = eff.GetComponent<AudioSource>();
distorySound.enabled = MusicContol.SingleMusicContol.nowMusicData.soundSwitch;
distorySound.volume = MusicContol.SingleMusicContol.nowMusicData.soundRoll;
eff.AddComponent<SelfDestroy>();
}
Destroy(this.gameObject);
}
}
private void Update()
{
//當(dāng)未碰撞時自動銷毀
endTimeDistory(gameObject);
}
//倒計時銷毀
public void endTimeDistory( GameObject instans)
{
endTime = Mathf.MoveTowards(endTime, 0, 0.1f);
if (endTime <= 0) Destroy(instans);
}
}
總結(jié):
問題: 為什么炮彈生成了卻無法射出 ; 原因: FreezePosition被鎖定了
問題:往前走時,發(fā)射方向和炮口方向相反
?相關(guān)文章?
?【2023unity游戲制作-mango的冒險】-6.關(guān)卡設(shè)計
?【2023unity游戲制作-mango的冒險】-5.攻擊系統(tǒng)的簡單實現(xiàn)
?【2023unity游戲制作-mango的冒險】-4.場景二的鏡頭和法球特效跟隨
?【2023unity游戲制作-mango的冒險】-3.基礎(chǔ)動作和動畫API實現(xiàn)
?【2023unity游戲制作-mango的冒險】-2.始畫面API制作
?【2023unity游戲制作-mango的冒險】-1.場景搭建
?“狂飆”游戲制作—游戲分類圖鑒(網(wǎng)易游學(xué))
?本站最全-unity常用API大全(萬字詳解),不信你不收藏文章來源:http://www.zghlxwxcb.cn/news/detail-585907.html
你們的點贊?? 收藏? 留言?? 關(guān)注?是我持續(xù)創(chuàng)作,輸出優(yōu)質(zhì)內(nèi)容的最大動力!文章來源地址http://www.zghlxwxcb.cn/news/detail-585907.html
到了這里,關(guān)于【unity之IMGUI實踐】游戲玩法邏輯實現(xiàn)【四】的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!