On my FPSController I have two scripts, Player Controller and Agent Controller.
Player Controller is for using the keys WSAD for moving the player around.
Agent Controller uses a NavMeshAgent to move the player to a position clicked with the mouse (like point and click ).
Everything is working fine, or was working fine until I tried to move the player around using the keys WSAD after clicking the mouse.
I clicked the mouse on the ground (terrain) or on a cube no matter what I clicked on and the player is moving to this position. Now at that position the player stops. Now I'm trying to move the player with the keys WSAD, but even if I click on S for example long time the and player is far away from the last mouse clicked position when I leave the S key the player automatically moves back to the mouse clicked position.
The player should stay at where I move him with the keys WSAD but he keeps moving back to the last clicked mouse position.
On the FPSController I have a Rigidbody and NavMeshAgent and the two scripts Player Controller and Agent Controller.
The original Agent Controller script was:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class AgentController : MonoBehaviour
{
public Camera camera;
public NavMeshAgent agent;
public bool agentControl = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
if (Input.GetMouseButtonDown(0) && agentControl == true)
{
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
agent.SetDestination(hit.point);
}
}
}
}
And the Player Controller script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed = 10.0f;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
float translatioin = Input.GetAxis("Vertical") * speed;
float straffe = Input.GetAxis("Horizontal") * speed;
translatioin *= Time.deltaTime;
straffe *= Time.deltaTime;
transform.Translate(straffe, 0, translatioin);
if (Input.GetKeyDown("escape"))
Cursor.lockState = CursorLockMode.None;
}
}
The main goal is to use either the Agent Controller for point and click with the navmeshagent or using the keys WSAD to move around.