Question by
NightBurger · Feb 21, 2016 at 04:35 PM ·
navmeshnavmeshagenttagfindgameobjectswithtag
Navmesh, help setting position to nearest tag? (Javacript)
I'm working on an action rts and trying to create troop movements like "Click to go there" and then "charge". On the charge mechanic (lower part of the code), I cannot at all figure out how to make it so that they will target the nearest game object with tag. How do I (using the navmesh) have my object go to the nearest object with the tag "Enemy". It'll be nice to learn an If statement for when the object is in range. here's the code:
var agent: NavMeshAgent;
private var RTSLock : int = 1;
var destination: Vector3;
function Start() {
agent = GetComponent.<NavMeshAgent>();
destination = agent.destination;
}
function Update() {
if (Input.GetKeyDown(KeyCode.F1))
{
RTSLock = 1;
}
if (Input.GetKeyDown(KeyCode.F2))
{
RTSLock = 2;
}
if (Input.GetMouseButtonDown(0)) {
if (RTSLock == 1)
{
var hit: RaycastHit;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit, 100)) {
agent.destination = hit.point;
}
}
}
if (RTSLock == 2){
if (Input.GetMouseButtonDown(1))
{
agent.SetDestination(GameObject.FindWithTag("Enemy").transform.position);
}
}
}
I tried working with the example they give you but I cannot get it to work at all.
Comment