在我的 Unity 游戏中,Collider 不会在我的播放器上注册

问题描述 投票:0回答:1

我正在和我的朋友做游戏,我试图让玩家向敌人发起攻击,但是当敌人进入对撞机时,对撞机不会注册。我已将其设置为玩家的对撞机相当大。当敌人进入时,它应该将布尔值设置为 true 并让你攻击。我已经链接了 2 个脚本,一个叫做 FPmovement

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

public class FPmovement : MonoBehaviour
{
    public AudioSource source;
    public AudioClip walking;
   public float speed = 5f;
    public float jumpSpeed = 5f;
    public float gravity = -9.81f;
    public float crouchHeight = 1f;
    public float lookSensitivity = 2f;
    public float superDashCost = 35.0f;
    private float _originalHeight;
    public bool j = true;
    public GameManager gameManager;
    public bool enemyIN = false;
    public EnemyDamage enemyD;

    private CharacterController _controller;
    public Transform _camera;

    void Start()
    {
        _controller = GetComponent<CharacterController>();
        _originalHeight = _controller.height;
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;


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

    

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Mouse0) && enemyIN == true)
        {
            // Attack Animation
            enemyD.takeDMG(Random.Range(12, 28));
            
        }      
        if (Input.GetKeyDown(KeyCode.V))
        {
            gameManager.AddTree(25);
        }
        if (Input.GetKeyDown(KeyCode.B))
        {
            if (gameManager.tree >= superDashCost)
            {
                Debug.Log("Money well Spent");
                gameManager.RemoveTree(35);
            }
            else
            {
                Debug.Log("Not enough Tree");
            }
        }



        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");
        
        Vector3 move = transform.right * x + transform.forward * z;

        _controller.Move(move * speed * Time.deltaTime);
            

        if (_controller.isGrounded && Input.GetButtonDown("Jump"))
        {
            _controller.Move(Vector3.up * jumpSpeed * Time.deltaTime);
        }


        _controller.Move(Vector3.up * gravity * Time.deltaTime);



        if (Input.GetKey(KeyCode.LeftControl))
        {
            _controller.height = crouchHeight;
            speed = 2.5f;
        }
        else
        {
            _controller.height = _originalHeight;
            speed = 5;
        }
        
        if (Input.GetKey(KeyCode.LeftShift)) 
        {
            speed = 7.5f;
        }
        else 
        {
            source.clip = walking;
            source.loop = true;
            source.Play();
            speed = 5;
        }

        // first-person look
        float mouseX = Input.GetAxis("Mouse X") * lookSensitivity;
        mouseX = Mathf.Clamp(mouseX, -20f, 70f);
        float mouseY = Input.GetAxis("Mouse Y") * lookSensitivity;

        transform.Rotate(0f, mouseX, 0f);

        float newXRotation = _camera.localEulerAngles.x - mouseY;
        if (newXRotation > 180f)
        {
            newXRotation -= 360f;
        }

        newXRotation = Mathf.Clamp(newXRotation, -20f, 80f);

        _camera.localEulerAngles = new Vector3(
            newXRotation,
            0f,
            0f
        );
    }

    public void OnCollisionEnter(Collision collision)
    {
        Debug.Log("Collison2");
        if (collision.gameObject.CompareTag("Enemy"))
        {
            Debug.Log("Collison with Enemy");
            enemyIN = true;
        }
    }

    private void OnCollisionExit(Collision collision)
    {
        if (collision.gameObject.CompareTag("Enemy"))
        {
            Debug.Log("Collison with Enemy Leave");
            enemyIN = false;
        }
    }

    
}

,FPmovement是玩家的脚本。第二个脚本是 EnemyDamage

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


public class EnemyDamage : MonoBehaviour
{

    public int EnemyHealth = 100;
    private Health PlayerHealth;

    private GameObject damVFX;

    private Image damVFXimg;

    private GameObject player;

    private Animator playerAnim;

    private bool attack;

    private GameObject AttackHand;
    // Start is called before the first frame update
    void Start()
    {
        damVFX = GameObject.FindWithTag("damage");
        damVFXimg = damVFX.GetComponent<Image>();
        player = GameObject.FindWithTag("Player");
        PlayerHealth = player.GetComponent<Health>();
        playerAnim = player.GetComponent<Animator>();
        attack = playerAnim.GetBool("isWeakAttack");
        AttackHand = GameObject.FindWithTag("AttackHand");
    }

    // Update is called once per frame
    void Update()
    {
        if (EnemyHealth <= 0) 
        {
            Destroy(gameObject);
        }
    }


    private void OnTriggerEnter(Collider other) {
                if (other.CompareTag("AttackHand")) {
            takeDMG(10);
            Debug.Log("uh");
        }
    }
    private void OnTriggerStay(Collider other) {
        if (other.CompareTag("Player")) {
            Damage(1);
            effectOn();
        }

    }

    private void OnTriggerExit(Collider other) {
        Invoke("effectOff", 2f);
    }
    public void Damage(int damage) 
    {
        PlayerHealth.health -= damage;
    }
    
    public void takeDMG(int Edamage) 
    {
        EnemyHealth -= Edamage;
    }
    public void effectOn() {
        damVFXimg.enabled = true;
    }

    public void effectOff() {
        damVFXimg.enabled = false;
    }
}

script 包含我在攻击时调用的方法。还有2张图是玩家和敌人的组件[Player] [Enemy] [Enemy]

我试过添加和删除刚体,删除和添加对撞机,重写了一些脚本。当它进入对撞机时应该在日志中写一些东西但是它没有发生

c# unity3d visual-studio-2022
1个回答
0
投票

如果使用 OnCollisionEnter() 你不能将对撞机作为触发器,如果你想让它成为触发器你应该使用 OnTriggerEnter()

希望有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.