技能释放及简单怪物AI

文章介绍了在Unity中制作技能动画的过程,包括确定图片中心以正确设置角色碰撞体。同时,讲解了如何为技能添加新的碰撞体并在录制过程中控制其大小。此外,文中还提供了一个简单的敌人AI脚本,敌人会追踪角色并受到技能伤害。当敌人血量归零时,敌人对象会被销毁。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 技能和攻击一样,都可以分为前摇,判定和后摇(这里为了方便俺就只用一个动画解决)

动画的录制

做好技能动画后,点击录制键

  • 在制作动画时要在精灵编辑器里去确定每个图片的中心,因为默认都是在图片的正中心,要将中心确定在角色的某一点上,这样角色本身的碰撞体才不会乱跑。

中间的小蓝点就是角色的中心,这里将中心放在角色胸口处

如这张,因为技能是由角色释放,所以中心还是在角色胸口处,而不是分身身上。(其他同理)

给技能添加一个新的碰撞体,在录制过程中可以控制碰撞体的开关以及大小

在录制过程中,只要给出打段时间前后的碰撞体变化,unity就会自动在每帧上调节碰撞体大小

  • 在这过程中不要去动角色的碰撞体和位置,仅可以调节新加的技能碰撞体


敌人简易AI

先为角色和敌人添加标签(tag),可以自行添加多余标签的

敌人代码:

using Cinemachine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AI : MonoBehaviour
{
    public static AI ai;//让其他脚本可访问

    public float speed;//向角色的移动速度
    private float maxHP = 2000;//开始血量
    private Transform target;//标签

    public float hp;//当前血量

    private void Awake()
    {
        ai = this;
    }

    // Start is called before the first frame update
    void Start()
    {
        hp = maxHP;
        target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();//通过标签来获取角色信息;

    }

    // Update is called once per frame
    void Update()
    {
        Follow();
    }

    public void Follow()
    {
        transform.position = Vector2.MoveTowards(transform.position, target.position,speed * Time.deltaTime);
        /*MoveTowds 三个参数,第一个是怪物的位置参数,第二个为角色位置参数,第三个为每次移动的距离*/

    }
}

MoveTowards的具体内容

  • 很简单的敌人AI,仅会追着角色跑,在上面图片中,为了方便我就没有将碰撞体调整好(QAQ)

角色代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class move : MonoBehaviour
{
    public float skill = 10;//技能伤害

    private Rigidbody2D rb;
    private Animator anim;
    public float speed = 1000;
    

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        Move();
    }

    public void Move()
    {
        float movex = Input.GetAxis("Horizontal");
        float x = Input.GetAxisRaw("Horizontal");

        if (Input.GetButtonDown("Jump"))
        {
            anim.Play("skill");
        }

        if (movex != 0)
        {
            rb.transform.position = new Vector2(transform.position.x + speed * movex *
            Time.deltaTime, transform.position.y);  //transform.position可以直接设置物体的位置
            anim.SetFloat("run", Mathf.Abs(x));
        }

        if (x != 0)
        {
            rb.transform.localScale = new Vector3(x, rb.transform.localScale.y, rb.transform.localScale.z);
        }

    }

    private void OnTriggerEnter2D(Collider2D other) 
    {
        if (other.tag == "AI")
        {
            AI.ai.hp -= skill;//当技能碰上敌人时,敌人hp减少
            if (AI.ai.hp == 0)
            {
                Destroy(AI.ai.gameObject);//当敌人血量为0时摧毁敌人
            }
        }
    }
}

OnTirggerEnter2D的具体用法,该方法要有方为is Trigger(可根据自己需求来选择)

  • 可以看到基本的战斗以及实现,那么仅需简单优化一下就可以了(更高深的内容我们项目的时候讲,才不是因为我不会QAQ~~~)

  • 基础篇完结,俺们项目见

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值