Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

I want to create an object between distances.

Discussion in 'Scripting' started by kixkout, May 22, 2020.

  1. kixkout

    kixkout

    Joined:
    Sep 26, 2014
    Posts:
    2
    hello.
    The distance value between p0 and p1.
    I want to create a gameObject between distances.
    The size of the prefab is fixed.

    As you can see, it can be generated in a straight line or in a diagonal line.

    I want to create a prefab starting from p1 to p0 and placing it exactly.
    What should I do?

    Please need your help.
    thank you for reading.

    distance.jpg
     
  2. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    781
    Perhaps something like this (haven´t tested it though):

    totalDistance = P1.transform.position - P0.transform.position;
    numberOfPrefabs = totalDistance.Magnitude / prefabForwardLengh;


    distanceStep = totalDistance / numberOfPrefabs;

    Then just put them there with:
    Code (CSharp):
    1. for (int i=1; i<numberOfPrefabs; i++)
    2. {
    3.      Instantiate(prefab, P0.transform.position + distanceStep * i, theRotationYoudLike);
    4. }
     
    Last edited: May 22, 2020