下面是部分代码:
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;
}
}