I have a hoverboard that adjusts it’s angle depending on the terrain below it. Every so often the board jerks to a strange angle for a moment.
My only guess on why is that the raycasts are hitting joining faces of the terrain ,
And the only solution I can see is somehow logging the last change in angle, if it is an extraordinary leap , then don’t transform.rotate
What is happening, and what should I do / be looking at ? thanks.
-
Edit - the problem actually seems to be a Gimbal Lock.
-
SOLVED with a completely different approach . see answers.
Sample script :
// -- X ANGLE calculations
// vectorBoardLeft
if (Physics.Raycast((transform.position + Vector3((widthRaycastX / 2), 0, 0)), Vector3.down, rayHit)) {
vectorBoardLeft = rayHit.point;
}
// vectorBoardRight
if (Physics.Raycast((transform.position - Vector3((widthRaycastX / 2), 0, 0)), Vector3.down, rayHit)) {
vectorBoardRight = rayHit.point;
}
// calculate X angle
differenceYX = vectorBoardLeft.y - vectorBoardRight.y;
angleBoardXtan = differenceYX / widthRaycastX;
angleBoardRotZ = Mathf.Rad2Deg*Mathf.Atan(angleBoardXtan);
// -- rotate board to align to terrain (Z angle is on the X-axis, and vice verca)
transform.rotation = Quaternion.Euler (angleBoardRotX, 0, angleBoardRotZ);
webplayer publish : http://www.alucardj.net16.net/unityquestions/hoverboard%201-1b.html
full script : http://www.alucardj.net16.net/unityquestions/ScriptHoverBoard2.js
- I have just changed the Title of this post as the problem seems to be as described by Kryptos below (thanks for the pointer).
- I have just changed the Title of this post as I have inadvertently stirred controversy , and created a conversation that is off-topic . Very sorry to all involved