1

I have created navMeshand agent. For target I used two empty object. For each empty object I created two buttons.

If I click on white button agent moves to empty target first again I click on red button agent moves to 2nd empty target.

I am facing problem when I want move agent from target-2 to target-1. How I can move that agent to taeget-1?

See video for better understanding

video link https://youtu.be/zRKHdMeQsi0

Code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class SampleAgentScript : MonoBehaviour {

    public Transform target , target2;
    NavMeshAgent agent;
    private static bool start1=false , start2=false;

    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
    }

    public static void buttonClick()
    {
        //if white button click
        start1 = true;
    }

    public static void buttonClick2()
    {
        //if red button click
        start2 = true;
    }

    void Update()
    {
        if (start1) //if white button click moves to targer-1
        {
            agent.SetDestination(target.position);
        }

        if (start2) //if red button click moves to targer-2
        {
            agent.SetDestination(target2.position);
        }
    }
}
Basile Perrenoud
  • 4,039
  • 3
  • 29
  • 52
Night Programmer
  • 341
  • 5
  • 25

3 Answers3

2

You have forgotten to alternate the state by resetting the boolean values to false. As you have set boolean values in the button click handler, so you can alternate states in the update function as well.

void Update()
{
    if (start1) //if white button click moves to targer-1
    {
        agent.SetDestination(target.position);
        start1=false;
    }

    if (start2) //if re button click moves to targer-2
    {
        agent.SetDestination(target2.position);
        start2=false;
    }
}
Rashedul.Rubel
  • 3,446
  • 25
  • 36
  • Hello Sir, please help me to solve this question , **https://stackoverflow.com/questions/53117631/navmeshagent-player-not-parallel-to-slope-of-hill-when-moving-over-hill** – Night Programmer Nov 03 '18 at 05:51
1

May be this will help.

public static void buttonClick()
{
      //if white button click
    start1 = true;
    start2 = false;
}

public static void buttonClick2()
{
     //if red button click
    start2 = true;
    start1 = false;
}
Bhavin Panara
  • 428
  • 4
  • 11
0

When you click the second button both of the conditions become true and in each frame you are setting two different destinations.

public Transform dest, target , target2;

public void buttonClick()
{
     dest = target;
}

public void buttonClick2()
{
     dest = target2;
}

void Update()
{
     agent.SetDestination(dest .position);
}
Milad Qasemi
  • 3,011
  • 3
  • 13
  • 17
  • how I can use non static member in Static method? if create instance then its not working. – Night Programmer Oct 29 '18 at 09:52
  • AFAIK that is not possible, Static methods cannot access non static fields. BTW there is no need that your button click functions to be `static` – Milad Qasemi Oct 30 '18 at 08:28
  • **buttonClick** function I am calling from another script file. for that calling I used **static** function – Night Programmer Oct 30 '18 at 09:15
  • You can call it form any script there is no need for it to be `static`, to learn how to do it you can check [this](https://unity3d.com/learn/tutorials/topics/user-interface-ui/ui-button) unity tutorial – Milad Qasemi Oct 30 '18 at 17:40
  • Hello Sir, please help me to solve this question , **https://stackoverflow.com/questions/53117631/navmeshagent-player-not-parallel-to-slope-of-hill-when-moving-over-hill** – Night Programmer Nov 03 '18 at 05:51