I am trying to make a minon spawn at a location that I believe I put as a node. Then move onto the next node. My question is how do I get the node object so I can put it in the code I found.
using UnityEngine;
using System.Collections;
public class Creep : MonoBehaviour {
public Transform myFirstWaypoint;
public Transform mySecondWaypoint;
public void Start()
{
(GetComponent(typeof(Seeker)) as Seeker).StartPath(myFirstWaypoint.position, mySecondWaypoint.position);
}
// Update is called once per frame
void Update () {
}
}
If there is a better way to have the minions follow a path of only 4 or 5 nodes. I am working on a DoTA, League of Legends, Heroes of Newarth type game. The AI im looking for is the minions to go from their base to the enemy base attacking/defending towers along the way.
Here is a link to my map and A Star setup. Its too big to post on the forums without stretching the browsers.
Not super clear on what your problem is at the moment, so I’m just going to clarify a few things…
You are spawning a unit and this is not a problem?
You have a grid layout? Is this an A* map? or just a mesh?
You have a few nodes laid down that define a “path” to the enemy base? I assume each node has a “next node” property?
If I understand correctly… when you spawn your unit (assuming you want it to move immediately to the other base) is to set it’s start node when you spawn… that way your npc can move towards this node, and when it’s there, be assigned to “next node”… which should lead it to the enemy base.
A* is best when you want a “shortest path” solution… usually in somewhat complex environments.
I’d of course have to see your game to know if A* was warranted, but if it’s just a simple “follow the points” mentality on the NPCs…
You can simply have the Spawner be linked up to the first “node”. That way when the unit spawns, you can assign their first “location” to the node". Each subsequent node should detect when the NPC gets close and set it’s new target location to the next node in the list.
That is definitely the easiest way of having something working. Though A* isn’t all that difficult once you figure it out as well… but I’m not sure you need it based on what you are saying. What code are you currently using? Is the the Sturestone’s?
I am not that familiar with their work, but it should be working somewhat automatically from what I gather if that’s the system you are using. Might want to check this thread: http://forum.unity3d.com/viewtopic.php?t=43822
But if you just want simple motion, I can certainly help with that. Specific help on their script package might be better sought out in that thread though, as I believe he actively checks it. (Sorry if you aren’t a “he” sturestone!)
I was using A* but after reading what you said I have changed to something that might be simpler.
Is there a property of transform or other function that does a move towards object?
What I have now is:
void Update ()
{
float amtToMove = MinionSpeed * Time.deltaTime;
// Step is to change to the next waypoint
if (step == 0)
{
// Move slighty left and up
transform.Translate(new Vector3(-.2f, 0, 1) * amtToMove);
}
// If creep crosses x and z then increase step
if (transform.position.x >= -318 transform.position.z >= -143)
{
step = 1;
}
if (step == 1)
{
// Move barely right so not to cross previous step and up
transform.Translate(new Vector3(.01f, 0, 1) * amtToMove);
}
if(test == true)
print("step = " + step);
}
I had it set to exact positions but it some times never passed over the location.
So you would need to calculate a “heading” vector which would essentially equal your target position - your unit’s current position.
At this point you have a vector defining the movement your unit will have to take, at that point you will then want to scale that vector by multiplying by Time.deltaTime and your speed variable (if you have one…you can toy with this when you see it working).
At that point you can detect when you move over your node point. One issue I foresee is if you have multiple units moving and they start bumping eachother out of the way slightly… you may get some wierd movement… you may want to assign a “Trigger” collider to your node points… and have a script detect when a unit moves within it’s node, and then set it’s target to the next node?
There are a few ways you can handle it, but this should hopefully get you started.
This will need a ridgidbody or some other code to apply gravity or it will make stuff float.
Seek.
//get direction
moveDirection = movementGoal - transform.position;
//zero out y since we dont want this unit to fly
moveDirection.y = 0;
//normalize so everything = 1 total;
moveDirection = moveDirection.normalized * maxSpeed;
Set movementGoal as the place you want to go to (Vector3), then when you get nere it.
IncrementWaypoint.
if (moveDirection.magnitude > distanceToMoveTo)
{
//Find Next MoveGoal
}
Just place Empty gameObjects around your level and put a script on them that holds the next node to move to and have your unit ask the node for the next.
Or have a array of nodes that you just go to the next one.(by removeing it from array or counting up/down)