OnPointerEnter和OnPointerExit未被觸發

好的,基本上我遇到的問題是,出于某種原因,游戲對象干擾了OnPointerEnter函數。我很確定OnPointReferer只檢測UI。這就是為什么當我看到一個特定的游戲對象在這種情況下,PlayerLogic游戲對象(你可以在屏幕截圖中看到)出于某種原因干擾UI元素的檢測時,我感到非常困惑。我之所以相信它是這個特定的游戲對象,是因為一旦我做了PlayerLogic.SetActive(false);OnPointerEnter又開始工作了,我也確信它不是PlayerLogic的任何一個孩子,因為我已經嘗試過專門關閉它們,但它仍然不起作用。

PlayerLogic對象的檢查器

Hierarchy

我用來測試指針連接器的代碼

經過一些測試,我意識到具體問題在于PlayerLogic游戲對象上的玩家腳本?,F在讓我困惑的是,一旦我關閉了播放器組件OnPointer,它就不起作用了,但是如果我完全從PlayerLogic游戲對象OnPointerEnter中刪除播放器組件,它就會起作用。

using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System.Collections;
using System.Collections.Generic;

public class Player : MonoBehaviour, TakeDamage {

    [SerializeField] private Animator playerAnimator;

    [SerializeField] private Transform mainCameraTransform;
    private bool isRunning = false; 

    [SerializeField] private CharacterController controller;
    public float speed = 10f;
    [SerializeField] private float jumpForce = 3f;
    [SerializeField] private float gravity = -10000.81f;
    Vector3 velocity;
    Vector3 desiredMoveDirection;

    private float dashSpeed = 30f;

    private float mouseX;
    private float mouseY;
    [SerializeField]
    private Transform Target;
    [SerializeField]
    private Transform player;
    
    private float turnSmoothVelocity;    

    private float time = 0f;
    public bool playerIsAttacking = false;

    [SerializeField] private Slider playerHealth, playerMana;
    [SerializeField] private TextMeshProUGUI healthText, manaText; 

    private Vector3 originalSpawnPos;
    private bool playerIsDead = false;

    [SerializeField] private LayerMask enemyLayerMask;
    [SerializeField] private Transform playerLook;

    private ShowHPBar obj;
    private bool HPBarShown = false;
    private bool unshowingHPBar = false;
    public bool lookingAtEnemy = false;
    public RaycastHit hit;

    [SerializeField] private Canvas abilityCanvas;
    [SerializeField] private Slider CD1;
    [SerializeField] private Slider CD2;
    [SerializeField] private Slider CD3;
    public List<Ability> currentlyEquippedAbilites = new List<Ability>();
    public List<string> abilityTexts = new List<string>();
    public float[] abilityCooldowns = new float[3]; 
    private float manaRegenTime;
    //public List<Image> abilityImages = new List<Image>();

    private void Awake() {
        Cursor.visible = false;
        Cursor.lockState = CursorLockMode.Locked;
    }

    private void Start() {
        playerHealth.onValueChanged.AddListener(delegate {OnValueChangedHealth(); });
        playerMana.onValueChanged.AddListener(delegate {OnValueChangedMana(); });
        originalSpawnPos = transform.position;
    }

    private void Update() {
        if (!playerIsDead) {
            PlayerMovementAndRotation();
        }
        PlayerDash();
        PlayerRun();
        PlayerSeeEnemyHealth();
        PlayerActivateAbility();

        if (manaRegenTime > 0.5f) {
            playerMana.value += playerMana.maxValue/100; 
            manaRegenTime = 0;
        }
        playerLook.rotation = mainCameraTransform.rotation;
        time += Time.deltaTime;
        manaRegenTime += Time.deltaTime;

        #region Ability Cooldowns
        if (currentlyEquippedAbilites.Count > 0) {
            if (currentlyEquippedAbilites[0].cooldown <= abilityCooldowns[0])
            {
                currentlyEquippedAbilites[0].isOnCooldown = false;
                abilityCooldowns[0] = 0;
                CD1.value = 0;
            }
            else if (currentlyEquippedAbilites[0].isOnCooldown) { 
                abilityCooldowns[0] += Time.deltaTime; 
                CD1.value = currentlyEquippedAbilites[0].cooldown - abilityCooldowns[0];
            }
        }

        if (currentlyEquippedAbilites.Count > 1) {
            if (currentlyEquippedAbilites[1].cooldown <= abilityCooldowns[1])
            {
                currentlyEquippedAbilites[1].isOnCooldown = false;
                abilityCooldowns[1] = 0;
                CD2.value = 0;
            }
            else if (currentlyEquippedAbilites[1].isOnCooldown) { 
                abilityCooldowns[1] += Time.deltaTime; 
                CD2.value = currentlyEquippedAbilites[1].cooldown - abilityCooldowns[1];
            }
        }

        if (currentlyEquippedAbilites.Count > 2) {
            if (currentlyEquippedAbilites[2].cooldown <= abilityCooldowns[2])
            {
                currentlyEquippedAbilites[2].isOnCooldown = false;
                abilityCooldowns[2] = 0;
                CD3.value = 0;
            }
            else if (currentlyEquippedAbilites[2].isOnCooldown) { 
                abilityCooldowns[2] += Time.deltaTime; 
                CD3.value = currentlyEquippedAbilites[2].cooldown - abilityCooldowns[2];
            }
        }
        #endregion
    }

    private void PlayerRun() {
        if (Input.GetKey(KeybindsScript.RunKey) && (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0)) {
            playerAnimator.SetInteger("isRunning", 1);
            playerAnimator.SetInteger("isIdle", 0);
            playerAnimator.SetInteger("isWalking", 0);
            speed = 15f;
        }
        else if (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0) {
            playerAnimator.SetInteger("isWalking", 1);
            playerAnimator.SetInteger("isIdle", 0);
            playerAnimator.SetInteger("isRunning", 0);
            speed = 10f;
        }
        else {
            playerAnimator.SetInteger("isRunning", 0);
            playerAnimator.SetInteger("isWalking", 0);
            playerAnimator.SetInteger("isIdle", 1);
            speed = 10f;
        }
    }

    private void PlayerMovementAndRotation() {
        bool isGrounded = controller.isGrounded;

        if (isGrounded && velocity.y < 0) {
            velocity.y = -2f;
        }
        float horizontal = Input.GetAxisRaw("Horizontal");
        float vertical = Input.GetAxisRaw("Vertical");
        Vector3 moveDir = (transform.right*horizontal+transform.forward*vertical).normalized;

        controller.Move(moveDir*Time.deltaTime*speed);
        transform.eulerAngles = new Vector3(0f, mainCameraTransform.eulerAngles.y, 0f);
        

        if (Input.GetKeyDown(KeybindsScript.JumpKey) && isGrounded) {
            velocity.y = Mathf.Sqrt(jumpForce * -2f * gravity);
        }

        velocity.y += gravity * Time.deltaTime;

        controller.Move(velocity * Time.deltaTime);
    }

    private void PlayerDash() {
        if (Input.GetKeyDown(KeybindsScript.DashKeybind) && isRunning == false) {
            if (Input.GetKey(KeybindsScript.MovementKeyBackward)) {
                StartCoroutine(PlayerDashTiming(2));
            }
            else if (Input.GetKey(KeybindsScript.MovementKeyRight)) {
                StartCoroutine(PlayerDashTiming(3));
            }
            else if (Input.GetKey(KeybindsScript.MovementKeyLeft)) {
                StartCoroutine(PlayerDashTiming(4));
            }
            else {
                StartCoroutine(PlayerDashTiming(1));
            }
        }
    }

    private void PlayerSeeEnemyHealth() {
        if (Physics.Raycast(playerLook.position, playerLook.forward, out hit, 1000f, enemyLayerMask)) {
            obj = hit.transform.gameObject.GetComponent<ShowHPBar>();
            if (obj != null && !HPBarShown && !unshowingHPBar) {obj.ShowHPBarFunction(); HPBarShown = true;}
            lookingAtEnemy = true;
        }
        else {
            if (obj != null && HPBarShown) {StartCoroutine(UnShowHPBar(obj)); HPBarShown = false;}
        }
    }

    public void PlayerEquipAbility(Ability ability, int place) {
        if (currentlyEquippedAbilites.Count < place) {currentlyEquippedAbilites.Add(ability);}
        else {currentlyEquippedAbilites[place-1] = ability;}

        //if (abilityImages.Count < place) {abilityImages.Add(ability.icon);}
        //else {abilityImages.Add(ability.icon);}
        if (abilityTexts.Count < place) {abilityTexts.Add(ability.name);}
        else {abilityTexts[place-1] = ability.name;}
        for (int i=0;i < abilityTexts.Count;++i) {
            abilityCanvas.transform.GetChild(i).GetChild(0).GetComponent<TextMeshProUGUI>().text = abilityTexts[i];
            abilityCanvas.transform.GetChild(i).GetChild(1).GetComponent<Slider>().maxValue = ability.cooldown;
        }
    }

    private void PlayerActivateAbility() { 
        if (Input.GetKeyDown(KeyCode.Alpha1)) {
            if (currentlyEquippedAbilites[0] != null) {
                if (currentlyEquippedAbilites[0].manaCost <= playerMana.value && !currentlyEquippedAbilites[0].isOnCooldown) {
                    ParticleEffect pe = currentlyEquippedAbilites[0].script.gameObject.GetComponent<ParticleEffect>();
                    playerMana.value -= currentlyEquippedAbilites[0].manaCost;
                    currentlyEquippedAbilites[0].isOnCooldown = true;
                    if (pe != null) {pe.PlayAnimation();}
                }
            }
        }
    }


    public void OnValueChangedHealth() {
        healthText.text = playerHealth.value + "/" + PlayerStats.HealthPoints;
    }

    public void OnValueChangedMana() {
        manaText.text = playerMana.value + "/" + PlayerStats.ManaAmount;
    }

    public void TakeDamageFunction(float damage) {
        playerHealth.value -= damage;
        if (playerHealth.value <= 0) {
            StartCoroutine(PlayerDeath());
        }
    }


    IEnumerator PlayerDashTiming(int x) {
        isRunning = true;
        float time = 0f;
        Vector3 savedVector = Vector3.zero;
        switch (x) {
            case 1: 
            savedVector = transform.forward;
            break;
            case 2:
            savedVector = -transform.forward;
            break;
            case 3:
            savedVector = transform.right;
            break;
            case 4:
            savedVector = -transform.right;
            break;
        }
        while(time < .3f)
        {
            time += Time.deltaTime;
            controller.Move(savedVector * dashSpeed * Time.deltaTime);
            yield return null; 
        }
        yield return new WaitForSeconds(1.5f);
        isRunning = false;
    }

    IEnumerator PlayerDeath() {
        //Respawn
        playerIsDead = true;
        playerAnimator.enabled = false;
        yield return new WaitForSeconds(1f);
        playerHealth.value = 100;
        transform.position = originalSpawnPos;
        yield return new WaitForSeconds(0.1f);
        playerIsDead = false;
        playerAnimator.enabled = true;
    }

    IEnumerator UnShowHPBar(ShowHPBar obj) {
        unshowingHPBar = true;
        yield return new WaitForSeconds(1.5f);
        obj.ShowHPBarFunction();
        unshowingHPBar = false;
    }
}

這是Player.cs腳本。

? 最佳回答:

我很確定OnPointReferer只檢測UI。

No.

它在UI上“開箱即用”,因為默認情況下,每個Canvas都包含一個GraphicsRaycaster組件,然后由EventSystem使用和處理。

對于non-UI3D對象,您必須確保

  • 您的對象具有三維碰撞器
  • 碰撞器與IPointerXY接口位于同一對象上
  • Camera上有一個PhysicsRayster組件

對于non-UI2D對象,它們非常相似

  • 對象具有二維碰撞器
  • 碰撞器與IPointerXY接口位于同一對象上
  • Camera有一個Physics2DRaycaster組件

請注意,任何其他碰撞器或一般光線投射阻止對象都可能位于輸入和目標對象之間,這也會阻止在對象上觸發OnPointerXY。

The CharacterController

它只是一個膠囊狀的對撞機,可以告訴它從腳本向某個方向移動。

這可能會阻塞輸入。


現在使用Player代碼:

You do

Cursor.lockState = CursorLockMode.Locked;

Awake中,即使您在之后關閉它,它也已經生效。

主站蜘蛛池模板: 中字幕一区二区三区乱码| 中文字幕乱码一区二区免费| 无码av免费一区二区三区| 秋霞日韩一区二区三区在线观看| 午夜视频在线观看一区| 无码av免费毛片一区二区| 久久久无码一区二区三区| 激情综合一区二区三区| 国产另类ts人妖一区二区三区| 国产在线一区视频| 人妖在线精品一区二区三区| 国产一区二区三区高清视频| 中文乱码精品一区二区三区| 亚洲成a人一区二区三区| 无码人妻精品一区二区三区99仓本| 福利视频一区二区牛牛 | 欧洲精品一区二区三区在线观看| 日本韩国一区二区三区| 亚洲码一区二区三区| 无码国产精品一区二区免费式影视| 久久精品一区二区东京热| 波多野结衣中文字幕一区| 国产AV国片精品一区二区| 精品一区二区三区在线观看l | 精彩视频一区二区三区| 亚洲国产AV一区二区三区四区 | 亚洲熟女综合一区二区三区| 麻豆高清免费国产一区| 色一情一乱一伦一区二区三欧美 | 国产精品一区视频| 亚洲AV香蕉一区区二区三区| 精品国产一区二区三区香蕉事| 成人免费视频一区二区| 精品永久久福利一区二区| 日本精品一区二区三本中文| 国产无套精品一区二区| 日日摸夜夜添一区| 欧洲精品一区二区三区在线观看| 精品一区二区三区四区在线| 亚欧免费视频一区二区三区| 色屁屁一区二区三区视频国产|