个人主页:@元宇宙-秩沅
hallo 欢迎 点赞 收藏⭐ 留言 加关注✅!
本文由 秩沅 原创
收录于专栏:unityUI专题篇
️
——————————————
___________________________
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________项目:
//___________功能: 地图相机跟随
//___________创建者:_______秩沅_______
//_____________________________________
//-------------------------------------
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:相机相关逻辑存放点
{
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;
}
}
️ 步骤:
1.靠近武器,碰撞检测
2.将武器位置于坦克发射位置重合
3.武器中封装了发射方法
4.发射方法中封装了炮弹实例化并且发射的逻辑
坦克基类的封装
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________项目:
//___________功能: 坦克基类——集中子类相同点
//___________创建者:______秩沅______
//___________________________________
//-------------------------------------
public abstract class TankFather : MonoBehaviour
{
//攻击和防御相关
public int attack;
public int defence;
public int nowBlood;
public int maxBlood;
//移动和转速相关
public int moveSpeed;
public int RotateSpeed;
public int HeadSpeed;
public Transform Head;
//击败特效
public GameObject diedEffect;
//受伤行为
public virtual void Heart(TankFather other)
{
//当攻击力大于防御力时才生效
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);
//将特效实例化相关逻辑
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;
//-------------------------------------
//—————————————————————————————————————
//___________项目:
//___________功能: 坦克的移动和旋转
//___________创建者:_______秩沅_______
//_____________________________________
//-------------------------------------
public class TankSport : TankFather
{
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________项目:
//___________功能: 坦克的移动和旋转
//___________创建者:_______秩沅_______
//_____________________________________
//-------------------------------------
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 );
//坦克的旋转 = 大小*轴向
transform.Rotate(Input.GetAxis("Horizontal") *Vector3.up *RotateSpeed *Time .deltaTime );
//头部炮管的旋转 = 大小*轴向
Head.Rotate(Input.GetAxis ("Mouse X") *Vector3.up*HeadSpeed*Time .deltaTime );
//左键发射炮弹
if(Input.GetMouseButtonDown(0))
{
Fire();
}
}
//捡武器行为
public void ChangeWeapon(GameObject weapon)
{
nowWeapon = weapon.GetComponent<Weapon>();
//依附成为子对象______待优化
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;
//-------------------------------------
//—————————————————————————————————————
//___________项目: ______________
//___________功能: 武器的逻辑相关控制
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class Weapon :MonoBehaviour
{
public GameObject Tank; //挂载的坦克对象
public GameObject bullet; //子弹对象
public Transform [] bulletNum; //子弹数量及位置
public int attack; //攻击力
public float bulletSpeed = 1000f; //速度
private AudioSource shootSound; //发射音效
private void Start()
{
//加载发射音效
shootSound = GetComponent<AudioSource>();
//同步设置中音效大小
shootSound.enabled = MusicContol.SingleMusicContol.nowMusicData.soundSwitch;
shootSound.volume = MusicContol.SingleMusicContol.nowMusicData.soundRoll;
shootSound.enabled = false;
//加载子弹发射数量
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();
//实例化炮弹,并进行发射
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("玩家");
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________项目: ______________
//___________功能: 子弹的逻辑相关
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------
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()
{
//当未碰撞时自动销毁
endTimeDistory(gameObject);
}
//倒计时销毁
public void endTimeDistory( GameObject instans)
{
endTime = Mathf.MoveTowards(endTime, 0, 0.1f);
if (endTime <= 0) Destroy(instans);
}
}
问题: 为什么炮弹生成了却无法射出 ; 原因: FreezePosition被锁定了
⭐相关文章⭐
⭐【2023unity游戏制作-mango的冒险】-6.关卡设计
⭐【2023unity游戏制作-mango的冒险】-5.攻击系统的简单实现
⭐【2023unity游戏制作-mango的冒险】-4.场景二的镜头和法球特效跟随
⭐【2023unity游戏制作-mango的冒险】-3.基础动作和动画API实现
⭐【2023unity游戏制作-mango的冒险】-2.始画面API制作
⭐【2023unity游戏制作-mango的冒险】-1.场景搭建
⭐“狂飙”游戏制作—游戏分类图鉴(网易游学)
⭐本站最全-unity常用API大全(万字详解),不信你不收藏
你们的点赞 收藏⭐ 留言 关注✅是我持续创作,输出优质内容的最大动力!