菜鸟的Unity自学日志8 添加第一个敌人—石像鬼

设置视线石像鬼的预设

回顾第二天的基本操作
在这里插入图片描述
在这里插入图片描述
新建一个Animator controller命名如下
在这里插入图片描述
在这里插入图片描述
Animator组件中Controller属性设置为Gargoyle
在这里插入图片描述

添加碰撞器

打开Gargoyle的prefab
在这里插入图片描述
设置如下
在这里插入图片描述

添加一个触发器来模拟石像鬼的视线

在Gargoyle对象中添加一个空对象命名为
在这里插入图片描述
设置其位置为
在这里插入图片描述
为其添加一个胶囊碰撞体勾选Is Trigge,如下设置
在这里插入图片描述
效果如图
在这里插入图片描述

自定义一个脚本用于应对John被发现后的触发事件

思路:John进入触发区域,显示失败UI,刷新游戏;在UI的控制脚本GameEnding中创建一个公共方法,触发器脚本中调用该方法以通知GameEnding控制脚本显示失败ui刷新游戏。
创建Observer拖入,PointOfView对象
在这里插入图片描述

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

public class Observer : MonoBehaviour
{
    public Transform player;//关联游戏对象John
    public GameEnding gameEnding;//用于更改GameEnding类中的私有成员变量
    bool m_IsPlayerInRange = false;//记录John是否进入触发区
    
    void OnTriggerEnter(Collider other)//进入触发区
    {
        if(other.transform == player)//进入对象是否是John
        {
            m_IsPlayerInRange = true;
        }
    }
    void OnTriggerExit(Collider other)//出触发区
    {
        if(other.transform == player)
        {
            m_IsPlayerInRange = false;
        }
    }
    void Update()
    {
        if(m_IsPlayerInRange)//进入了触发区
        {
            Vector3 direction = player.position - transform.position + Vector3.up;//众所周知向量的值等于坐标A减去坐标B,其中Vector3.up相当于(0,1,0)
            Ray ray = new Ray(transform.position, direction);//用Ray方法实例化一个名为ray的Ray类对象,第一个参数
            RaycastHit raycastHit;//
            if(Physics.Raycast(ray, out raycastHit))//如果该光线打到某个对象返回true否则返回false,out参数的值可以通过其他方式更改或设置,而RaycastHit类型的参数就是一个out参数
            {
                if(raycastHit.collider.transform == player)//检测被光线击中的对象是否是John
                {
                    /*这里将要更变之前写的GameEnding脚本,为它添加重启游戏功能*/
                    gameEnding.CaughPlayer();//调用gameEnding实例对象的公共方法CaughPlayer()将GameEnding1类中的bool类型成员m_IsPlayerCaught置为true
                }
            }
        }
    }
}

通过在Unity中的接口设置public成员变量。

修改FaderCanvas和GameEnding

在FaderCanvas中右键点击Background复制并在此根下粘贴,将两个分别重命名为ExitImageBackground和CaughtImageBackground,对应退出和被发现的情况。
在这里插入图片描述
进入CaughtImageBackground对象,更改ExitImage为CaughtImage
在这里插入图片描述

编辑

在这里插入图片描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;//SceneManager.LoadScene();所在的命名空间

public class GameEnding : MonoBehaviour
{
    public float fadeDuration = 1f;//渐变发生持续时间
    public GameObject player;//用于添加John对象
    bool m_IsPlayerAtExit;//是否逃脱游戏
    bool m_IsPlayerCaught;//是否被抓住
    float m_Timer;//记录持续时间
    public CanvasGroup exitBackroundImageCanvasGroup;//用于关联CanvasGroup组件
    public CanvasGroup caughtBackroundImageCanvasGroup;//用于关联CanvasGroup组件
    public float displayImageDuration = 1f;
    void OnTriggerEnter(Collider other)//触发器
    {
         if(other.gameObject == player)//如果触发者是John
        {
            m_IsPlayerAtExit = true;//游戏结束
        }
    }
    void Update()//帧更新
    {
        if(m_IsPlayerAtExit)//如果触发了逃脱条件
        {
            EndLevel(exitBackroundImageCanvasGroup, false);//逐渐显示逃脱成功结束图片,并退出游戏
        }
        else if(m_IsPlayerCaught)//如果被抓住
        {
            EndLevel(caughtBackroundImageCanvasGroup, true);//组件显示逃脱失败图片,并且重新开始游戏
        }
    }
    void EndLevel (CanvasGroup imageCanvasGroup, bool doRestart)//根据第一个参数处理结束画面
    {
        m_Timer += Time.deltaTime;
        if(doRestart)//是否重新开始游戏
        {
            caughtBackroundImageCanvasGroup.alpha = m_Timer / fadeDuration;//在一秒类组件显示失败图片
            if (m_Timer > displayImageDuration + fadeDuration)//持续2秒
            {
                SceneManager.LoadScene(0);//重新加载你的第一个场景
            }
        }
        else
        {
            exitBackroundImageCanvasGroup.alpha = m_Timer / fadeDuration;//在一秒内逐渐显示成功图片
            if(m_Timer > displayImageDuration + fadeDuration)//持续2秒
            {
                Application.Quit();//退出
            }
        }
    }
    public void CaughPlayer ()
    {
        m_IsPlayerCaught = true;
    }
}

修改后GameEnding中的脚本中作如下设置,分别关联以上两个对象。
在这里插入图片描述
保存完成

个人练习:让石像鬼来回扫视

思路:添加刚体组件,确定来回扫视范围,角速度,最大旋转角速度,停顿时间,根据以上信息获得旋转量旋转。

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

public class GargoyleAct : MonoBehaviour
{
    // Start is called before the first frame update
    public int initDirection = -1;//1 或 -1
    public float startAngle = 180f;//开始位置
    public float turnSpeed = 20f;//允许的最大角速度20度每帧
    public float maxDeltaAngle = 5f;//最大转速5度每帧
    public float waitTime = 2f;//等待时间2秒
    public float changePoint = 30f;//开始加速或减速时的偏移量
    private float acceleration;//加速度由公式 maxDeltaAngle / (2 * changePoint / maxDeltaAngle)得到
    private float angle;
    public float m_Timer;//计时器
    public float angleTemp;//保存瞬时角速度
    private float endAngle;//结束的角度,在该脚本中等于两倍的startAngle
    private Quaternion m_Rotation;
    private Rigidbody m_Rigidbody;
    private Vector3 m_Direction;
    private struct State//保存角速度状态
    {
        public bool isSpeedDown;
        public bool isSpeedUp;
        public bool isWaiting;
    };
    State state;
    void Start()
    {
        m_Rigidbody = GetComponent<Rigidbody>();
        initData(); 
        
    }
    // Update is called once per frame
    void Update()
    {
        StateCheck();//更新状态
        GetAngleTemp();//获得瞬时角速度
        UpDateAngle(angleTemp);//更新朝向角度
        GetRotation();//获得旋转量
    }
    private void OnAnimatorMove()
    {
        m_Rigidbody.MoveRotation (m_Rotation);//旋转
    }
    private void StateCheck()
    {
        state.isWaiting = Mathf.Approximately(angle % startAngle, 0);//时候处于等待状态
        state.isSpeedUp = angle <= startAngle + changePoint;//是否处于加速状态
        state.isSpeedDown = angle >= endAngle - changePoint;//是否处于减速状态
        //ps:速度为负时加速即这里的减速
    }
    private void GetAngleTemp()
    {
        if (state.isWaiting)
        {
            state.isWaiting = WaitMinute(waitTime);
        }
        if (!state.isWaiting)
        {
            if (state.isSpeedUp)
            {
                raiseDeltaAngle(acceleration);
            }
            else if (state.isSpeedDown)
            {
                reduceDeltaAngle(acceleration);
            }
        }
    }
    private void GetRotation()
    {
        float sin = Mathf.Sin(Mathf.PI * angle / 180f);
        float cos = Mathf.Cos(Mathf.PI * angle / 180f);
        m_Direction.Set(initDirection * sin, 0f, initDirection * cos);//获得当前朝向
        m_Direction = Vector3.RotateTowards(transform.forward, m_Direction, turnSpeed * Time.deltaTime, 0f);//获得目标朝向
        m_Rotation = Quaternion.LookRotation(m_Direction);//获得两个向量间的旋转量
    }
    private void initData()
    {
        m_Rotation = Quaternion.identity;
        m_Direction.Set(1f, 0f, 1f);
        angle = startAngle;
        angleTemp = 0f;
        m_Timer = 0f;
        acceleration = maxDeltaAngle / (2 * changePoint / maxDeltaAngle); //由加速度公式算出角加速度
        endAngle = 2 * startAngle;
    }
    private void UpDateAngle(float angleTemp)
    {
        angle += angleTemp;
    }
    private bool WaitMinute(float waitTime)//等待中时返回true,等待结束返回false
    {
        m_Timer += Time.deltaTime;
        if(m_Timer < waitTime)
        {
            angleTemp = 0f;
            return true;
        }
        else
        {
            m_Timer = 0f;
            return false;
        }
    }
    private void reduceDeltaAngle(float acceleration)//减速
    {
        angleTemp -= acceleration;
    }
    private void raiseDeltaAngle(float acceleration)//加速
    {
        angleTemp += acceleration;
    }
}
//功能尚未完善,待修改

下一节

添加第二个敌人与AI设置

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值