Drawing a path - rotation of corner textures

I am letting the user draw out a path on a tile grid by dragging the mouse. For each ‘node’ of the path, I create a plane with a texture on, which is a corner join texture if the path is changing direction there.

The corner sections need to rotate to join up with the preceding and following nodes. Therefore, when I add a new node, I am going back to the previous one to change its texture and rotate it correctly.

Right now I’m using this to rotate the corner sections:

Vector3 turnDirection = (newPosition.position - lastPosition.position).normalized; lastNode.transform.rotation = Quaternion.LookRotation(turnDirection, Vector3.up);

But this only works for left turns in the path. Right turns are not rotated properly. How can I get it to work for both directions?

An example of the problem (ignore my rubbish test textures :smiley: ):

alt text

If your texture is working for a rotate left, the texture would need to be rotated an extra 90 degrees for a right rotation. Or instead of adding an extra 90 degrees, you can use a different texture for right rotations. I can think of a few ways to detect left vs. right rotation:

  • ATan2() will produce an absolute 2D rotation. The 0/360 boundary is a problem, but I believe Mathf.DeltaAngle() handles this correctly.
  • The Math3D script available in the Wiki has a SignedDotProduct() function which can be used to determine left from right.
  • Or if you have an array underlying your tiles, you can determine left and right from the array. In fact you could get all angles by examining the positions in the array of the tiles hit.