Eonirr

Untitled

Apr 27th, 2025 (edited)
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. Vector3 direction = transform.forward;
  2. float distance = 1f;
  3. float stopDistance = 0.5f;
  4.  
  5. RaycastHit hit;
  6.  
  7. if (Physics.Raycast(transform.position, direction, out hit, distance))
  8. {
  9. if (hit.collider.CompareTag("Enemy"))
  10. {
  11. float distanceToEnemy = Vector3.Distance(transform.position, hit.collider.transform.position);
  12.  
  13. if (distanceToEnemy < stopDistance)
  14. {
  15. rb.velocity = Vector3.zero;
  16. }
  17. else
  18. {
  19. Vector3 avoidDirection = Vector3.Reflect(direction, hit.normal);
  20. rb.AddForce(avoidDirection * moveForce);
  21. }
  22. }
  23. else
  24. {
  25. rb.AddForce(direction * moveForce);
  26. }
  27. }
  28.  
Advertisement
Add Comment
Please, Sign In to add comment