PacMan 01-- player moves

Copyright notice:

  • This article Original starting in the following website:
  1. Blog Park "excellent dream maker culture" in space: https: //www.cnblogs.com/raymondking123
  2. Excellent dream maker culture official blog: https: //91make.top
  3. Excellent game dream maker culture lecture: https: //91make.ke.qq.com
  4. "Excellent dream maker culture" of micro-channel public number: umaketop
  • You are free to reprint, but must include the full copyright notice

player walking

Walking principle:

1. Each time a mobile unit, determines whether there is an obstacle, the obstacle can not pass through
2. Trigger a method of generating a key is pressed when the ray, if the wall is found, it returns to true
3. Only four mobile direction
4. the movement distance per beans must be equally spaced and

code show as below

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;//返回射线信息
    }
}

state machine

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)//状态更新
    {

    }
}

Monster state transitions (only departure and patrol)

```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 // one state, the state of the starting
{
Private List path;
Private int index; // current number of path points to go
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.speed Time.fixedDeltaTime);
c.GetComponent () .MovePosition (A);
IF ((the Vector2) c.transform.position == A)
{
index ++;
. IF (index> = path.Count) // real number COUNT attribute determining element
{
c.machine.Init (c, new XunluoPoint ()) ; // finish waypoint after start patrol state
Debug.Log ( "complete starting waypoint");
} the else
{
the Vector2 path B = [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++)
{

                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();//每帧调用
 }

}

Guess you like

Origin www.cnblogs.com/raymondking123/p/11582427.html