Advertisement
IsntCo

Untitled

Sep 6th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5.  
  6. namespace DunGen.NavigationDemo
  7. {
  8. public sealed class ClickToMove : MonoBehaviour
  9. {
  10. public Camera Camera;
  11. public NavMeshAgent Agent;
  12. public Animator Animator;
  13.  
  14. private bool isTraversingLink;
  15. private float traversalSpeed;
  16. private Vector3 previousPosition;
  17.  
  18.  
  19. private void Update()
  20. {
  21.  
  22.  
  23.  
  24.  
  25. if (Agent.isOnOffMeshLink)
  26. {
  27. if (!isTraversingLink)
  28. {
  29. // The agent seems to snap to the entry point on the link
  30. // We reset the agent to its last position to smooth out the transition
  31. Agent.transform.position = previousPosition;
  32.  
  33. // Use our current speed for the duration of the link
  34. traversalSpeed = CalculateCurrentVelocity().magnitude;
  35.  
  36. // Just to be safe, ensure we're using a valid speed
  37. if (traversalSpeed <= 0.0f)
  38. traversalSpeed = Agent.speed;
  39.  
  40. isTraversingLink = true;
  41. }
  42.  
  43. TraverseOffMeshLink();
  44. }
  45.  
  46. UpdateAnimator();
  47. previousPosition = Agent.transform.position;
  48.  
  49. }
  50.  
  51. private Vector3 CalculateCurrentVelocity()
  52. {
  53. return (Agent.transform.position - previousPosition) / Time.deltaTime;
  54.  
  55.  
  56. }
  57.  
  58. private void TraverseOffMeshLink()
  59. {
  60. var link = Agent.currentOffMeshLinkData;
  61. Vector3 destination = link.endPos + Vector3.up * Agent.baseOffset;
  62. Vector3 toDestination = destination - Agent.transform.position;
  63.  
  64. float distanceToDestination = toDestination.magnitude;
  65.  
  66. // If we're close enough, consider the traversal complete
  67. if (distanceToDestination <= 0.01f)
  68. {
  69. Agent.updateRotation = true;
  70. Agent.CompleteOffMeshLink();
  71. isTraversingLink = false;
  72. }
  73. else
  74. {
  75. Vector3 targetForwardDirection = toDestination.normalized;
  76. Quaternion targetRotation = Quaternion.LookRotation(targetForwardDirection, Vector3.up);
  77.  
  78. float maxPositionDelta = Agent.speed * Time.deltaTime;
  79. float maxRotationDelta = Agent.angularSpeed * Time.deltaTime;
  80.  
  81. Agent.updateRotation = false; // We'll update the agent's rotation manually while traversing the link
  82. Agent.transform.position = Vector3.MoveTowards(Agent.transform.position, destination, maxPositionDelta);
  83. Agent.transform.rotation = Quaternion.RotateTowards(Agent.transform.rotation, targetRotation, maxRotationDelta);
  84.  
  85. }
  86. }
  87.  
  88. private void UpdateAnimator()
  89. {
  90. // Update animator
  91. if (Animator == null)
  92. return;
  93.  
  94.  
  95.  
  96.  
  97. Vector3 currentPosition = Agent.transform.position;
  98. Vector3 velocity = CalculateCurrentVelocity();
  99. float speed = velocity.magnitude;
  100.  
  101. Animator.SetFloat("velx", velocity.x);
  102. Animator.SetFloat("vely", velocity.z);
  103.  
  104.  
  105. Animator.SetBool("move", speed > 0f);
  106. Animator.SetFloat("Speed", speed);
  107. }
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement