• 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 /
  • Help Room /
avatar image
0
Question by pokitman12 · May 02, 2018 at 08:23 AM · distancepositions

Figuring out positions within a distance between two points.

Hi guys. It's kinda difficult to explain what I'm trying to achieve here but here goes.

Basically I'm trying to get the distance between two points, Get each integer within that float value and figure out the position if each integer.

In the Image I've attached, You can see there are two points set at a distance of 5. I've manually placed markers at each 1 unit along the line. It's these positions of each marker I'm trying to figure out.

So for example, in this instance, Once pointA and pointB have been positioned, A collectable object can be placed at marker 1,2,3,4 and pointB position.

I'm unsure if I'm even approaching this in the right way.

I hope I've explained this clearly. Any advice on how to approach this would be very much appreciated.

Thanks

alt text

image4161.png (37.9 kB)
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
Best Answer

Answer by Hellium · May 02, 2018 at 08:56 AM

I haven't tested the following script, but I think it's a good start:

 public Vector3[] GetCollectablePositions( Vector3 pointA, Vector3 pointB, float distance )
 {
     // The direction vector between A and B
     Vector3 direction = pointB - pointA ;
 
     // Get the number of collectables
     int collectableCount = Mathf.Floor( (direction.magnitude - Mathf.Epsilon )  / distance ) ;
 
     // Create the array of positions to gather all the positions
     Vector3[] collectablePositions = new Vector3[collectableCount];
 
     // Direction is now a vector separating two collectables
     direction /= collectableCount ;
 
     // Fill the array by determining the positions of the collectables according to the direction vector
     // and the position of A
     for( int collectableIndex = 0 ; collectableIndex < collectableCount ; ++collectableIndex )
     {
         collectablePositions[collectableIndex] = pointA + direction * (collectableIndex + 1)  ;
     }    
 
     return collectablePositions ;
 } 

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 pokitman12 · May 02, 2018 at 05:55 PM 0
Share

Thank you very much! After a few hours of reading into $$anonymous$$athf.Epsilon and $$anonymous$$athf.Floor as I've never used these before, I've finally got it. Thanks Again !

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Related Questions

Fade Material's Transparency Based on Distance 3 Answers

Scaling Crosshair 0 Answers

Missing GameObject, help!! 0 Answers

Y position incorrect in code, correct in inspector. Constantly changes. 0 Answers

VIVE Parts Callouts 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges