Skip to main content
LookRotation correction
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

Instead of breaking into two triangles, where you have to make a choice of which diagonal to split on, I'd recommend using a form that does not favour one orientation over the other:

// Average the "right" directions at the front and back of the tank. var rightward = topRight + bottomRight - topLeft - bottomLeft; // Average the "forward" directions at the left and right sides of the tank. var forward = topRight + topLeft - bottomLeft - bottomRight; // Forward x Right = Up _slopeVector = Vector3.Cross(forward, rightward).normalized; 

You're also using the wrong LookRotation: your forward vector is overriding the pitch rotation that your _slopeVector wants to apply, limiting it to only affect the roll. Try this instead:

var look = Quaternion.LookRotatiin(_slopeVector, -toTarget); var targetRotation = look * Quaternion.Euler(90, 0, 0); 

Instead of breaking into two triangles, where you have to make a choice of which diagonal to split on, I'd recommend using a form that does not favour one orientation over the other:

// Average the "right" directions at the front and back of the tank. var rightward = topRight + bottomRight - topLeft - bottomLeft; // Average the "forward" directions at the left and right sides of the tank. var forward = topRight + topLeft - bottomLeft - bottomRight; // Forward x Right = Up _slopeVector = Vector3.Cross(forward, rightward).normalized; 

Instead of breaking into two triangles, where you have to make a choice of which diagonal to split on, I'd recommend using a form that does not favour one orientation over the other:

// Average the "right" directions at the front and back of the tank. var rightward = topRight + bottomRight - topLeft - bottomLeft; // Average the "forward" directions at the left and right sides of the tank. var forward = topRight + topLeft - bottomLeft - bottomRight; // Forward x Right = Up _slopeVector = Vector3.Cross(forward, rightward).normalized; 

You're also using the wrong LookRotation: your forward vector is overriding the pitch rotation that your _slopeVector wants to apply, limiting it to only affect the roll. Try this instead:

var look = Quaternion.LookRotatiin(_slopeVector, -toTarget); var targetRotation = look * Quaternion.Euler(90, 0, 0); 
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

Instead of breaking into two triangles, where you have to make a choice of which diagonal to split on, I'd recommend using a form that does not favour one orientation over the other:

// Average the "right" directions at the front and back of the tank. var rightward = topRight + bottomRight - topLeft - bottomLeft; // Average the "forward" directions at the left and right sides of the tank. var forward = topRight + topLeft - bottomLeft - bottomRight; // Forward x Right = Up _slopeVector = Vector3.Cross(forward, rightward).normalized;