当前位置:   article > 正文

Unity实战篇:MOBA游戏小地图制作(三:实现点击小地图控制人物移动)_moba游戏坐标

moba游戏坐标

前言:在开始之前,建议大家先了解一下Unity的原生点击事件以及UGUI的基础知识(旁白:少废话,谢谢)

我们知道,Game窗口左下角坐标为(0,0),右上角为自己设置分辨率的值,比如我分辨率是1920*1080,那么我Game窗口的右上角就是(1920,1080)

然后我们注意到上篇的UI和实体地图都是正方形(这里可以根据需求自由变化,只需要注意比例即可),那么,我们就可以依据比例来反射小地图上的点到实体地图上。

我们先给多边形碰撞体添上点击事件,注意继承接口IPointerDownHandler。

  1. public void OnPointerDown(PointerEventData eventData)
  2. {
  3. if (!polygonCollider2D.OverlapPoint(eventData.position))
  4. {
  5. return;
  6. }
  7. OrPoint.position = eventData.position;
  8. Debug.Log("点击了地图,点击的点为"+eventData.position.ToString());
  9. Debug.Log("OrPoint的本地坐标为" + OrPoint.localPosition.ToString());
  10. }

比如我点击这个英雄的头像,就会把那个深渊权杖移动到点击位置,说明还是发生在UI(屏幕)层的点击事件,OrPoint.localPosition就是深渊权杖的本地坐标(相当于父物体MapMask位置而言),那么,我们依据等比例换算就能得到物体在实体地图的位置。

 

完整代码

MapResponse.cs

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. public class MapResponse : MonoBehaviour,IPointerDownHandler
  6. {
  7. private PolygonCollider2D polygonCollider2D;
  8. private Transform OrPoint;
  9. private Vector3 mapSize;
  10. private Vector2 miniMapSize;
  11. private void Awake()
  12. {
  13. polygonCollider2D = GetComponent<PolygonCollider2D>();
  14. OrPoint = transform.Find("OrPoint");
  15. mapSize = new Vector3(200f, 0.01f, 200f);//地图实体大小
  16. miniMapSize = new Vector2(GetComponent<RectTransform>().rect.width,GetComponent<RectTransform>().rect.height);//小地图大小
  17. }
  18. public void OnPointerDown(PointerEventData eventData)
  19. {
  20. if (!polygonCollider2D.OverlapPoint(eventData.position))
  21. {
  22. return;
  23. }
  24. OrPoint.position = eventData.position;
  25. Debug.Log("点击了地图,点击的点为"+eventData.position.ToString());
  26. Debug.Log("OrPoint的本地坐标为" + OrPoint.localPosition.ToString());
  27. SetTargetPosition(OrPoint.localPosition);
  28. }
  29. void SetTargetPosition(Vector2 vector2 )
  30. {
  31. Vector3 target = new Vector3(-vector2.x *mapSize.x/miniMapSize.x,0,-vector2.y*mapSize.z/miniMapSize.y);//等比例求点击点在实体地图中的位置
  32. Move.Instance.suc = true;
  33. Move.Instance.MapMove(target);//传递到Move类进行移动
  34. }
  35. }

Move.cs

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Move : MonoBehaviour
  5. {
  6. private RaycastHit hit;
  7. private Ray ray;
  8. public bool suc;
  9. private Animator animator;
  10. private GameObject Hero;
  11. private static Move instance;
  12. public static Move Instance
  13. {
  14. get
  15. {
  16. return instance;
  17. }
  18. set { }
  19. }
  20. private bool startToRun;
  21. float t = 0, w = 0;
  22. private void Awake()
  23. {
  24. instance = this;
  25. Hero = transform.Find("juggment").gameObject;
  26. animator = Hero.GetComponent<Animator>();
  27. }
  28. void FixedUpdate()
  29. {
  30. if (Input.GetMouseButtonDown(1))
  31. {
  32. TargetMove();
  33. }
  34. CheckPerFrame();
  35. }
  36. public void TargetMove()
  37. {
  38. ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  39. suc = Physics.Raycast(ray, out hit);
  40. if (suc)
  41. {
  42. Debug.Log(hit.transform.gameObject.name + ":" + hit.transform.gameObject.layer);
  43. if (t <= 0.01f)
  44. {
  45. startToRun = true;
  46. }
  47. animator.SetFloat("Blend", 1);//防止在奔跑状态转到默认状态途中发出行走指令,导致Blend的值恒定
  48. Hero.transform.localPosition = Vector3.zero;
  49. Hero.transform.localRotation = Quaternion.Euler(0, 0, 0);
  50. transform.LookAt(hit.point);
  51. }
  52. }
  53. public void MapMove(Vector3 target)
  54. {
  55. if (t <= 0.01f)
  56. {
  57. startToRun = true;
  58. }
  59. animator.SetFloat("Blend", 1);//防止在奔跑状态转到默认状态途中发出行走指令,导致Blend的值恒定
  60. Hero.transform.localPosition = Vector3.zero;
  61. Hero.transform.localRotation = Quaternion.Euler(0, 0, 0);
  62. transform.LookAt(target);
  63. hit.point = target;
  64. }
  65. private void CheckPerFrame()
  66. {
  67. if (suc)
  68. {
  69. Debug.DrawLine(transform.position, hit.point);
  70. transform.position = Vector3.MoveTowards(transform.position, hit.point, Time.fixedDeltaTime * 16);
  71. }
  72. if (Vector3.Distance(transform.position, hit.point) <= 0.01f && suc)
  73. {
  74. w = Mathf.Clamp(w -= Time.fixedDeltaTime * 5, -1, 0);//近似到达终点时,开始过渡到默认动画
  75. t = Mathf.InverseLerp(0, 1, 1 + w);//求比例
  76. animator.SetFloat("Blend", t);
  77. if (t <= 0.01f)
  78. {
  79. w = 0;
  80. t = 0;
  81. suc = false;
  82. }
  83. }
  84. if (startToRun)
  85. {
  86. w = Mathf.Clamp(w += Time.fixedDeltaTime * 10, 0, 1);
  87. t = Mathf.InverseLerp(0, 1, w);
  88. animator.SetFloat("Blend", t);
  89. if (t >= 0.99f)
  90. {
  91. w = 0;
  92. t = 1;
  93. startToRun = false;
  94. }
  95. }
  96. }
  97. }

 

效果图

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/872904
推荐阅读
相关标签
  

闽ICP备14008679号