我有一個(gè)玩家精靈,可以在點(diǎn)擊屏幕上的任何位置移動(dòng)。如果單擊播放器精靈,我正在嘗試彈出播放器信息面板。但不幸的是,我只讓玩家移動(dòng)了幾個(gè)像素。我有一個(gè)Box Collider 2d添加到 sprite 和一個(gè)事件觸發(fā)器設(shè)置為指針單擊以運(yùn)行該方法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); }
統(tǒng)一使可移動(dòng)的精靈可點(diǎn)擊
幕布斯7119047
2022-11-13 13:37:54