c# - 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++;
}
}
}
解决方案
您只需要一个 OnTriggerEnter 函数:
private void OnTriggerEnter(Collider other)
{
switch (other.gameObject.layer)
{
case 6:
Destroy(other.gameObject);
superJUmp++;
break;
case 8:
Destroy(other.gameObject);
Speed++;
break;
}
}
推荐阅读
- javascript - Observable 属性未初始化
- cross-browser - Robot Framework - 跨浏览器测试
- firemonkey - Firemonkey,多视图当前状态
- kotlin - 具有共享模块的多平台项目
- django - 无法在 HttpResponseRedirect 的环回请求中找到查询部分 | 会话未保存键/值
- hadoop - “无法为您的平台加载 native-hadoop 库”警告
- php - php://input 将 url 更改为不包含 index.php 时丢失数据
- user-interface - JavaFx 变化场景
- php - 如何从 Wordpress 插件 PHP 文件自动生成 POT 文件?
- html - 带引导程序的图像,打破列/ div