http://www.gamasutra.com/blogs/AdamWinkels/20140220/211306/DevLog_7_Learning_How_to_Walk.php
Following the link above I have been able to get my character to move and rotate very smoothly over angled surfaces up-to 80 degrees. Anything over that and it begins to clip and wobble when traveling down the surface. The spiders rotation only reaches half of the desired angle. So at 85 degrees it only rotates to 42.5. Going up is a mess, but I have not implemented the fix for that from the link above, so I am less worried about that. Any suggestions?
private float RayDuration = 0;
private float RayAngle = 5f;
private float RayLength;
Vector2 MovementDirection;
float OffSetY = 5;
float OffSetX = .5f;
int GroundMask = 1 << 8;
void Start()
{
SpiderRect = GetComponent<RectTransform>();
RayLength = SpiderRect.rect.height;
}
private void TouchHeld(CustomEvent customEvent)
{
if (Input.mousePosition.x > 400)
{
MovementDirection = transform.right;
}
else
{
MovementDirection = -transform.right;
}
Vector2 transformUp = transform.up;
Vector2 position = transform.position;
Vector2 rightRayPosition = new Vector2(transform.position.x + OffSetX, transform.position.y + OffSetY);
Quaternion rightOffSetAngle = Quaternion.AngleAxis(-RayAngle, new Vector3(0, 0, 1));
Vector2 rightRayAngle = rightOffSetAngle * Vector2.down ;
Vector2 leftRayPosition = new Vector2(transform.position.x - OffSetX, transform.position.y + OffSetY);
Quaternion leftOffSetAngle = Quaternion.AngleAxis(RayAngle, new Vector3(0, 0, 1));
Vector2 leftRayAngle = leftOffSetAngle * Vector2.down;
RaycastHit2D rightHit = Physics2D.Raycast(rightRayPosition, rightRayAngle, RayLength, GroundMask);
RaycastHit2D leftHit = Physics2D.Raycast(leftRayPosition, leftRayAngle, RayLength, GroundMask);
#region move
if (rightHit && leftHit)
{
Debug.DrawRay(rightRayPosition, rightRayAngle, Color.red, RayDuration);
Debug.DrawRay(rightRayPosition, rightHit.normal, Color.green, RayDuration);
Debug.DrawRay(leftRayPosition, leftRayAngle, Color.yellow, RayDuration);
Debug.DrawRay(leftRayPosition, leftHit.normal, Color.magenta, RayDuration);
Vector2 averagePoint = (leftHit.point + rightHit.point) / 2;
Debug.DrawRay(averagePoint, -transform.up, Color.cyan, RayDuration);
Vector2 averageNormal = (leftHit.normal + rightHit.normal) / 2;
Debug.DrawRay(averagePoint, averageNormal, Color.cyan, RayDuration);
Quaternion targetRotation = Quaternion.FromToRotation(Vector3.up, averageNormal);
Quaternion finalRotation = Quaternion.RotateTowards(transform.rotation, targetRotation, 10);
transform.rotation = Quaternion.Euler(0, 0, finalRotation.eulerAngles.z);
transform.position = Vector2.MoveTowards(transform.position, averagePoint + MovementDirection, .15f);
}
else
{
Debug.Log("What");
}