- Home /
Instantiating A* PF target for RTS
This question has been answered a hundred times but I can't find an answer that helps me. So far here is the code I have.
Object newlocation = (GameObject)Instantiate(Resources.Load("AstarTarget"));
bool going;
// Use this for initialization
void Start () {
going =false;
}
// Update is called once per frame
void Update (){
if(Input.GetMouseButtonDown(0)){ // the button is pressed
newlocation=Input.mousePosition;
going = true;}
if(going){
if((Vector3.Distance(newlocation,transform.position))>0.5f){
transform.LookAt(newlocation);
transform.Translate(Time.deltaTime, 0, Time.deltaTime);
}
}
else
going =false;
}
}
Now I'm not sure how I should word this correctly so that when I click the left mouse button it creates the prefab that the A* pathfinding will move my object to where I click on my terrain. Everything I have found either goes over my head or does not work the way I am inputting it in.
Answer by Cherno · May 19, 2014 at 01:40 AM
Why are you instantiating a target prefab? Wouldn't it suffice to just get teh position you are clicking an (via RayCast) and uses the Vector3 of the resulting hit.point directly as the move-to target?
would that work with pathfinding? so far i can get my player object to teleport to where i click but not actually pathfind to the location. if i use a target prefab i should be able to put that in the pathfinding script for target. maybe im asking the wrong question or phrasing it wrong so how do i get an instance of an object to spawn where i click on my terrain?
I ony ever used Aron Granberg's Astar Pathfinding, and there you just need to provide a Vector3 for the pathfinding to work.
If you really need a target (I highly doubt that as it would mean dozens of otherwise useless gameobjects in the scene if multiple actors are all pathfinding), I would suggest no instantiating it in the main body.
Do it like this:
GameObject newlocationPrefab;
GameObject newlocationClone;
void Start() {
newlocationPrefab = Resources.Load("AstarTarget");
newlocationClone = Instantiate(newlocationPrefab , Vector3.zero, Quternion.identity) as GameObject;
}
Then you just change the newlocationClones's transform.position to whereever you clicked on the terrain.
Take a look at the Unity Script Reference for Raycasting for further information.
Ok I did what you suggested and also looked into ray casting but now I'm getting an error saying cannot convert Object to GameObject...
void Start () { newlocationPrefab = Resources.Load("AstarTarget"); newlocationClone = Instantiate(newlocationPrefab , Vector3.zero, Quaternion.identity) as GameObject; }
void Update () { if(Input.Get$$anonymous$$ouseButtonDown(0)) { Ray ray; RaycastHit hit; ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit, 100.0f)) { if (hit.collider.name=="New Terrain") Instantiate(newlocationClone, hit.point, Quaternion.identity); } } } }
Dont declare the involved variables (prefab and it's clone) as Objects; Delcare them as GameObjects.
Your answer
Follow this Question
Related Questions
How to detect multiple objects for avoidance 1 Answer
How to add multiple values to single variable(or something else)? 0 Answers
adding multiple values to single function/variable. 0 Answers
Unit local avoidance in RTS type games 3 Answers
Why does the Unity Navmesh Agent slow down while traversing obstacles? 2 Answers