CameraFollow1.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow1 : MonoBehaviour
{
//跟随的游戏人物
private Transform Player;
//相机与人物之间的距离
Vector3 offset;
// Start is called before the first frame update
void Start()
{
Player = GameObject.Find("JohnLemon").transform;
offset = transform.position - Player.position;
}
// Update is called once per frame
void Update()
{
transform.position = offset + Player.position;
}
}
PlayMovement.cs
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class PlayMovement : MonoBehaviour
{
//定义一个旋转的速度
public float turnspeed = 20f;
//定义游戏任务上的组件
Animator m_Animator;
Rigidbody m_Rigidbody;
//游戏人物移动的方向
Vector3 m_Movement;
//游戏人物旋转的角度
Quaternion m_Quaternion = Quaternion.identity;
// Start is called before the first frame update
void Start()
{
//获取游戏人物上的刚体,动画控制机组件
m_Animator = GetComponent<Animator>();
m_Rigidbody = GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
//获取水平,竖直(z轴方向)是否有键值输入
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
//设置游戏任务移动的方向
m_Movement.Set(horizontal,0f,vertical);
m_Movement.Normalize();
//定义游戏人物是否移动的bool值
//bool hasHorizontalInput=!Mathf.Approximately(horizontal,0f);
bool hasHorizontalInput = !Mathf.Approximately(horizontal,0f);
bool hasVerticalInput = !Mathf.Approximately(vertical,0f);
//控制人物动画的转换
bool iswalking = hasHorizontalInput || hasVerticalInput;
m_Animator.SetBool("IsWalking", iswalking);
//旋转的过渡,通过三元数转四元数的方式获取游戏人物当前应有的角度
Vector3 desirForward = Vector3.RotateTowards(transform.forward, m_Movement, turnspeed * Time.deltaTime,0f);
m_Quaternion = Quaternion.LookRotation(desirForward);
}
//游戏人物的旋转和移动
private void OnAnimatorMove()
{
m_Rigidbody.MovePosition(m_Rigidbody.position + m_Movement * m_Animator.deltaPosition.magnitude);
m_Rigidbody.MoveRotation(m_Quaternion);
}
}
GameEnding.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine. SceneManagement;
public class GameEnding : MonoBehaviour
{
//游戏胜利时,图片淡入淡出的时间
public float fadeDuration = 1f;
//游戏胜利时图片显示的时间
public float displayDuration=1f;
//游戏人物
public GameObject Player;
//游戏胜利时的画布背景
public CanvasGroup ExitBK;
//游戏失败时的画布背景
public CanvasGroup FailBK;
//游戏胜利时的bool值
bool IsExit=false;
//游戏失败时的bool值
bool IsFail=false;
//定义计时器,用于图片的渐变和完全显示
public float timer=0f;
//音频组件
public AudioSource winaudio;
public AudioSource failaudio;
//bool值控制音效只播放一次
bool IsPlay=false;
private void OnTriggerEnter(Collider other)
{
if (other.gameObject == Player)
{
//检测到玩家
//游戏胜利
IsExit = true;
}
}
//游戏失败时的控制函数
public void Caught()
{
IsFail = true;
}
// Update is called once per frame
void Update()
{
//游戏胜利
if (IsExit)
{
EndLevel(ExitBK, false,winaudio);
}
//游戏失败
else if (IsFail)
{
EndLevel(FailBK, true,failaudio);
}
}
//游戏胜利或失败时的方法
void EndLevel(CanvasGroup igCanvasGroup,bool doRestart,AudioSource playaudio)
{
//游戏胜利或失败的音效播放
if (!IsPlay)
{
playaudio.Play();
IsPlay=true;
}
//玩家碰到触发器时,计时器开始计时
timer+=Time.deltaTime;
//控制CanvasGroup的不透明度
igCanvasGroup.alpha = timer/fadeDuration;
//游戏胜利/失败图片渐变了1秒,显示了1秒,游戏结束
if(timer>fadeDuration+displayDuration)
{
//游戏失败,重启游戏
if (doRestart)
{
SceneManager.LoadScene(0);
}
//游戏胜利,退出游戏
else if (!doRestart)
{
Application.Quit();
}
}
}
}
Observer.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Observer : MonoBehaviour
{
//游戏角色
public Transform Player;
//GameEnding
public GameEnding gameEnding;
//游戏玩家是否进入到扫描棒的视线范围
bool IsInRange=false;
private void OnTriggerEnter(Collider other)
{
//扫描棒扫描到玩家了
if (other.gameObject == Player.gameObject)
{
IsInRange = true;
}
}
private void OnTriggerExit(Collider other)
{
//扫描棒扫描到玩家离开了
if(other.gameObject == Player.gameObject)
{
IsInRange=false;
}
}
// Update is called once per frame
void Update()
{
if (IsInRange == true)
{
//射线检测
Vector3 dir=Player.position-transform.position+Vector3.up;
Ray ray = new Ray(transform.position,dir);
RaycastHit raycastHit;
if(Physics.Raycast(ray,out raycastHit))
{
if (raycastHit.collider.transform == Player)
{
//游戏失败,玩家被抓住,调用gameEnding的Caught方法
gameEnding.Caught();
}
}
}
}
}
WayPointsPatrol.cs文章来源:https://www.toymoban.com/news/detail-518215.html
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class WayPointsPatrol : MonoBehaviour
{
//导航组件
private NavMeshAgent navMeshAgent;
//导航点的数组
public Transform[] waypoints;
//当前巡逻的目标点
int m_currentpointIndex;
// Start is called before the first frame update
void Start()
{
//获取NavMeshAgent组件
navMeshAgent = GetComponent<NavMeshAgent>();
//从起点到达第一个巡逻点
navMeshAgent.SetDestination(waypoints[0].position);
}
// Update is called once per frame
void Update()
{
//到达目标点,前往下一个目标点
if (navMeshAgent.remainingDistance<navMeshAgent.stoppingDistance)
{
m_currentpointIndex=(m_currentpointIndex + 1) % waypoints.Length;
navMeshAgent.SetDestination(waypoints[m_currentpointIndex].position);
}
}
}
PS:根据b站教程完成 文章来源地址https://www.toymoban.com/news/detail-518215.html
到了这里,关于Unity入门案例(约翰的密室逃脱)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!