统一使可移动精灵可点击

时间:2019-03-25 16:07:18

标签: c# unity3d

我有一个播放器精灵,它可以在屏幕上单击的任何位置移动。如果单击播放器精灵,我会尝试弹出播放器信息面板。

但是不幸的是,我只能让播放器移动几个像素。我将ShowPlayerInfoPanel添加到了精灵,并将事件触发器设置为Pointer单击以运行方法using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class PlayerController : MonoBehaviour { //Player Movement float speed = 2f; Vector2 targetPos; private Rigidbody2D myRigidbody; private Animator myAnim; private static bool playerExists; public static PlayerController instance; public string exitPortal; public bool startMoving; public float smoothTime = 0.3F; private Vector3 velocity = Vector3.zero; //Player Info public string displayName; public string coins; //Player Panel display public GameObject playerInfoPanel; private void Start() { myRigidbody = GetComponent<Rigidbody2D>(); myAnim = GetComponent<Animator>(); if(instance == null){ instance = this; } else { Destroy(gameObject); } DontDestroyOnLoad(transform.gameObject); targetPos = transform.position; } void Update() { if (Input.GetMouseButtonDown(0)) { targetPos = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition); startMoving = true; } if ((Vector2)transform.position != targetPos && startMoving) { Move(); } else { myAnim.SetBool("PlayerMoving", false); } } void Move() { Vector2 oldPos = transform.position; transform.position = Vector2.MoveTowards(transform.position, targetPos, speed * Time.deltaTime); //transform.position = Vector3.SmoothDamp(transform.position, targetPos, ref velocity, smoothTime); Vector2 movement = (Vector2)transform.position - oldPos; myAnim.SetBool("PlayerMoving", true); myAnim.SetFloat("Horizontal", movement.x); myAnim.SetFloat("Vertical", movement.y); } public void ShowPlayerInfoPanel() { Debug.Log("hi"); PlayerInfoPanel playerInfo = playerInfoPanel.GetComponent<PlayerInfoPanel>(); playerInfo.DisplayName.text = displayName; playerInfo.Coins.text = coins; playerInfoPanel.SetActive(true); } }

GO

1 个答案:

答案 0 :(得分:2)

在游戏对象上具有对撞机,您只需使用OnMouseDown即可检测到何时单击了对象。

void OnMouseDown()
{
    ShowPlayerInfoPanel();
}