3DTank大战总结

预制体修改可以放入场景中修改再应用

1.transform.Find()的用法

transform.Find 只能找到所在物体的子物体
在这里插入图片描述
脚本挂在car游戏物体上

Debug.Log(transform.Find(“test”)); 

结果是null空值。
想要查找子辈的子辈怎么办,需要指定全路径

Debug.Log(transform.Find(“GameObject (3)/test”));

即可找到。

2.初始化子弹,并控制子弹移动

创建一个空物体GameObject改为FirePosition,并放在Tank下,作为初始化子弹发射的位置,

Instantiate(物体,位置,方向)

public class TankAttack : MonoBehaviour
{
	public GameObject shell;//声明子弹的引用
	public KeyCode fireKey=KeyCode.Space;//声明KeyCode按键,默认空格键为子弹发射键,另一个玩家可以在外部修改发射按键

	private Transform firePosition;//声明初始化子弹的位置引用,通过上图的FirePosition物体赋值

	void Start()
    {
        firePosition = transform.Find("FirePosition");//transform.Find查找当前物体的子物体
    }

	if(Input.GetKeyDown(fireKey))
	{
	GameObject  go=Instantiate(shell, firePosition.position, firePosition.rotation);//实例化子弹,确定位置、旋转
	go.GetComponent<Rigidbody>().velocity = go.transform.forward * shellSpeed;//刚体移动
	}
}

3.触发器碰撞检测,销毁物体

实例化子弹爆炸效果,销毁子弹
子弹触发器脚本

	/*脚本在子弹上*/
public class Shell : MonoBehaviour
{
	public GameObject ShellExplosion;//定义子弹爆炸效果引用
    private void OnTriggerEnter(Collider other)//检测函数
    {
    	//子弹一碰到其他任何物体就产生爆炸效果,并销毁子弹
        Instantiate(ShellExplosion,transform.position,transform.rotation);//初始化子弹爆炸效果
        Destroy(gameObject);//销毁子弹
        if (collider.tag =="Tank")//子弹碰到坦克
        {
            collider.SendMessage("TankDamage");//执行TankDamage方法
        }
    }
}
/*脚本在子弹爆炸效果上*/
public class ShellExplosionDestory : MonoBehaviour
{
    public float _time;//爆炸效果持续时间,外部赋值
    void Start()
    {
        Destroy(gameObject, _time);//爆炸效果持续时间一过,销毁爆炸效果
    }
}
/*脚本在Tank上*/
public class TankAttack : MonoBehaviour
{
	private void TankDamage()
    {
        if (hp <= 0) return;
        hp -= 10;//坦克减少血量10(设置100血量)
        hpSlider.value = (float)hp / 100;//改变血条
        if (hp<=0)
        {
            Instantiate(TankExplosion,transform .position + Vector3.up , transform.rotation);//实例化爆炸效果
            Destroy(gameObject);//销毁坦克游戏物体
        }
    }
}

子弹碰到任何物体,都会产生子弹爆炸效果并销毁子弹,实例化子弹爆炸效果后会销毁(子弹爆炸效果物体上挂有销毁脚本)。如果子弹碰到Tank,执行TankDamage();

圆形血条制作

1.在画布上添加slider
在这里插入图片描述
2.删除Handle Slide Area
3.更改Source Image
在这里插入图片描述在这里插入图片描述
4.Background,Fill上下左右拉伸
在这里插入图片描述
在这里插入图片描述
Image Type选择Filled,Canvas的Render Mode(渲染模式)设为Word Space
在这里插入图片描述
修改Value值可以修改血条

public Slider hpSlider;//声明Slider 组件引用
hpSlider.value = (float)hp / 100;//改变血条

4.如何控制两个玩家

/*脚本挂在Tank上*/
public class TankMove : MonoBehaviour
{
	public int playerNumber=1;//定义玩家1,2

	void FixedUpdate()
    {
        float v = Input.GetAxisRaw("VerticalPlayer"+ playerNumber);//在Input面板设置两个Player的控制键
        rig.velocity = transform.forward * v* speed;//刚体的运动速度
        float h= Input.GetAxisRaw("HorizontalPlayer"+playerNumber);
        rig.angularVelocity  = transform.up * h * rotationSpeed ;//刚体的旋转速度
    }
}

设置两个玩家,Ctrl+D复制两个Vertical与两个Horizontal进行修改设置
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

5.相机跟随两个物体

/*脚本挂在相机上*/
using UnityEngine;
public class FollowTarget : MonoBehaviour
{
    public Transform player1;//直接声明Transform组件,拖拽游戏物体赋值,Unity自动查询游戏物体上的Transform组件
    public Transform player2;

    private Vector3 offset;
    void Start()
    {
        offset = transform.position - (player1.position + player2.position) / 2;//获取相机最初的偏移量
    }
    void Update()
    {
    	if (player1 == null || player2 == null) return;//一方坦克摧毁则停止跟随
        float size= Vector3.Distance(player1.position, player2.position) * 0.73f;//测算a,b之间的距离
        transform.position=offset  + (player1.position + player2.position) / 2;//跟新相机位置
        if (size >= 10)
        {
            GetComponent<Camera>().orthographicSize = Vector3.Distance(player1.position, player2.position) * 0.73f;
        } 
    }
}

Vector3.Distance(player1.position, player2.position) * 0.73f;——返回a,b之间的距离
GetComponent().orthographicSize——正交相机Size值

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值