三十七.Boss基本设置

Boss基本设置

1.新建脚本

新建BossTankController.cs,添加组件到BossBattle节点上

添加参数

public Transform theBoss;   //boss
public Animator theAnim;    //动画主体

[Header("移动参数")]
public float moveSpeed;     //移动速度
public Transform leftPoint; //移动左边界点
public Transform rightPoint;//移动右边界点
private bool moveRight; //是否向右移动

[Header("射击参数")]
public GameObject bullet;   //子弹
public Transform firePoint; //射击点
public float timeBetweenShot;   //射击间隔时间
private float shotCounter;      //射击间隔计数器

[Header("伤害参数")]
public float hurtTime;      //伤害间隔时间
private float hurtCounter;  //伤害间隔计数器

在BossBattle节点下,新建两个空节点,分别命名为LeftPoint和RightPoint,并设置位置为左右两点,供坦克左右移动

在TheTank节点下,新建空节点,命名为FirePoint,为开火点,移动到炮口附近

请添加图片描述

在Unity中,绑定对应组件,并设置相应参数值

请添加图片描述

2.添加状态

在BossTankController.cs中,添加枚举状态,并添加状态参数

//状态枚举
public enum bossState
{
    shooting,
    hurt,
    moving
}
public bossState currentState;  //当前状态

添加受伤函数TakeHit()

public void TakeHit()
{
    //受伤状态
    currentState = bossState.hurt;
    hurtCounter = hurtTime;
}

在Update()中,设置状态,并添加受伤测试

void Update()
{
    switch (currentState)
    {
        case bossState.shooting:

            break;
        case bossState.hurt:

            if(hurtCounter > 0)
            {
                hurtCounter -= Time.deltaTime;
                if(hurtCounter <= 0)
                {
                    currentState = bossState.moving;
                }
            }
            break;
        case bossState.moving:

            break;
        default:

            break;
    }
#if UNITY_EDITOR    //在Unity Editor中才生效
    //测试
    if(Input.GetKeyDown(KeyCode.H))
    {
        TakeHit();
    }
#endif
}

3.Boss移动

在case bossState中,添加代码实现移动

case bossState.moving:
    if(moveRight)
    {
        theBoss.position += new Vector3(moveSpeed * Time.deltaTime, 0f, 0f);
        if(theBoss.position.x > rightPoint.position.x)
        {
            moveRight = false;

            currentState = bossState.shooting;
            shotCounter = timeBetweenShot;
        }
    }
    else
    {
        theBoss.position -= new Vector3(moveSpeed * Time.deltaTime, 0f, 0f);
        if (theBoss.position.x < leftPoint.position.x)
        {
            moveRight = true;

            currentState = bossState.shooting;
            shotCounter = timeBetweenShot;
        }
    }

    break;

设置Boss正向反向(上面判断中,两个设置moveRight之前添加)

//正向
//theBoss.localScale = new Vector3(1f,1f, 1f);
theBoss.localScale = Vector3.one;


//反向
theBoss.localScale = new Vector3(-1f, 1f, 1f);

添加动画触发

TakeHit()函数里,添加触发受伤动画

public void TakeHit()
{
    //受伤状态
    currentState = bossState.hurt;
    hurtCounter = hurtTime;

    //受伤动画
    theAnim.SetTrigger("Hit");
}

添加触发停止移动动画(bossState.moving中,改变状态为bossState.shooting之后<2个>)

//停止移动动画
theAnim.SetTrigger("StopMoving");

整理相同代码合并(moving中两个),新建为EndMovement()

private void EndMovement()
{
    currentState = bossState.shooting;
    shotCounter = timeBetweenShot;
    //停止移动动画
    theAnim.SetTrigger("StopMoving");
}

替换原代码为EndMovement()

case bossState.moving:
    if(moveRight)
    {
        theBoss.position += new Vector3(moveSpeed * Time.deltaTime, 0f, 0f);
        if(theBoss.position.x > rightPoint.position.x)
        {
            //正向
            //theBoss.localScale = new Vector3(1f,1f, 1f);
            theBoss.localScale = Vector3.one;

            moveRight = false;

            EndMovement();
        }
    }
    else
    {
        theBoss.position -= new Vector3(moveSpeed * Time.deltaTime, 0f, 0f);
        if (theBoss.position.x < leftPoint.position.x)
        {
            //反向
            theBoss.localScale = new Vector3(-1f, 1f, 1f);

            moveRight = true;

            EndMovement();
        }
    }

    break;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值