版权申明:

  • 本文原创首发于以下网站:
  1. 博客园『优梦创客』的空间:https://www.cnblogs.com/raymondking123
  2. 优梦创客的官方博客:https://91make.top
  3. 优梦创客的游戏讲堂:https://91make.ke.qq.com
  4. 『优梦创客』的微信公众号:umaketop
  • 您可以自由转载,但必须加入完整的版权声明

player的行走

行走原理:

1.每次移动一个单位,判断是否有障碍物,障碍物不可以穿越

2.在按下按键时候触发一个方法,该方法生成一个射线,如果发现墙壁,则就返回true

3.只有四个移动方向

4.每次移动距离都要与豆子间隔相等

代码如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class PlayerController : MonoBehaviour {
float Speed=5f;//移动速度
Vector2 playerto;//移动方向
void Start()
{
playerto = gameObject.transform.position;//初始位置
}
void FixedUpdate()
{
//移动方法
Vector2 a = Vector2.MoveTowards(this.gameObject.transform.position, playerto, Speed * Time.fixedDeltaTime);//移动方法
this.gameObject.GetComponent<Rigidbody2D>().MovePosition(a);//利用rigidoby2D移动
if ((Vector2)gameObject.transform.position == playerto)
{
Vector2 s=Vector2.zero;
if (Input.GetKey(KeyCode.A)&&!CanGo(Vector2.left))
{
s = Vector2.left;
}
else if (Input.GetKey(KeyCode.S)&&!CanGo(Vector2.down))
{
s = Vector2.down;
}
else if (Input.GetKey(KeyCode.D)&&!CanGo(Vector2.right))
{
s = Vector2.right;
}
else if (Input.GetKey(KeyCode.W)&&!CanGo(Vector2.up))
{
s = Vector2.up;
}
playerto += s;//改变移动坐标
gameObject.GetComponent<Animator>().SetFloat("DirX",s.x);//播放相应的动画
gameObject.GetComponent<Animator>().SetFloat("DirY",s.y);
}
}
void Update()
{
}
bool CanGo(Vector2 ss)//检测方法
{
//Debug.Log("检测到障碍物");
RaycastHit2D raycastHit2=Physics2D.Linecast(this.transform.position,(Vector2)this.transform.position+ss,1<<LayerMask.NameToLayer("map"));
if (raycastHit2==true)
{
Debug.Log("检测到障碍物");
}
return raycastHit2;//返回射线信息
}
}

状态机

FSM

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class StaticMachine<T> : MonoBehaviour {
// 状态机控制器;
public SureStatic<T> SureStatic = null;//当前的状态;
public T owner;//状态机拥有者;
public void Init(T owner,SureStatic<T> initalState)
{
this.owner = owner;
SureStatic = initalState;
ChangeState(SureStatic);//状态机变化方法
}
public void ChangeState(SureStatic<T> NewState)
{
if (NewState!=null)
{
SureStatic.Exit(owner);
}
SureStatic = NewState;
SureStatic.Enter(owner);
}
public void Update()
{
SureStatic.Update(owner);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class SureStatic<T> : MonoBehaviour
{
//被用于继承
public virtual void Exit(T a)//状态退出
{ }
public virtual void Enter(T b)//状态进入
{ }
public virtual void Update(T c)//状态更新
{ }
}

怪物的状态转换(只有出发和巡逻)

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class Moster : MonoBehaviour {
StaticMachine<Moster> machine = new StaticMachine<Moster>();
public List<Vector2> LiveHomePath;
public float speed = 4f;
class WayPointSetect:SureStatic<Moster>//一种状态,出发状态
{
private List<Vector2> path;
private int index;//当前走的路径点个数
public WayPointSetect(List<Vector2> path)
{
this.path = path;
this.index = 0;
}
public override void Update(Moster c)
{
Vector2 a = Vector2.MoveTowards(c.transform.position,path[index],c.speed*Time.fixedDeltaTime);
c.GetComponent<Rigidbody2D>().MovePosition(a);
if ((Vector2)c.transform.position==a)
{
index++;
if (index>=path.Count)//.count属性判断元素真实个数
{
c.machine.Init(c,new XunluoPoint());//走完路点后,开始巡逻状态
Debug.Log("出发路点完成");
}else
{
Vector2 b = path[index] - path[index - 1];
c.GetComponent<Animator>().SetFloat("MirX",b.x);
c.GetComponent<Animator>().SetFloat("MirY",b.y);
}
}
}
}
class XunluoPoint:SureStatic<Moster>//巡逻状态
{
private Vector2 dir;//目标点
private Vector2 Edir;//当前方向向量
private Vector2[] EdirTo = new Vector2[] { Vector2.left, Vector2.up,Vector2.right,Vector2.down };
public override void Enter(Moster b)
{
dir = b.transform.position;
Edir = b.transform.position;
}
public override void Update(Moster c)
{
//Edir = c.transform.position;
Vector2 b = Vector2.MoveTowards(c.transform.position,dir,c.speed*Time.fixedDeltaTime);
c.gameObject.GetComponent<Rigidbody2D>().MovePosition(b);
if ((Vector2)c.transform.position==dir)
{
List<Vector2> Averation = new List<Vector2>();
for (int i=0;i<EdirTo.Length;i++)
{ if (EdirTo[i]==-Edir)
{
Debug.Log("相反,跳出路径;");
continue;
}
if (c.CanGo(EdirTo[i]) == false)
{
Averation.Add(EdirTo[i]);
} }
int a = Random.Range(0,Averation.Count);
Edir = Averation[a];
dir += Averation[a];
//Vector2 s = dir - Edir;
c.GetComponent<Animator>().SetFloat("MirX", Edir.x);
c.GetComponent<Animator>().SetFloat("MirY", Edir.y);
}
}
}
private bool CanGo(Vector2 dir)
{
RaycastHit2D a = Physics2D.Linecast(this.transform.position,(Vector2)this.transform.position+dir,1<<LayerMask.NameToLayer("map"));
return a;
}
void Start ()
{
machine.Init(this, new WayPointSetect(LiveHomePath));//初始化
}
void FixedUpdate()
{
machine.Update();//每帧调用
}
}

PacMan 01——玩家移动的更多相关文章

  1. PacMan 01——地图的搭建

    版权申明: 本文原创首发于以下网站: 博客园『优梦创客』的空间:https://www.cnblogs.com/raymondking123 优梦创客的官方博客:https://91make.top ...

  2. 【强化学习】python 实现 q-learning 例五(GUI)

    本文作者:hhh5460 本文地址:https://www.cnblogs.com/hhh5460/p/10143579.html 感谢pengdali,本文的 class Maze 参考了他的博客, ...

  3. Linux 桌面玩家指南:01. 玩转 Linux 系统的方法论

    特别说明:要在我的随笔后写评论的小伙伴们请注意了,我的博客开启了 MathJax 数学公式支持,MathJax 使用$标记数学公式的开始和结束.如果某条评论中出现了两个$,MathJax 会将两个$之 ...

  4. PacMan 03——追踪玩家

    版权申明: 本文原创首发于以下网站: 博客园『优梦创客』的空间:https://www.cnblogs.com/raymondking123 优梦创客的官方博客:https://91make.top ...

  5. Unity中制作游戏的快照游戏支持玩家拍快照

    Unity中制作游戏的快照游戏支持玩家拍快照 有些游戏支持玩家“拍快照”,也就是将游戏的精彩瞬间以图片的形式记录下来的功能.这个功能比较有趣,而且以后的用途也会很广,为此本节打算介绍:截取矩形区域内游 ...

  6. Pacman主题下给Hexo增加简历类型

    原文 http://blog.zanlabs.com/2015/01/02/add-resume-type-to-hexo-under-pacman-theme/ 背景 虽然暂时不找工作,但是想着简历 ...

  7. nyoj 203 三国志(最短路加01背包)

    三国志 时间限制:3000 ms  |  内存限制:65535 KB 难度:5   描述 <三国志>是一款很经典的经营策略类游戏.我们的小白同学是这款游戏的忠实玩家.现在他把游戏简化一下, ...

  8. 《Genesis-3D开源游戏引擎完整实例教程-跑酷游戏篇01:道路的自动生成》

    1.道路的自动生成 道路自动生成概述: 3D跑酷游戏的核心就是跑,在跑这一过程中增加趣味性使得游戏具有更多的可玩性.道路的自动生成和自由拼接,为游戏增设了更多的不可预见性.这种不可预见性使得玩家在游戏 ...

  9. Unity多玩家网络游戏开发教程1章Unity带有网络功能

    Unity网络多玩家游戏开发教程第1章Unity自带网络功能 Unity拥有大量的第三方插件.专门提供了对网络功能的支持. 可是.大部分开发人员第一次接触到的还是Unity自带的网络功能.也就是大家常 ...

随机推荐

  1. 从零开始搭建前后端分离的NetCore(EF Core CodeFirst+Au)+Vue的项目框架之二autofac解耦

    在 上一篇 中将项目的基本骨架搭起来能正常跑通,这一篇将讲到,如何通过autofac将DbContext和model进行解耦,只用添加model,而不用在DbContext中添加DbSet. 在这里就 ...

  2. mysql函数拼接查询concat函数

    //查询表managefee_managefee的年year 和 month ,用concat函数拼成year-month.例如将2017和1 拼成2017-01.. select CONCAT(a. ...

  3. 更新!ArcMap和ArcGIS Pro加载百度影像地图

    上一篇文章写了ArcMap和ArcGIS Pro中加载百度地图 的方法 一次没有把百度影像加载的功能开发出来,趁这几天有空整理了下 加载方法按照上次那篇文章操作. 百度影像wmts加载地址:http: ...

  4. parseInt和Number的应用区别

    parseInt() 和 Number()的应用区别 这两个函数最多的应用就是把一个字符串转换成数据类型. 1.parseInt() parseInt()函数将给定的字符串以指定的基数解析为整数 语法 ...

  5. Java入门指南-02 变量

    一.回顾上一篇讲到了常用的DOS命令.如何创建第一个程序.打印.注释与空白. 那么我们已经学会了使用System.out.println() 可以来输出一个值.进一步的,我们可以用它来显示简单的算术运 ...

  6. 第10章 文档对象模型DOM 10.2 Document类型

    Document 类型 JavaScript 通过 Document 类型表示文档.在浏览器中, document 对象是 HTMLDocument (继承自 Document 类型)的一个实例,表示 ...

  7. 牛客第七场 Minimum Cost Perfect Matching 规律

    题意:1-n-1个数和1-n-1个数两两匹配,每次匹配将两个数的值进行与运算,要求每次匹配与运算的和相加最小,问满足匹配的配对方式 分析:打表前10个数与运算最小的匹配,我们发现,从n-1开始按位取反 ...

  8. poj 1984 Navigation Nightmare(带权并查集+小小的技巧)

    题目链接:http://poj.org/problem?id=1984 题意:题目是说给你n个线,并告知其方向,然后对于后面有一些询问,每个询问有一个时间点,要求你输出在该时间点a,b的笛卡尔距离,如 ...

  9. 一个简单的hibernate自动创建表

    导入关键jar 举炉石传说卡片说明,Card.java   Card.hbm.xml Card.java Card.hbm.xml <?xml version="1.0"?& ...

  10. 微信小程序 自定义组件 多列选择器 对象数组 ObjectArray 自关联 三级联动

    使用方法 在 Page.json 注册组件 { "usingComponents": { "address-picker": "/component/ ...