unity2D让游戏物体在不规则圈内走走停停

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

public class AnimalSrc100 : MonoBehaviour
{
    private float speed = 4;
    public Vector3 dir;//this.gameobject的运动方向
    public int index;//记录随机点
    private float time;//定时器
    public bool isWalk;//状态判断
    private Animator Anim;

    void Start()
    {
        dir = new Vector3(Random.Range(-4, 4), Random.Range(-4, 4), 0);//给游戏物体一个初始方向,让它去撞击边界触发器
        Anim = GetComponent();
       // Anim.Play("run");
        time = 0;
        isWalk = true;
    }
    
    void Update()
    {
        time += Time.deltaTime;//定时
        if (time > 3)//3秒改变一次状态,让游戏物体可以停停走走,不然很僵硬
        {
            ChangeState();
        }
        //一种运动中状态
        //一种停止状态 //两秒变换一次状态
        if (isWalk)
        {
            //运动: anim.play("run")
            transform.localPosition += dir.normalized * speed * Time.deltaTime;
            Rotate();
          
        }
        else
        {
            //停下来吃东西的状态
            // Anim.Play("eat");
        }
       
    }
    void ChangeState()
    {
        int value = Random.Range(0, 2);
        if (value == 0) //停下来
        {
            isWalk = false;//停止
        }
        else //继续走
        {
            if (!isWalk)//如果本来是停下来的鸡,现在变为走动,那就转一下方向
            {
                dir = new Vector3(Random.Range(-4, 4), Random.Range(-4, 4), 0);
            }
            isWalk = true;//运动
        }
        time = 0;//定时器清零
    }

    private void Rotate()
    {
        if (dir.magnitude <= 0) return;

        float angle = Vector3.SignedAngle(transform.up, dir, Vector3.forward);
        if (Mathf.Abs(angle) <= 20.0f)
        {
            return;
        }
        transform.Rotate(new Vector3(0, 0, Mathf.Sign(angle) * 360.0f * Time.deltaTime));
    }

 

 

 

 

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

public class Detector100 : MonoBehaviour
{
    
    void Start()
    {
        
    }

   
    void Update()
    {
        
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        int temp = Random.Range(0, ChickenPenManage.instance.spawns.Count);//在第二步的随机点中随机选一个出来
        while (temp == collision.gameObject.GetComponent().index)//这个随机点和前一个随机点不能相同,如果相同的话,就会直接出边界了。
        {
            temp = Random.Range(0, ChickenPenManage.instance.spawns.Count);
        }
        collision.gameObject.GetComponent().dir = ChickenPenManage.instance.spawns[temp].position - collision.transform.position;//给游戏物体一个到随机点的方向,这个方向就是它接下来的运动方向。
        collision.gameObject.GetComponent().index = temp;//记录当前随机点
    }   

}

 

 

 

 

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

public class ChickenPenManage : MonoBehaviour
{
    public static ChickenPenManage instance = null;
    [SerializeField]
    public List spawns = new List();
    void Awake()
    {
        instance = this;
    }

    
    void Update()
    {
        
    }
}

 

 

 

你可能感兴趣的:(unity2D让游戏物体在不规则圈内走走停停)