Search Unity

NavmeshAgent SetDestination combined with Input.GetAxis issue

Discussion in 'Navigation' started by MlleBun, Dec 2, 2018.

  1. MlleBun

    MlleBun

    Joined:
    Sep 19, 2017
    Posts:
    163
    Hello everyone,

    I have a weird issue when combining NavmeshAgents with translation using Input.GetAxis("Horizontal") that I don't really understand.

    I would like the player to move to target in Z only and be able to manually translate in X-axis using Input.GetAxis("Horizontal"). For that, I have a simple SetDestination function on the player to makes it go to a target located at 1700 units in Z from it, and a float MoveHorizontal() that translate the player in X depending on Input.GetAxis("Horizontal").

    Here is the complete code below :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. public class PlayerControllerBug : MonoBehaviour
    7. {
    8.  
    9.     [Header("Move Player to target")]
    10.     public Transform target;
    11.     public float speed;
    12.     NavMeshAgent Agent;
    13.  
    14.     void Start()
    15.     {
    16.         Agent = GetComponent<NavMeshAgent>();
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         MoveToTarget();
    22.     }
    23.  
    24.     /*******************************
    25.     * Method to move to target
    26.     * ******************************/
    27.     void MoveToTarget()
    28.     {
    29.         Agent.speed = speed;
    30.         Agent.SetDestination(new Vector3(MoveHorizontal(speed), 0, target.transform.position.z));
    31.     }
    32.  
    33.     /********************************************
    34.     * Method to move this horizontally only
    35.     * *******************************************/
    36.     float MoveHorizontal(float speedH)
    37.     {
    38.         float h = Input.GetAxis("Horizontal");
    39.  
    40.         float translate = h * speedH;
    41.         translate *= Time.deltaTime;
    42.         Debug.Log(translate); // logging purposes
    43.  
    44.         return translate;
    45.     }
    46. }
    47.  


    The issue is that at beginning of the game, the player will keep going back to 0 whenever I move it left, i.e when Input.GetAxis("Horizontal") is negative. Moving right is ok. What is weird is that this behaviour dissapears while approaching target (at least that's what I'm guessing).

    Can anyone point to me what I am surely missing ? And thanks for your help :rolleyes:

    SetDestination&Translate.gif
     
    Last edited: Dec 2, 2018
  2. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    Well, you're constantly setting your destination back to 0,0,z when there is no input. Because of that your agent will automatically begin walking back. It's strange that the behaviour seems to disappear, but that's probably what's causing it.
    Honestly combining direct movement input with Navigation is generally a bad idea, you might be better off figuring out the z-position of your player another way, like by applying gravity to keep your character grounded.