NavmeshAgent pathfinding with FirstPersonController
Hi,
I'm trying to adapt the FirstPersonController from the Unity's Standard Assets for a Point and Click usage with NavMeshAgent. The point is to click on a node on the screen and the character moves to this node using the NavMeshAgent pathfinding but I want to keep the head bobbing and the foot steps provided by the script.
I think I need to sync the direction of the next move with the Agent's one by moving the character with FirstPersonController script using NavMesh pathfinding but I can't find how.
Here's what I tried :
private void GetInput(out float speed)
{
// Read input
float horizontal = CrossPlatformInputManager.GetAxis("Horizontal");
float vertical = CrossPlatformInputManager.GetAxis("Vertical");
bool waswalking = m_IsWalking;
#if !MOBILE_INPUT
// On standalone builds, walk/run speed is modified by a key press.
// keep track of whether or not the character is walking or running
m_IsWalking = !Input.GetKey(KeyCode.LeftShift);
#endif
// set the desired speed to be walking or running
speed = m_IsWalking ? m_WalkSpeed : m_RunSpeed;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
//Debug.DrawRay(transform.position, Vector3.back * 1000);
if (Physics.Raycast(ray, out hit, 1000))
{
//Debug.Log("Hit");
if (hit.collider.gameObject.layer == 8 && hit.collider != null)
{
if (Input.GetMouseButtonDown(0))
{
//navMesh.destination = hit.point;
m_Input = new Vector2(hit.point.x, hit.point.z);
}
}
}
//m_Input = new Vector2(horizontal, vertical);
// normalize input if it exceeds 1 in combined length:
if (m_Input.sqrMagnitude > 1)
{
m_Input.Normalize();
}
// handle speed change to give an fov kick
// only if the player is going to a run, is running and the fovkick is to be used
if (m_IsWalking != waswalking && m_UseFovKick && m_CharacterController.velocity.sqrMagnitude > 0)
{
StopAllCoroutines();
StartCoroutine(!m_IsWalking ? m_FovKick.FOVKickUp() : m_FovKick.FOVKickDown());
}
}
I know this is not good because m_Input is set to the destination point and not to the next step, I also tried with NavMeshAgent.nextPosition but it's still not working.
Need help ! Thanks !
Answer by axepoty · Sep 05, 2020 at 09:43 AM
Finally managed to find a solution by replacing all m_CharacterController.velocity by my NavMeshAgent velocity and use the destination system instead of CharacterController.Move() method.