- Home /
Make one unit move towards another.
Edit: Okay tossed out the previous script after digging up some tutorials yet this still isn't working any help?
I am working on some basic scripts so that I can use them as the basis for a few RTS games I have in mind. I wrote out this script and applied it but for some reason the unit it is attached to is not moving towards the enemy. Anyone have any ideas what I am doing wrong? Oh I should also note that when I put it in the editor and tell it to just go after the object it will start flying off like crazy in some random direction.
private var human : Transform;
private var nav : NavMeshAgent;
private var LOS = 10;
function Start () {
human = GameObject.FindGameObjectsWithTag ("Human").transform;
nav = GetComponent (NavMeshAgent);
}
function Update () {
nav.SetDestination(human.position);
}
Also I have another script to create the tags as vars it goes
public static var human : String = "Human";
If you have a Nav$$anonymous$$esh agent then that could be overriding your position. Try letting the Nav$$anonymous$$eshAgent move the character ins$$anonymous$$d of using $$anonymous$$oveTowards or disable the Nav$$anonymous$$eshAgent.
Answer by Mmmpies · Feb 04, 2015 at 09:10 AM
Not well up on JS but your code looks absolutely fine. I'll take it as read that you've baked the navmesh into the scene already so let's get some idea of what's going on. What is the "Human" character? It's just most people call their enemies enemy or something bad.
If this is the enemy then that's fine if it's the character you want to move then that's where the error is. You'd be asking the character to move to it's own location, it's already there so it'll just stay where it is.
Test this by putting an empty GameObject on the scene and call it something unique like MoveToHere then try that instead of heading to your Human character. Or even just nick this direct from the Scripting API page:
private var agent: NavMeshAgent;
function Start () {
agent = GetComponent.<NavMeshAgent>();
}
function Update () {
var hit: RaycastHit;
// When the mouse is clicked...
if (Input.GetMouseButtonDown(0)) {
// If the click was on an object then set the agent's
// destination to the point where the click occurred.
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit)) {
agent.SetDestination(hit.point);
}
}
}
Unity staff wrote that code so if it doesn't work then you know there's some major component missing.
I am working on a RTS game and one of the factions is Human so I figured having a tag for them would be good. Lemme test your code.
Your script works so all the things seem to be in thee so I am not sure what is wrong with $$anonymous$$e?
Bit of an update I am noticing the error pop up "NullReferenceException: Object reference not set to an instance of an object Basic Enemy.Update () (at Assets/Basic Enemy.js:11) " Does that help?
Sorry @Rin night fell. Yes null reference is important and that line is the nav line, chances are it's not picked up the Nav$$anonymous$$eshAgent. $$anonymous$$y knowledge of JS is poor but the documentation suggest your line is O$$anonymous$$, having said that if the example code worked try setting the line as they do in that code.
nav = GetComponent.<Nav$$anonymous$$eshAgent>();
Let's also try some Debug.Log lines so we know where we are.
function Update () {
if(nav != null)
{
nav.SetDestination(human.position);
Debug.Log("my pos = " + transfrom.position);
Debug.Log("human pos = " + human.positon);
}
I put in the debug code but I am getting an error on the "my pos" line saying "unknown identifier transform".
Your answer
Follow this Question
Related Questions
Object rotation to unit vector 1 Answer
Component based architecture Unit, AI, Health, Attack 0 Answers
iTween MoveTo/MoveUpdate 1 Answer
how can i throw the object? need all step to create and throw. 1 Answer
Help making script!! 1 Answer