155 lines
5.1 KiB
C#
155 lines
5.1 KiB
C#
using PixelCrushers.DialogueSystem;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.AI;
|
|
|
|
public class NahalController : MonoBehaviour
|
|
{
|
|
Transform m_PlayerTransform;
|
|
public float m_TargetHeight = 1.8f;
|
|
public float m_followRate = 1.0f;
|
|
NavMeshAgent m_meshAgent;
|
|
NavMeshPath m_path;// = new NavMeshPath();
|
|
int m_currentPathPoint = -1;
|
|
public float m_pathPointDistance = 1f;
|
|
public float m_randomMotionFreq = 1f;
|
|
public float m_randomMotionScale = 0.2f;
|
|
public float m_maxPlayerDist = 5f;
|
|
public float m_minPlayerDist = 2f;
|
|
public float m_minTargetDistFromPlayer = 1f;
|
|
Vector3 m_destinationPoint;
|
|
|
|
Vector3 m_randShift = Vector3.zero;
|
|
public Transform m_ballTransform;
|
|
public Transform m_targetTransformWaypoint;
|
|
// Start is called before the first frame update
|
|
public enum State { IDLE, FOLLOW_PLAYER, FOLLOW_PATH }
|
|
public State m_state = State.FOLLOW_PLAYER;
|
|
IEnumerator GenerateRandomShift()
|
|
{
|
|
while (true)
|
|
{
|
|
m_randShift.x = Random.Range(-m_randomMotionScale, m_randomMotionScale);
|
|
m_randShift.y = Random.Range(-m_randomMotionScale, m_randomMotionScale) + m_TargetHeight;
|
|
m_randShift.z = Random.Range(-m_randomMotionScale, m_randomMotionScale);
|
|
|
|
yield return new WaitForSeconds(m_randomMotionFreq);
|
|
}
|
|
}
|
|
void Start()
|
|
{
|
|
m_meshAgent = GetComponent<NavMeshAgent>();
|
|
|
|
var p =GameObject.FindGameObjectWithTag("Player");
|
|
if (p)
|
|
{
|
|
m_PlayerTransform = p.transform;
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("Player not found!");
|
|
}
|
|
StartCoroutine(GenerateRandomShift());
|
|
}
|
|
void SetState(State state)
|
|
{
|
|
m_state = state;
|
|
}
|
|
|
|
void MoveBallTowards(Vector3 localPos)
|
|
{
|
|
m_ballTransform.localPosition = Vector3.Lerp(localPos, m_ballTransform.localPosition, Mathf.Exp(-Time.deltaTime * m_followRate));
|
|
|
|
}
|
|
|
|
Vector3 GetPositionNextToPlayer()
|
|
{
|
|
return m_PlayerTransform.position + m_PlayerTransform.right * m_minTargetDistFromPlayer;
|
|
}
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
// Vector3 currGroundPos = transform.position - Vector3.up * m_TargetHeight;
|
|
Vector3 playerPos = GetPositionNextToPlayer();
|
|
var diff = playerPos - transform.position;
|
|
diff.y = 0f;
|
|
float playerDist = diff.magnitude;
|
|
switch (m_state)
|
|
{
|
|
case State.FOLLOW_PLAYER:
|
|
//m_meshAgent.
|
|
if (!m_meshAgent.hasPath)
|
|
{
|
|
if (playerDist > m_minPlayerDist)
|
|
{
|
|
m_destinationPoint = playerPos;
|
|
m_meshAgent.SetDestination(m_destinationPoint);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var dist = (playerPos - m_destinationPoint).magnitude;
|
|
if (dist > m_maxPlayerDist)
|
|
{
|
|
m_destinationPoint = playerPos;
|
|
m_meshAgent.SetDestination(m_destinationPoint);
|
|
}
|
|
}
|
|
//Vector3 targetPos = m_PlayerTransform.position + Vector3.up * m_TargetHeight + m_randShift;
|
|
//transform.position = Vector3.Lerp(targetPos, transform.position, 0.9f);
|
|
//MoveTowards(targetPos);
|
|
break;
|
|
case State.FOLLOW_PATH:
|
|
if (m_path != null && m_path.status == NavMeshPathStatus.PathComplete)
|
|
{
|
|
|
|
if (m_meshAgent.hasPath)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var currPoint = m_path.corners[m_currentPathPoint];
|
|
diff = currPoint - transform.position;
|
|
diff.y = 0f;
|
|
if (diff.magnitude < m_pathPointDistance && playerDist < m_maxPlayerDist)
|
|
{
|
|
m_currentPathPoint++;
|
|
if (m_currentPathPoint == m_path.corners.Length)
|
|
{
|
|
SetState(State.FOLLOW_PLAYER);
|
|
m_path = null;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
m_meshAgent.SetDestination(currPoint);
|
|
//targetPos = currPoint + Vector3.up * m_TargetHeight + m_randShift;
|
|
// MoveTowards(targetPos);
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
m_path = new NavMeshPath();
|
|
|
|
|
|
if (m_meshAgent.CalculatePath(m_targetTransformWaypoint.position, m_path))
|
|
{
|
|
m_currentPathPoint = 0;
|
|
|
|
}
|
|
else
|
|
{
|
|
SetState(State.FOLLOW_PLAYER);
|
|
}
|
|
}
|
|
break;
|
|
case State.IDLE:
|
|
break;
|
|
}
|
|
MoveBallTowards(m_randShift);
|
|
|
|
}
|
|
}
|