Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 mokabiri · Jun 13, 2017 at 02:13 PM · unity 5navmeshnavmeshagentpathfindingnavigation

Put (Instantiate) Gameobject along or accross a path Unity

I'm a beginner on Unity and I want to create a personal project, alone. I have two Gameobjects on the Scene, I used LineRenderer and NavMesh agent on Unity to draw a path between two gameobjects, So far, everything's good, Unity draw path between two Gameobjects. Here draw path

  Void Update (){
     OnDrawGiz(GameObject.Find("Destination"));
   }
   void OnDrawGiz(GameObject obj)
    {

 Transform target = obj.GetComponent<Transform> ();
 NavMeshAgent nav;
 LineRenderer line;
 nav= this.GetComponent<NavMeshAgent>();
 if( nav == null || nav.path == null )
     return;
 line = this.GetComponent<LineRenderer>();
 line.material = new Material( Shader.Find( "Sprites/Default" ) ) { color 
      = Color.yellow };

 line.startColor=Color.yellow;
 line.endColor = Color.green;

 nav.SetDestination (target.position);
 nav.isStopped=true;
 var path = nav.path;
 if (path.corners.Length < 2)
     return ;

 line.positionCount=path.corners.Length ;

 //Draw the line
 for( int i = 0; i < path.corners.Length; i++ )
 {
     line.SetPosition( i, path.corners[i] );
 }
  }

Now my question is how can I put and duplicate a Gameobject for example a cube along or across this path please?! i want to achieve like this][2]

[2]: /storage/temp/95876-capture-decran-want.png

capture-decran-2017-06-13-a-152331.png (143.3 kB)
capture-decran-want.png (97.0 kB)
Comment
Add comment · Show 1
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 mokabiri · Jun 13, 2017 at 02:20 PM 0
Share

I use C# script, Unity 5.6

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Dave-Carlile · Jun 13, 2017 at 05:50 PM

You can use the Vector3.Lerp function to find a point on a line in between any two vectors.

For example, given vectors a and b, you can find the vector directly in the middle like this:

 Vector3 middle = Vector3.Lerp(a, b, 0.5f);


You can think of the t parameter as a percentage between the two vectors. 0.5 is 50%, 0.25 would be 25%, and so on. If you want to add multiple objects you can use a loop variable to modify t. For example:

 for (int i = 0; i < 4; i++)
 {
   Vector3 position = Vector3.Lerp(a, b, i * 0.25f);
   // TODO : create object at position
 }    


Comment
Add comment · Show 2 · 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 mokabiri · Jun 13, 2017 at 07:15 PM 0
Share

Thanks Dave-Carlile, it work correctly :)

avatar image mokabiri · Jun 13, 2017 at 07:27 PM 0
Share

Here is my result on Unity alt text

capture-decran-2017-06-13-a-211819.png (131.7 kB)

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

131 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I get NavMeshAgents not to walk through objects? 3 Answers

How to build a custom shape NavMesh Area 1 Answer

Increase size of obstacle - navmesh 1 Answer

What is the difference between configuration of agent in 'Agents' tab and in 'Bake' tab Unity? 1 Answer

Get NavMeshAgent to look where it's going 0 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