Синтаксис:
Используется csharp
public Transform[] movePoint;
NavMeshAgent myAgent;
Animator myAnim;
public bool circularRoute;
public int curWaypoint;
public float distance;
public Vector3 moveToPoint;
public float speed;
public float WalkSpeed;
public float rotationSpeed;
public float runspeed;
public bool fast;
void Start()
{
moveToPoint = transform.position;
myAnim = GetComponent<Animator>();
myAgent = GetComponent<NavMeshAgent>();
myAgent.updatePosition = false;
}
public void Patrol()
{
if (movePoint.Length >= 1)
{
if (movePoint.Length > curWaypoint)
{
if (Vector3.Distance(transform.position, movePoint[curWaypoint].position) < 0.2)
{
curWaypoint++;
}
else
{
MoveToPoint(movePoint[curWaypoint].position);
}
}
else if (movePoint.Length == curWaypoint)
{
if (circularRoute)
curWaypoint = 0;
else
{
if (Vector3.Distance(transform.position, movePoint[curWaypoint - 1].position) < 0.2f)
{
Stop();
}
else
{
MoveToPoint(movePoint[curWaypoint].position);
}
}
}
}
}
public void MoveToPoint(Vector3 position)
{
if (moveToPoint != position)
{
myAgent.SetDestination(position);
moveToPoint = position;
}
if (myAgent.hasPath)
{
if (fast)
speed = Mathf.MoveTowards(speed, runspeed, Time.captureDeltaTime * 3);
else speed = Mathf.MoveTowards(speed, WalkSpeed, Time.captureDeltaTime * 3);
myAnim.SetFloat("Speed", speed);
myAnim.speed = speed;
RotationToTarget(myAgent.path.corners[1]);
}
else RotationToTarget(moveToPoint);
}
public bool RotationToTarget(Vector3 position)
{
Vector3 direction = (position = transform.position).normalized;
Quaternion LookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
transform.rotation = Quaternion.RotateTowards(transform.rotation, LookRotation, Time.deltaTime * rotationSpeed);
if (transform.rotation == LookRotation)
return true;
else return false;
}
public void Stop()
{
if (speed > 0)
{
myAgent.SetDestination(transform.position);
speed = Mathf.MoveTowards(speed, 0, Time.deltaTime * 15);
myAnim.SetFloat("Speed", speed);
myAgent.speed = speed;
}
}
}
NavMeshAgent myAgent;
Animator myAnim;
public bool circularRoute;
public int curWaypoint;
public float distance;
public Vector3 moveToPoint;
public float speed;
public float WalkSpeed;
public float rotationSpeed;
public float runspeed;
public bool fast;
void Start()
{
moveToPoint = transform.position;
myAnim = GetComponent<Animator>();
myAgent = GetComponent<NavMeshAgent>();
myAgent.updatePosition = false;
}
public void Patrol()
{
if (movePoint.Length >= 1)
{
if (movePoint.Length > curWaypoint)
{
if (Vector3.Distance(transform.position, movePoint[curWaypoint].position) < 0.2)
{
curWaypoint++;
}
else
{
MoveToPoint(movePoint[curWaypoint].position);
}
}
else if (movePoint.Length == curWaypoint)
{
if (circularRoute)
curWaypoint = 0;
else
{
if (Vector3.Distance(transform.position, movePoint[curWaypoint - 1].position) < 0.2f)
{
Stop();
}
else
{
MoveToPoint(movePoint[curWaypoint].position);
}
}
}
}
}
public void MoveToPoint(Vector3 position)
{
if (moveToPoint != position)
{
myAgent.SetDestination(position);
moveToPoint = position;
}
if (myAgent.hasPath)
{
if (fast)
speed = Mathf.MoveTowards(speed, runspeed, Time.captureDeltaTime * 3);
else speed = Mathf.MoveTowards(speed, WalkSpeed, Time.captureDeltaTime * 3);
myAnim.SetFloat("Speed", speed);
myAnim.speed = speed;
RotationToTarget(myAgent.path.corners[1]);
}
else RotationToTarget(moveToPoint);
}
public bool RotationToTarget(Vector3 position)
{
Vector3 direction = (position = transform.position).normalized;
Quaternion LookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
transform.rotation = Quaternion.RotateTowards(transform.rotation, LookRotation, Time.deltaTime * rotationSpeed);
if (transform.rotation == LookRotation)
return true;
else return false;
}
public void Stop()
{
if (speed > 0)
{
myAgent.SetDestination(transform.position);
speed = Mathf.MoveTowards(speed, 0, Time.deltaTime * 15);
myAnim.SetFloat("Speed", speed);
myAgent.speed = speed;
}
}
}