0
\$\begingroup\$

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.

Agent

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.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

When you call NavMeshAgent.SetDestination(Vector3 destination), you're telling it "your job from now on is to move toward this point. You don't give it a time limit or an interrupt condition or even an instruction to forget its destination once it arrives. It will just keep heading toward that spot until you tell the NavMeshAgent you want it to do something else.

So, tell it you want it to do something else.

When you get nonzero input on the horizontal or vertical axes (ie. from the WASD keys), call NavMeshAgent.ResetPath() so it knows you've cancelled the last move order and it should stop trying to move to the last clicked point until you give it a new destination.

\$\endgroup\$
3
  • \$\begingroup\$ It's working for the W and D keys ( Forward and Right ) but if I press on A or S keys it's not working. In the Player Controller script I added a variable public NavMeshAgent agent; then after the float straffe line...in the Update I did: if (translatioin > 0 || straffe > 0) agent.ResetPath(); \$\endgroup\$
    – Daniel Lip
    Jul 30, 2018 at 17:37
  • 1
    \$\begingroup\$ Remember, there's two ways to be different from zero. Checking only the positive direction means you missed the negatives. \$\endgroup\$
    – DMGregory
    Jul 30, 2018 at 17:39
  • \$\begingroup\$ Right I'm checking now if both values not equal zero. And now it's working. \$\endgroup\$
    – Daniel Lip
    Jul 30, 2018 at 17:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .