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

Drawing a line in-game that follows terrain formation

Discussion in 'Scripting' started by niklasss, Aug 20, 2014.

  1. niklasss

    niklasss

    Joined:
    Aug 20, 2014
    Posts:
    2
    Hello!

    I'm trying to create a system that allows the player to create kind of paths in-game.

    So what this means is that the player can click somewhere in the terrain to place a starting point (point A) of the path and then click somewhere else in the terrain to place the ending point (point B) and the game would draw a line between those points.

    I was reading the documentation of unity's line renderer and the documentation says the following:
    The Line Renderer takes an array of two or more points in 3D space and draws a straight line between each one.

    I don't however want the line to be straight. I want it to follow the formation of the terrain. So to explain it we would have point A on the other side of some mountain and point B on the other side. When player has placed the points I don't want the line to be drawn through that mountain but over it following the formation of the terrain.

    Would the line renderer be a good way to approach this problem? Can this be achieved with it?

    Thanks!
     
  2. daemon3000

    daemon3000

    Joined:
    Aug 13, 2012
    Posts:
    139
    You could cast rays down on the terrain at certain intervals between A and B and store the hit positions in a list. Then you can draw lines between those points. If that doesn't give you the required quality or it's too slow you might want to do some research on pathfinding.
     
  3. mhtraylor

    mhtraylor

    Joined:
    Jan 13, 2013
    Posts:
    33
    You could also sample some heights on the terrain between point A and point B, with the Terrain classes.
     
  4. niklasss

    niklasss

    Joined:
    Aug 20, 2014
    Posts:
    2
    In theory pathfinding might be what I'm looking for. I definitely have to do more research on that. Also I hadn't even thought about the other option, interesting thought. Thanks guys!