Guest User

Untitled

a guest
Nov 30th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. void DoDodge(Vector2 dodgeInput)
  2. {
  3. // @TODO: We can actually source the length of the teleport tween from the states that the dodge's "attack" have added - just query for how long the current batch of states will last in total
  4. // Once we have that, we look back at the point we're teleporting from, and add a variable that states how much distance the tween should cover. We tune that variable based on how close we're
  5. // willing to teleport the player near enemies. We then project back toward the original point by that distance, and that's the starting point of the camera tween, which takes the given length.
  6.  
  7. bool shouldDodge = true;
  8.  
  9. // First, we figure out what direction we're dodging in, and roughly how far (and we default to dodging straight backwards)
  10. Vector3 dodgeDir = new Vector3(0.0f, 0.0f, -1.0f);
  11. if (!MathManager.IsNearZero(dodgeInput)) { dodgeDir = new Vector3(dodgeInput.x, 0.0f, dodgeInput.y); }
  12. dodgeDir = gameObject.transform.TransformDirection(dodgeDir);
  13.  
  14. // (throughout, we're bumping by 0.1f to keep the end position from being in contact with anything on landing)
  15. Vector3 targetPos = gameObject.transform.position + (dodgeDir * dodgeDistance) + new Vector3(0.0f, 0.1f, 0.0f);
  16.  
  17. // Next, make sure that isn't inside a wall / adjust accordingly
  18. int layersToCheck = 1; // (for Default layer)
  19. layersToCheck |= 1 << LayerMask.NameToLayer("Environment");
  20. layersToCheck |= 1 << LayerMask.NameToLayer("Player Blocker");
  21.  
  22. // Bump us away from any walls we may have hit
  23. RaycastHit rayHit;
  24. if (Physics.Raycast(gameObject.transform.position, dodgeDir, out rayHit, dodgeDistance, layersToCheck, QueryTriggerInteraction.Collide))
  25. {
  26. targetPos = rayHit.point + (rayHit.normal * (CharacterRef.PhysicalRadius + 0.1f));
  27. }
  28.  
  29. // Also bump us UP, since we could have run into a ramp-type surface, or gone up a stairstep
  30. rayHit = new RaycastHit();
  31. if (shouldDodge && Physics.Raycast(targetPos + new Vector3(0.0f, CharacterRef.PhysicalHeight / 2.0f, 0.0f), Vector3.down, out rayHit, CharacterRef.PhysicalHeight + 0.1f, layersToCheck, QueryTriggerInteraction.Collide))
  32. {
  33. targetPos = rayHit.point + (Vector3.up * CharacterRef.PhysicalHeight * 0.5f) + new Vector3(0.0f, 0.1f, 0.0f);
  34. }
  35.  
  36. // Now do the safety check, to make sure the target is clear. We check out a bit on all sides, but we DON'T extend the bubble down, since that probably clips the floor. The goal is to prevent being
  37. // in contact with anything on landing, as that will invalidate the collision, and let you walk straight through walls et al.
  38. float checkCylRadius = CharacterRef.PhysicalRadius + 0.1f;
  39. Vector3 checkCylEndOffset = new Vector3(0.0f, (CharacterRef.PhysicalHeight / 2.0f) - checkCylRadius, 0.0f);
  40. while (shouldDodge && Physics.CheckCapsule(targetPos - checkCylEndOffset, targetPos + checkCylEndOffset, checkCylRadius, layersToCheck, QueryTriggerInteraction.Collide))
  41. {
  42. targetPos -= dodgeDir * CharacterRef.PhysicalRadius;
  43.  
  44. // If we nudge so far back that we're going the other way, nope, stop, just don't teleport
  45. Vector3 newDodgeDir = (targetPos - gameObject.transform.position).normalized;
  46. if (Vector3.Dot(dodgeDir, newDodgeDir) <= 0.0f) { shouldDodge = false; }
  47. }
  48.  
  49. // Finally, move us to the target position, if we should
  50. if (shouldDodge) { CharacterRef.Teleport(targetPos); }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment