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
0
Question by Kondor0 · Nov 25, 2012 at 05:33 AM · positionpathfindingvectornearest

How can I determine the nearest point to a position (for pathfinding)?

Hi

I'm working with the Unity pathfinding system in a game where the player can order an agent to move to a destination by tapping a point in the screen. The problem is that if the player taps a position too close to another agent, they will start colliding and jittering.

What I'm trying to do is to calculate a new destination for the agent that is close to the destination set by the player but with enough distance so they don't collide. My problem is that I have very little knowledge of vectors so I tried to get a direction by substracting the destination requested and the position of the moving agent and then move from that point a pair of units (the radius of the agents, actually) but the result was nothing like I was expecting (please, see the attached picture to better understand what I'm trying to do).

So my question is what changes should I do to this code in order to get the desired destination (shown in the picture)?

 newDestination = (agent1.transform.position - destinationSetByPlayer);
 newDestination = destinationSetByPlayer + (newDestination * agentRadius);
 

alt text

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

2 Replies

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

Answer by fafase · Nov 25, 2012 at 09:51 AM

My guess, use linecast. There maybe other solutions and even some better ones but here is mine.Add a sphere collider to your agent as trigger. Get the click position and draw a linecast from the player to that position. http://docs.unity3d.com/Documentation/ScriptReference/Physics.Linecast.html

Now, using the info from the linecast, if you get a collision it means you are inside the sphere collider, you get the collision with the hit variable and use it as target vector. If you get no collision tehn you use the tap position. See the pictures. The red circle is the sphere collider.

Remember to add a layer mask for your guy so that the line does not collide with himself. The sphere radius will define the min distance with your agent.

alt text


présentation1.png (11.6 kB)
Comment
Add comment · Show 3 · 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 fafase · Nov 25, 2012 at 10:02 AM 0
Share

Note that raycast would do just the same...

avatar image Owen-Reynolds · Nov 26, 2012 at 03:18 PM 0
Share

But the pathfinding system may walk red Agent1 up, over and down, putting the blue "have to stop here" X more to the northwest of the red X (in other words, the picture sometimes wrong.)

Can still use this method, but need to shoot from the 2nd to last waypoint to the last (does pathfinding still return an array of waypoints?)

avatar image Kondor0 · Nov 26, 2012 at 05:38 PM 0
Share

Seems like a nice alternate solution, if I take into account what O. Reynolds said I could be able to implement it (thanks to both of you).

I still wish I could calculate the position though, I'm pretty sure it can be done but my ignorance of vectors gets in the way (I'll give it a try and if it fails, I'm doing this). Thanks.

avatar image
0

Answer by Owen-Reynolds · Nov 26, 2012 at 03:13 PM

IMHO, unask your question. Just let them click anywhere and don't try to fix it at that stage. Maybe the blocker will move, or maybe someone will wander in your path midway.

Instead, as they move, use standard "raycast to not ram stuff." That generally includes stuff like "sidestep trees" and "close enough to target, stop here."

Comment
Add comment · Show 1 · 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 Kondor0 · Nov 26, 2012 at 05:33 PM 0
Share

But I'm trying to avoid making too much raycasts (for performance). Anyway, if the other agent moves I think I can "update" the destination of the moving agent (I have an object that keeps status of the current locations and destinations of the agents so that should help). If someone wander in the path midway, the obstacle avoidance system should solve it... I think.

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

13 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

Related Questions

Vector Based - Mini Map 1 Answer

How can I tell my laser which enemy to target? 0 Answers

How to set and objects gravity direction to Z -10 based on the objects rotation? (2D) 1 Answer

hard to get a position 1 Answer

Enemy Moves Jitters when Supposed to Be Still 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