让机器人更加智能_AI中增加随机巡逻思考

下面是部分代码:
using UnityEngine;
using Photon.Pun.Demo.PunBasics;
namespace BehaviorDesigner.Runtime.Tasks.Movement
{
public class SeekRandomPosition : Action
{

    [Tooltip("The speed of the agent")]
    public SharedFloat speed;
    [Tooltip("The agent has arrived when the magnitude is less than this value")]
    public SharedFloat arriveDistance = 0.01f;
    [Tooltip("Should the agent be looking at the target position?")]
    public SharedBool lookAtTarget = true;
    [Tooltip("Max rotation delta if lookAtTarget is enabled")]
    public SharedFloat maxLookAtRotationDelta;
    [Tooltip("The GameObject that the agent is moving towards")]
    public SharedGameObject target;
    [Tooltip("If target is null then use the target position")]
    public SharedVector3 targetPosition;

    public override void OnStart()
    {
        base.OnStart();
        //new Vector3(UnityEngine.Random.insideUnitCircle.x * 10, 0.1f, UnityEngine.Random.insideUnitCircle.y * 10)
        //(15/0.49)* DuMat.GetFloat("_CircleRadius")
        //GameManager.Instance.duobj.GetComponent<Renderer>().material.GetFloat("_CircleRadius")
        targetPosition.Value = new Vector3(Random.insideUnitCircle.x*(15f/0.49f)*(GameManager.Instance.duobj.GetComponent<Renderer>().material.GetFloat("_CircleRadius")),this.transform.position.y, Random.insideUnitCircle.y * (15f / 0.49f) * (GameManager.Instance.duobj.GetComponent<Renderer>().material.GetFloat("_CircleRadius")));
    }

    public override TaskStatus OnUpdate()
    {

        if (GetComponent<AI_Player>().isacceleratespeed)//如果在加速
        {
            foreach (AnimationState state in GetComponent<Animation>())
            {
                state.speed = 2f;
            }
        }
        else
        {
            foreach (AnimationState state in GetComponent<Animation>())
            {
                state.speed = 1f;
            }
        }
        var position = Target();
        // Return a task status of success once we've reached the target
        if (Vector3.Magnitude(transform.position - position) < arriveDistance.Value)
        {
            GetComponent<Animation>().CrossFade("idle");
            GetComponent<AI_Player>().animationsname = "idle";
            return TaskStatus.Success;
        }
        // We haven't reached the target yet so keep moving towards it
        transform.position = Vector3.MoveTowards(transform.position, position, speed.Value * Time.deltaTime * GetComponent<AI_Player>().accelerateSpeed);
        GetComponent<Animation>().CrossFade("run");
        GetComponent<AI_Player>().animationsname = "run";
        if (lookAtTarget.Value && (position - transform.position).sqrMagnitude > 0.1f)
        {
            transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(position - transform.position), maxLookAtRotationDelta.Value);
        }
        return TaskStatus.Running;
    }

    // Return targetPosition if targetTransform is null
    private Vector3 Target()
    {
        if (target == null || target.Value == null)
        {
            return targetPosition.Value;
        }
        return target.Value.transform.position;
    }

    // Reset the public variables
    public override void OnReset()
    {
        arriveDistance = 0.01f;
        lookAtTarget = true;
    }
}

}

public class ThinkSometime : Action
{

 float thinkTime = 0f;


public override void OnStart()
{
    base.OnStart();
    thinkTime = Random.Range(0.5f, 3f);
}

// Seek the destination. Return success once the agent has reached the destination.
// Return running if the agent hasn't reached the destination yet
public override TaskStatus OnUpdate()
{
    if (this.GetComponent<AI_Player>().isacceleratespeed)//如果在加速
    {
        foreach (AnimationState state in this.GetComponent<Animation>())
        {
            state.speed = 2f;
        }
    }
    else
    {
        foreach (AnimationState state in this.GetComponent<Animation>())
        {
            state.speed = 1f;
        }
    }

    thinkTime -= Time.deltaTime;

    if (thinkTime<=0)
    {
        GetComponent<Animation>().CrossFade("idle");
        GetComponent<AI_Player>().animationsname = "idle";
        return TaskStatus.Success;
    }
    GetComponent<Animation>().CrossFade("idle");
    GetComponent<AI_Player>().animationsname = "idle";
    return TaskStatus.Running;
}

public override void OnReset()
{
    base.OnReset();
    thinkTime = 0f;
}

}
右边是新增加巡逻和思考行为

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值