为了账号安全,请及时绑定邮箱和手机立即绑定

统一使可移动的精灵可点击

统一使可移动的精灵可点击

C#
幕布斯7119047 2022-11-13 13:37:54
我有一个玩家精灵,可以在点击屏幕上的任何位置移动。如果单击播放器精灵,我正在尝试弹出播放器信息面板。但不幸的是,我只让玩家移动了几个像素。我有一个Box Collider 2d添加到 sprite 和一个事件触发器设置为指针单击以运行该方法ShowPlayerInfoPanelusing 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);    }
查看完整描述

1 回答

?
慕哥6287543

TA贡献1831条经验 获得超10个赞

使用游戏对象上的对撞机,您只需使用OnMouseDown来检测对象何时被单击。


void OnMouseDown()

{

    ShowPlayerInfoPanel();

}


查看完整回答
反对 回复 2022-11-13
  • 1 回答
  • 0 关注
  • 80 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号