Search Unity

Trying to spawn cylinder ending at a raycast hit and beginning at its origin

Discussion in 'Editor & General Support' started by WilkinsMicawber, Dec 18, 2016.

  1. WilkinsMicawber

    WilkinsMicawber

    Joined:
    Jan 22, 2016
    Posts:
    27
    I am sending a raycast and want a cylinder to spawn stretching from the start of the raycast to the location the raycast hits. I'm familiar with sending raycasts but cannot for the life of me figure out how to orientate the cylinder correctly. I set the cylinder to half the height of the raycast, assign its position to the raycast origin, and then attempt to use Quaternion.Lookat(directionVector) to rotate it toward the spot. It isn't working. Is there an easier way to do this?
     
  2. WilkinsMicawber

    WilkinsMicawber

    Joined:
    Jan 22, 2016
    Posts:
    27
    Changing it from a cylinder to a cube, with the long side along the z axis worked.
     
  3. greg-harding

    greg-harding

    Joined:
    Apr 11, 2013
    Posts:
    381
    If you are using the default cylinder object as is without nesting under other gameobjects or making other prefabs etc. then first take note of where the origin of the transform of the cylinder is and how high it is. By default the transform is in the centre of the cylinder mesh and it is 2 units high.

    So, if you want to orient it along a raycast then you need to position it at the centre point of the raycast, rotate it, and scale it in the y dimension correctly (raycast length / 2, as you're already doing). If you position it at either end of the raycast then when you rotate and scale the cylinder half of it will stretch out in the opposite direction you intended. After positioning it in the right place you should be able to get it rotated correctly.
     
  4. WilkinsMicawber

    WilkinsMicawber

    Joined:
    Jan 22, 2016
    Posts:
    27
    Thank you. The configurable joint connecting the cylinder to the wall holds up, but the one connecting the player to the other end of the cylinder is breaking immediately. Any idea why a configurable joint would break instantaneously? I tried increasing the solver iterations to 255 with no luck.
     
    Last edited: Dec 18, 2016
  5. greg-harding

    greg-harding

    Joined:
    Apr 11, 2013
    Posts:
    381
    If you're creating a physics collider and joints based on the raycast and they're breaking then perhaps add some length tolerance at either end (assuming everything's allowed to collide as you're suggesting). Start by adding a high tolerance that reduces the length of the new cylinder so there's no break, then reduce it to find where it breaks.
     
  6. WilkinsMicawber

    WilkinsMicawber

    Joined:
    Jan 22, 2016
    Posts:
    27
    The problem ended up being my code unintentionally breaking the joint in a portion I forgot existed. Everything is working well now!