Advertisement
Guest User

Distance

a guest
Oct 23rd, 2015
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class Distance: MonoBehaviour {
  4.  
  5. Texture2D textureDr;
  6. Texture2D textureAbravanel;
  7.  
  8. void Start() {
  9. int sizeX = 201;
  10. int sizeY = 201;
  11.  
  12. textureDr = new Texture2D(sizeX, sizeY);
  13. textureAbravanel = new Texture2D(sizeX, sizeY);
  14.  
  15. Vector2 point = new Vector2(100, 100);
  16.  
  17. for(int x = 0; x < sizeX; x++) {
  18. for(int y = 0; y < sizeY; y++) {
  19. if(IsPointInRangeOfPoint2D(new Vector2(x, y), point, 10F))
  20. textureDr.SetPixel(x, y, Color.green);
  21. else
  22. textureDr.SetPixel(x, y, Color.red);
  23.  
  24. if(IsPlayerInRangeOfXY(new Vector2(x, y), 10F, point.x, point.y))
  25. textureAbravanel.SetPixel(x, y, Color.green);
  26. else
  27. textureAbravanel.SetPixel(x, y, Color.red);
  28. }
  29. }
  30.  
  31. textureDr.Apply();
  32. textureAbravanel.Apply();
  33. }
  34.  
  35. bool IsPointInRangeOfPoint2D(Vector2 point1, Vector2 point2, float range) {
  36. Vector2 newPoint = new Vector2(point1.x - point2.x, point1.y - point2.y);
  37. float distance = Mathf.Abs(Mathf.Sqrt((newPoint.x * newPoint.x) + (newPoint.y * newPoint.y)));
  38. if(distance <= range)
  39. return true;
  40. return false;
  41. }
  42.  
  43. bool IsPlayerInRangeOfXY(Vector2 point, float radius, float x, float y) {
  44. float[] velha = new float[2] { point.x, point.y };
  45. float[] nova = new float[2] {
  46. (velha[0] - x),
  47. (velha[1] - y)
  48. };
  49. if(((nova[0] < radius) && (nova[1] > -radius)))
  50. return true;
  51. return false;
  52. }
  53.  
  54. void OnGUI() {
  55. GUI.DrawTexture(new Rect(Screen.width * 0.5F - 250, Screen.height * 0.5F - 100, 201, 201), textureDr);
  56. GUI.DrawTexture(new Rect(Screen.width * 0.5F + 50, Screen.height * 0.5F - 100, 201, 201), textureAbravanel);
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement