【学习日志】2022.10.21 Unity添加AI小人、踩踏攻击

添加AI小人(蓝色的)

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

public class AIController : MonoBehaviour
{
    Character character;
    
    public float changeActTime = 2; //改变行动的时间间隔
    public float jumpTime = 0.5f; //每隔0.5秒尝试跳跃
    public float jumpChance = 30; //每次有30%概率跳跃

    float inputX;         //模拟横向输入 -1~1
    bool jump = false;

    void Start()
    {
        character = GetComponent<Character>();
        StartCoroutine(ChangeAct());
        StartCoroutine(AIJump());
    }

    void Update()
    {
        character.Move(inputX, jump);
        jump = false;
    }

    IEnumerator ChangeAct()
    {
        while (true)
        {
            //改变inputX
            inputX = Random.Range(-1f, 1f);

            yield return new WaitForSeconds(changeActTime);
        }
    }
    IEnumerator AIJump()
    {
        while (true)
        {
            int r = Random.Range(0, 100);
            if(r < jumpChance)
            {
                jump = true;
            }
            yield return new WaitForSeconds(jumpTime);
        }
    }
}

踩踏攻击

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

public class Character : MonoBehaviour
{
    CharacterController cc;
    Animator animator;
    Vector3 pendingVelocity;    // 这一帧移动的向量

    public float speed = 6;
    public float jumpSpeed = 5;
    bool isGround = false;
    public GameObject deathParticle;   //死亡粒子特效

    void Start()
    {
        cc = GetComponent<CharacterController>();
        animator = GetComponent<Animator>();
    }

    public void Move(float h,bool jump)
    {
        pendingVelocity.x = h * speed;

        if (Mathf.Abs(h) > 0.1f)
        {
            //考虑旋转
            Quaternion right = Quaternion.LookRotation(Vector3.forward);
            Quaternion left = Quaternion.LookRotation(Vector3.back);

            if (h > 0)
            {
                //往右转
                transform.rotation = Quaternion.Slerp(transform.rotation, right, 0.05f);
            }
            else
            {
                //往左转
                transform.rotation = Quaternion.Slerp(transform.rotation, left, 0.05f);
            }
        }
        //处理跳跃逻辑
        if (jump && isGround)
        {
            //正常跳跃
            pendingVelocity.y = jumpSpeed;
            isGround = false;
        }
        //考虑下落
        if (!isGround)
        {
            pendingVelocity.y += Physics.gravity.y * 2 * Time.deltaTime;
        }
        else
        {
            pendingVelocity.y = 0;
        }

        cc.Move(pendingVelocity * Time.deltaTime);

        //更新动画
        UpdateAnim();

        //踩踏攻击检测
        AttackCheck();
    }

    private void FixedUpdate()
    {
        isGround = false;
        Ray ray = new Ray(transform.position + new Vector3(0,0.2f,0),Vector3.down);

        Vector3 start = transform.position + new Vector3(0, 0.2f, 0);
        //Debug.DrawLine(start,start + new Vector3(0,-0.25f,0),Color.blue);

        RaycastHit hit;
        if(Physics.Raycast(ray,out hit, 0.25f, LayerMask.GetMask("Default")))
        {
            isGround = true;
        }
        Physics.Raycast(ray, out hit, 0.3f, LayerMask.GetMask("Default","Ground"));
    }

    void AttackCheck()
    {
        Ray ray = new Ray(transform.position + new Vector3(0, 0.2f, 0), Vector3.down);

        RaycastHit hit;
        if (Physics.Raycast(ray, out hit, 0.1f, LayerMask.GetMask("Cha")))
        {
            //被射线打中的物体,是hit.transform
            Character cha = hit.transform.GetComponent<Character>();
            cha.TakeDamage();
            //反弹
            Bounce();
        }
    }

    GameObject temp;
    public void TakeDamage()
    {
        Debug.Log(gameObject.name + "TakeDamage");
        Destroy(gameObject);
        GameObject go = Instantiate(deathParticle, transform.position, Quaternion.identity);
        Destroy(go, 2); //2s后删除粒子
    }

    void Bounce()
    {
        pendingVelocity.y = jumpSpeed/2;
    }

    void UpdateAnim()
    {
        animator.SetFloat("Forward", cc.velocity.magnitude / speed);
        animator.SetBool("IsGround", isGround);
    }
    private void OnCollisionEnter(Collision collision)
    {
        
    }
    private void OnTriggerEnter(Collider other)
    {
        
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AIController : MonoBehaviour
{
    Character character;
    
    public float changeActTime = 2; //改变行动的时间间隔
    public float jumpTime = 0.5f; //每隔0.5秒尝试跳跃
    public float jumpChance = 30; //每次有30%概率跳跃

    float inputX;         //模拟横向输入 -1~1
    bool jump = false;

    void Start()
    {
        character = GetComponent<Character>();
        StartCoroutine(ChangeAct());
        StartCoroutine(AIJump());
    }

    void Update()
    {
        character.Move(inputX, jump);
        jump = false;
    }

    IEnumerator ChangeAct()
    {
        while (true)
        {
            //改变inputX
            inputX = Random.Range(-1f, 1f);
            yield return new WaitForSeconds(changeActTime);
        }
    }
    IEnumerator AIJump()
    {
        while (true)
        {
            int r = Random.Range(0, 100);
            if(r < jumpChance)
            {
                jump = true;
            }
            yield return new WaitForSeconds(jumpTime);
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    Character character;
    void Start()
    {
        character = GetComponent<Character>();
    }

    // Update is called once per frame
    void Update()
    {
        float h = Input.GetAxis("Horizontal");
        bool jump = Input.GetButtonDown("Jump");

        character.Move(h,jump);
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值