131 lines
3.0 KiB
C#
131 lines
3.0 KiB
C#
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using UnityEngine;
|
|||
|
using UnityEngine.AI;
|
|||
|
|
|||
|
using MyBT;
|
|||
|
|
|||
|
public class BtNavmeshCtrl : MonoBehaviour {
|
|||
|
|
|||
|
Vector3 startPosition;
|
|||
|
Vector3 targetPosition;
|
|||
|
NavMeshAgent navMeshAgent;
|
|||
|
|
|||
|
public void Start() {
|
|||
|
startPosition = transform.position;
|
|||
|
navMeshAgent = GetComponent<NavMeshAgent>();
|
|||
|
}
|
|||
|
|
|||
|
[Task]
|
|||
|
public void Rotate() {
|
|||
|
if (Task.isStartingOrRunning) {
|
|||
|
// Debug.Log("Rotate");
|
|||
|
transform.Rotate(new Vector3(0, 30 * Time.deltaTime, 0));
|
|||
|
// Task.SetSucceeded(this);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
[Task]
|
|||
|
public void RaycastCheck () {
|
|||
|
if (Task.isStartingOrRunning) {
|
|||
|
RaycastHit hit;
|
|||
|
|
|||
|
Vector3 source = transform.position + Vector3.up * 1;
|
|||
|
// Does the ray intersect any objects excluding the player layer
|
|||
|
Debug.DrawRay(source, transform.TransformDirection(Vector3.forward) * 20, Color.green);
|
|||
|
if (Physics.Raycast(source, transform.TransformDirection(Vector3.forward), out hit, 20)) {
|
|||
|
Debug.DrawRay(source, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
|
|||
|
//Debug.Log("Did Hit");
|
|||
|
if (hit.collider.CompareTag("Player")) {
|
|||
|
if (Task.isDebugging) Debug.Log("Player Detected");
|
|||
|
targetPosition = hit.collider.transform.position;
|
|||
|
Task.SetSucceeded();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
[Task]
|
|||
|
public void MoveTo () {
|
|||
|
if (Task.isStarting) {
|
|||
|
|
|||
|
}
|
|||
|
if (Task.isStartingOrRunning) {
|
|||
|
if (navMeshAgent == null) {
|
|||
|
navMeshAgent = GetComponent<NavMeshAgent>();
|
|||
|
}
|
|||
|
navMeshAgent.SetDestination(targetPosition);
|
|||
|
if (ReachedDestination()) {
|
|||
|
Debug.Log($"Reached Destination {gameObject.name}");
|
|||
|
Task.SetSucceeded();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
[Task]
|
|||
|
public void Avoid () {
|
|||
|
if (Task.isStarting) {
|
|||
|
|
|||
|
}
|
|||
|
if (Task.isStartingOrRunning) {
|
|||
|
Vector3 realTargetPos = (transform.position - targetPosition) + transform.position;
|
|||
|
navMeshAgent.SetDestination(realTargetPos);
|
|||
|
if (ReachedDestination()) {
|
|||
|
Task.SetSucceeded();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
[Task]
|
|||
|
public void Return () {
|
|||
|
if (Task.isStarting) {
|
|||
|
navMeshAgent.SetDestination(startPosition);
|
|||
|
}
|
|||
|
if (Task.isStartingOrRunning) {
|
|||
|
if (ReachedDestination()) {
|
|||
|
Task.SetSucceeded();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
[Task]
|
|||
|
public void SetVelocity (float speed) {
|
|||
|
if (Task.isStartingOrRunning) {
|
|||
|
navMeshAgent.speed = speed;
|
|||
|
Task.SetSucceeded();
|
|||
|
}
|
|||
|
if (Task.isAborting) {
|
|||
|
// Task.SetError();
|
|||
|
// Task.SetSucceeded();
|
|||
|
// Task.SetFailed();
|
|||
|
Task.SetUndefined();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
public bool ReachedDestination() {
|
|||
|
if (!navMeshAgent.pathPending) {
|
|||
|
if (navMeshAgent.remainingDistance <= navMeshAgent.stoppingDistance) {
|
|||
|
if (!navMeshAgent.hasPath || navMeshAgent.velocity.sqrMagnitude == 0f) {
|
|||
|
return true;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
public void SphereCast () {
|
|||
|
Vector3 source = transform.position + Vector3.up * 1;
|
|||
|
RaycastHit[] raycastHits = Physics.SphereCastAll(source, 5, Vector3.up);
|
|||
|
if (raycastHits.Length > 0) {
|
|||
|
foreach (RaycastHit rayHit in raycastHits) {
|
|||
|
if (rayHit.collider.gameObject.CompareTag("Player")) {
|
|||
|
Task.SetSucceeded();
|
|||
|
navMeshAgent.SetDestination(startPosition);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
}
|