Search Unity

Set X scale of object to underlying normal of mesh

Discussion in 'Scripting' started by kai_226, Dec 30, 2017.

  1. kai_226

    kai_226

    Joined:
    Jul 6, 2017
    Posts:
    53
    I want to set the X component of an object's scale equal to the X scale of the normal below the object. Take a look:

    https://prnt.sc/hu76vz

    The white shiny thing should reach from track barrier to the other [it's a cube].

    I thought I should use a raycast to get the underlying normal and then just use the localSize, but does it return the size of the normal [Quad shaped] or the size of the collider normal [triangle] and how can I achieve to have the X size spawning from the left track barrier to the right track barrier?

    The code below leads to the result seen in the picture above:
    Code (CSharp):
    1. Ray rayFlash = new Ray(new Vector3(flashTransform.position.x, flashTransform.position.y + 2f, flashTransform.position.z), Vector3.down);
    2. RaycastHit hitInfoFlash;
    3.  
    4. Transform flashTransform;
    5. if (Physics.Raycast(rayFlash, out hitInfoFlash, 4f, 1 << LayerMask.NameToLayer("Track")))
    6. {
    7.     flashTransform.localScale = new Vector3(hitInfoFlash.normal.normalized.x, flashTransform.localScale.y, flashTransform.localScale.z);
    8. }
    Thanks for every help!
     
  2. kai_226

    kai_226

    Joined:
    Jul 6, 2017
    Posts:
    53
    I solved it by another approach:

    http://prntscr.com/hu7s12

    Instead of getting the ground normal, I check for the distance to the left and the right border with Rays, position the Flash Object in the center and scale it upwards to match the sum of both distances. This requires two Rays instead of one but I hope this is ok since the Rays are only used once at startup.

    Code (CSharp):
    1.  
    2. rayFlashRight = new Ray(flashTransform.position, flashTransform.right);
    3. rayFlashLeft = new Ray(flashTransform.position, -flashTransform.right);
    4.  
    5. if (Physics.Raycast(rayFlashRight, out hitInfoFlashRight, 25f, 1 << LayerMask.NameToLayer("Obstacle")))
    6. {
    7.     if (Physics.Raycast(rayFlashLeft, out hitInfoFlashLeft, 25f, 1 << LayerMask.NameToLayer("Obstacle")))
    8.     {
    9.         flashTransform.localScale += new Vector3(hitInfoFlashRight.distance + hitInfoFlashLeft.distance, flashTransform.localScale.y, flashTransform.localScale.z);
    10.  
    11.         if (hitInfoFlashRight.distance > hitInfoFlashLeft.distance)
    12.         {
    13.             flashTransform.position = flashTransform.position + flashTransform.right * (hitInfoFlashLeft.distance - ((hitInfoFlashLeft.distance + hitInfoFlashRight.distance) / 2f));
    14.         }
    15.         else
    16.         {
    17.             flashTransform.position = flashTransform.position - flashTransform.right * (hitInfoFlashLeft.distance - ((hitInfoFlashLeft.distance + hitInfoFlashRight.distance) / 2f));
    18.         }
    19.     }
    20. }
    21.  
    22.  
     
    Last edited: Dec 31, 2017