Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Leprekhaun · May 19, 2014 at 12:19 AM · pathfindingrts

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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?

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Leprekhaun · May 19, 2014 at 01:53 AM 0
Share

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?

avatar image Cherno · May 19, 2014 at 02:10 AM 1
Share

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.

avatar image Leprekhaun · May 19, 2014 at 03:14 AM 0
Share

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); } } } }

avatar image Cherno · May 19, 2014 at 08:12 PM 0
Share

Dont declare the involved variables (prefab and it's clone) as Objects; Delcare them as GameObjects.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges