Guest User

Untitled

a guest
Jul 16th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. // first need to determine if the character that is moving forward collides with
  2. // an object in the way
  3. // the character is moving along 'path', which is a series of waypoints
  4. dir = transform.forward;
  5. gizmoBoxHit = Physics.BoxCast(transform.position, new Vector3(1.75f, 1.75f, 0.01f), dir, out boxHit, transform.rotation, 1.75f );
  6. if ( gizmoBoxHit )
  7. {
  8. // here we've correctly determined it hit an object by doing a
  9. // boxcast forward that is exactly as wide and forward (i think)
  10. // as the character's collider. I believe its correct because
  11. // as you can see from the images the character has stopped at the
  12. // correct point, just in contact with a cylinder based on the
  13. // boxcast.
  14.  
  15. movementPaused = true;
  16. if (!stopTrying)
  17. {
  18. bool foundAround = false; // flag should turn true when way around is found
  19. float rotateLimit = 180f; // most rotation i want to test
  20. float rotationInterval = 3f; // increments of rotation tested
  21. float rotationAdded = 0f; // current cumulative rotation added
  22. Vector3 currentRotation = transform.rotation.eulerAngles;
  23. Vector3 facingDirection = transform.forward.normalized;
  24. float collidedWidth = boxHit.transform.GetComponent<BasicProperties>().colliderRadius * 2f;
  25. // testLength is how far away I want to point to be in the
  26. // correct direction, the width of the cylinder plus radius of character
  27. testLength = basicProperties.colliderRadius + collidedWidth;
  28. euler = Vector3.zero;
  29. // loop a cumulative building rotation until you find the
  30. // correct rotation
  31. while (!foundAround && rotationAdded < rotateLimit)
  32. {
  33. rotationAdded += rotationInterval;
  34. currentRotation.y += rotationInterval;
  35. euler = new Vector3(0, rotationAdded, 0);
  36. LayerMask wtfLayerMask = (1 << LayerMask.NameToLayer("Champions")) | (1 << LayerMask.NameToLayer("NPCs"));
  37. // HERE LIES THE PROBLEM:
  38. // BoxCast result should be true until a clear path is found, at which point it should become false. But its becoming false too early,
  39. // with too small of a rotationAdded
  40. newPathFoundHit = Physics.BoxCast(transform.position, new Vector3(1.75f, 1.7f, 0.01f), facingDirection, out newPathBoxHit, Quaternion.Euler(euler), testLength, wtfLayerMask);
  41. if (!newPathFoundHit)
  42. {
  43. foundAround = true;
  44. }
  45. }
  46. // based on rotation determined, figure out the point
  47. Vector3 vector = transform.forward * testLength;
  48. vector = Quaternion.AngleAxis(rotationAdded, Vector3.up) * vector;
  49. Vector3 globalVector = transform.TransformPoint(vector);
  50.  
  51. // draw a cyan sphere to see where the resulint point is.
  52. GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
  53. sphere.GetComponent<Renderer>().material.color = Color.cyan;
  54. sphere.transform.position = globalVector;
  55. stopTrying = true;
  56. }
  57.  
  58. }
  59. else
  60. {
  61. movementPaused = false;
  62. }
Add Comment
Please, Sign In to add comment