首页 > 解决方案 > Assets\Scripts\Player.cs(69,18):错误 CS0111:类型“Player”已经定义了一个名为“OnTriggerEnter”的成员,具有相同的参数类型

问题描述

我需要在我的脚本中有 2 个 OnTriggerEnter() 因为我有 2 种类型的硬币,Speed Coin 和 Superjump Coin,所以当我触摸它时,它会增加跳跃高度和移动速度,但它会抛出 Assets\Scripts\Player.cs( 69,18): 错误 CS0111: 类型 'Player' 已经定义了一个名为 'OnTriggerEnter' 的成员具有相同的参数类型

对我!

任何帮助都会很棒

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

public class Player : MonoBehaviour
{
    private bool jumpKeyWasPressed;
    private float horizontalInput;
    private Rigidbody rb;
    public Transform GroundCheck;
    public LayerMask playerMask;
    private int superJUmp;
    private int Speed;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            jumpKeyWasPressed = true;
        }

        if (Speed > 0)
        {
            horizontalInput *= 2;
            Speed--;
        }
        horizontalInput = Input.GetAxis("Horizontal");
    }

    private void FixedUpdate()
    {
        rb.velocity = new Vector3(horizontalInput, rb.velocity.y, 0);

        if (Physics.OverlapSphere(GroundCheck.position, 0.1f, playerMask).Length == 0)
        {
            return;
        }

        if (jumpKeyWasPressed)
        {
            float jumpPower = 7f;
            if (superJUmp > 0)
            {
                jumpPower *= 2;
                superJUmp--;
            }
            rb.AddForce(Vector3.up * jumpPower, ForceMode.VelocityChange);
            jumpKeyWasPressed = false;
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.layer == 6)
        {
            Destroy(other.gameObject);
            superJUmp++;
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.layer == 8)
        {
            Destroy(other.gameObject);
            Speed++;
        }
    }
}

标签: c#unity3d

解决方案


您只需要一个 OnTriggerEnter 函数:

 private void OnTriggerEnter(Collider other)
 {
     switch (other.gameObject.layer)
     {
        case 6:
           Destroy(other.gameObject);
           superJUmp++;
           break;
        case 8:
           Destroy(other.gameObject);
           Speed++;
           break;
     }
}

推荐阅读