经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » 游戏设计 » 查看文章
PacMan 01——玩家移动
来源:cnblogs  作者:优梦创客  时间:2019/9/25 10:23:58  对本文有异议

版权申明:

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

player的行走

行走原理:

1.每次移动一个单位,判断是否有障碍物,障碍物不可以穿越
2.在按下按键时候触发一个方法,该方法生成一个射线,如果发现墙壁,则就返回true
3.只有四个移动方向
4.每次移动距离都要与豆子间隔相等

代码如下

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class PlayerController : MonoBehaviour {
  5. float Speed=5f;//移动速度
  6. Vector2 playerto;//移动方向
  7. void Start()
  8. {
  9. playerto = gameObject.transform.position;//初始位置
  10. }
  11. void FixedUpdate()
  12. {
  13. //移动方法
  14. Vector2 a = Vector2.MoveTowards(this.gameObject.transform.position, playerto, Speed * Time.fixedDeltaTime);//移动方法
  15. this.gameObject.GetComponent<Rigidbody2D>().MovePosition(a);//利用rigidoby2D移动
  16. if ((Vector2)gameObject.transform.position == playerto)
  17. {
  18. Vector2 s=Vector2.zero;
  19. if (Input.GetKey(KeyCode.A)&&!CanGo(Vector2.left))
  20. {
  21. s = Vector2.left;
  22. }
  23. else if (Input.GetKey(KeyCode.S)&&!CanGo(Vector2.down))
  24. {
  25. s = Vector2.down;
  26. }
  27. else if (Input.GetKey(KeyCode.D)&&!CanGo(Vector2.right))
  28. {
  29. s = Vector2.right;
  30. }
  31. else if (Input.GetKey(KeyCode.W)&&!CanGo(Vector2.up))
  32. {
  33. s = Vector2.up;
  34. }
  35. playerto += s;//改变移动坐标
  36. gameObject.GetComponent<Animator>().SetFloat("DirX",s.x);//播放相应的动画
  37. gameObject.GetComponent<Animator>().SetFloat("DirY",s.y);
  38. }
  39. }
  40. void Update()
  41. {
  42. }
  43. bool CanGo(Vector2 ss)//检测方法
  44. {
  45. //Debug.Log("检测到障碍物");
  46. RaycastHit2D raycastHit2=Physics2D.Linecast(this.transform.position,(Vector2)this.transform.position+ss,1<<LayerMask.NameToLayer("map"));
  47. if (raycastHit2==true)
  48. {
  49. Debug.Log("检测到障碍物");
  50. }
  51. return raycastHit2;//返回射线信息
  52. }
  53. }

状态机

FSM

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class StaticMachine<T> : MonoBehaviour {
  5. // 状态机控制器;
  6. public SureStatic<T> SureStatic = null;//当前的状态;
  7. public T owner;//状态机拥有者;
  8. public void Init(T owner,SureStatic<T> initalState)
  9. {
  10. this.owner = owner;
  11. SureStatic = initalState;
  12. ChangeState(SureStatic);//状态机变化方法
  13. }
  14. public void ChangeState(SureStatic<T> NewState)
  15. {
  16. if (NewState!=null)
  17. {
  18. SureStatic.Exit(owner);
  19. }
  20. SureStatic = NewState;
  21. SureStatic.Enter(owner);
  22. }
  23. public void Update()
  24. {
  25. SureStatic.Update(owner);
  26. }
  27. }
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class SureStatic<T> : MonoBehaviour
  5. {
  6. //被用于继承
  7. public virtual void Exit(T a)//状态退出
  8. {
  9. }
  10. public virtual void Enter(T b)//状态进入
  11. {
  12. }
  13. public virtual void Update(T c)//状态更新
  14. {
  15. }
  16. }

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

```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Moster : MonoBehaviour {
StaticMachine machine = new StaticMachine();
public List LiveHomePath;
public float speed = 4f;
class WayPointSetect:SureStatic//一种状态,出发状态
{
private List path;
private int index;//当前走的路径点个数
public WayPointSetect(List path)
{
this.path = path;
this.index = 0;
}
public override void Update(Moster c)
{
Vector2 a = Vector2.MoveTowards(c.transform.position,path[index],c.speedTime.fixedDeltaTime);
c.GetComponent().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().SetFloat("MirX",b.x);
c.GetComponent().SetFloat("MirY",b.y);
}
}
}
}
class XunluoPoint:SureStatic//巡逻状态
{
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().MovePosition(b);
if ((Vector2)c.transform.position==dir)
{
List Averation = new List();
for (int i=0;i<EdirTo.Length;i++)
{

  1. if (EdirTo[i]==-Edir)
  2. {
  3. Debug.Log("相反,跳出路径;");
  4. continue;
  5. }
  6. if (c.CanGo(EdirTo[i]) == false)
  7. {
  8. Averation.Add(EdirTo[i]);
  9. }
  10. }
  11. int a = Random.Range(0,Averation.Count);
  12. Edir = Averation[a];
  13. dir += Averation[a];
  14. //Vector2 s = dir - Edir;
  15. c.GetComponent<Animator>().SetFloat("MirX", Edir.x);
  16. c.GetComponent<Animator>().SetFloat("MirY", Edir.y);
  17. }
  18. }
  19. }
  20. private bool CanGo(Vector2 dir)
  21. {
  22. RaycastHit2D a = Physics2D.Linecast(this.transform.position,(Vector2)this.transform.position+dir,1<<LayerMask.NameToLayer("map"));
  23. return a;
  24. }
  25. void Start ()
  26. {
  27. machine.Init(this, new WayPointSetect(LiveHomePath));//初始化
  28. }
  29. void FixedUpdate()
  30. {
  31. machine.Update();//每帧调用
  32. }

}

原文链接:http://www.cnblogs.com/raymondking123/p/11582427.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号