c# - OnCollisonEnter(Collision other) 没有检测到我的对象?
问题描述
我正在 Unity VR 中制作一款游戏,我在其中打数字并获得分数。
手套负责检测它们击中的数字之间的碰撞。在我的手套上,我有一个PunchScript
组件,每个数字都有一个rigidBody
和collider
。
问题是似乎从来没有发生过碰撞。我在碰撞检测代码中放置了一个Debug.LogError
来断言这一点。
我尝试打开/关闭所有对象的运动学并使用不同的碰撞系统无济于事。
这是我的PunchScript
组件:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PunchScript : MonoBehaviour
{
public SteamVR_TrackedObject hand;
private Rigidbody rBody;
private bool visible = true;
// Start is called before the first frame update
void Start()
{
rBody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
rBody.MovePosition(hand.transform.position);
rBody.MoveRotation(hand.transform.rotation);
// print(rBody.velocity.magnitude* 1000);
}
void OnCollisonEnter(Collision other)
{
Rigidbody otherR = other.gameObject.GetComponentInChildren<Rigidbody>();
if (other.gameObject.name == "frpnchbg") {
Debug.LogError("Hit!");
}
if (other == null)
return;
Vector3 avgPoint = Vector3.zero;
foreach (ContactPoint p in other.contacts) {
avgPoint += p.point;
}
avgPoint /= other.contacts.Length;
Vector3 dir = (avgPoint - transform.position).normalized;
otherR.AddForceAtPosition(dir *50f* rBody.velocity.magnitude, avgPoint);
}
}
这是手套对象在 Unity 检查器中的外观。
解决方案
正确编写 Unity 回调方法的名称非常重要,否则 Unity 将无法在对象上检测到它们(并因此永远无法执行它们)。
在您的情况下,您拼错了OnCollisionEnter回调。而不是OnCollisonEnter
应该是OnCollisionEnter
.
推荐阅读
- typescript - Blazor 的 TypeScript 类型
- reactjs - 如何使用功能性 React 组件使用回调事件
- c++ - 以下哪一项是正确的行为(g++ vs clang++-12)?
- reactjs - 同一个项目中的两个 React 实例
- android - Jetpack 撰写在设备内保存自定义对象可变列表的最佳方法是什么?
- sql-server - 使用 Get-StoredCredential 的 SQL Server 连接
- apache-nifi - Nifi 流不运行并显示活动线程消息
- windows - 笔记本中的非 ASCII 字符
- android - 如何摆脱这个错误,exoplayer 与我的应用程序发生冲突
- python - Python Tkinter Entry.get() 给出了先前的输入