简体   繁体   English

统一使可移动精灵可点击

[英]unity make moveable sprite clickable

I have a player sprite that moves anywhere on the screen clicked. 我有一个可以在屏幕上任意位置单击的播放器精灵。 I am trying to make a player info panel popup if the player sprite is clicked. 如果尝试单击播放器精灵,则尝试弹出播放器信息面板。

But unfortunately I only get the player moving a couple of pixels. 但不幸的是,我只能让播放器移动几个像素。 I have a Box Collider 2d added to the sprite and an event trigger set to Pointer Click to run the method ShowPlayerInfoPanel 我将Box Collider 2d添加到精灵中,并将事件触发器设置为Pointer单击以运行方法ShowPlayerInfoPanel

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);
    }
}

With a collider on your gameObject you can just use OnMouseDown to detect when the object is clicked. 在您的gameObject上使用对撞机时,您可以仅使用OnMouseDown来检测何时单击该对象。

void OnMouseDown()
{
    ShowPlayerInfoPanel();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM