Collider 并不总是检测到 OnTriggerEnter

Collider does not always detect OnTriggerEnter

我在使用本书 "Unity in Action" 时遇到了一个问题。在第 3 章结束时,您将了解简单 fps 游戏的基础知识。它基本上是一个简单而小的水平的播放器(连接到它的相机),它只存在于形成墙壁和地板等的许多立方体中。这些立方体上都有盒子对撞机。现在玩家也可以射击移动的敌人,这些敌人也可以射击。这是由 Raycast/RaycastHit 完成的。所有这一切都很完美,所以我想添加一些东西来重新组装弹孔,只需在墙上实例化一个黑色球体对象,火球(这是敌人和玩家射击的对象)击中它。这行得通,但有时火球物体会穿过墙壁。如果它后面有另一堵墙,火球对象会被第二堵墙摧毁,所需的球体会在第二堵墙而不是第一堵墙上创建。

我把火球的速度从 20 改成 10,然后改回 20,速度为 10 时,成功率约为 19/20,而速度为 20 时,成功率约为 6/10。

这是火球的代码,它应该检查它是击中玩家(然后扣除生命值,可以正常工作)还是击中敌人(然后敌人倒下,也可以正常工作)或击中在这种情况下应该创建球体的墙。

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

 public class Fireball : MonoBehaviour {

 public float speed = 10.0f;
 public int damage = 1;
 [SerializeField] private GameObject wallHit;
 private GameObject _wallHit;

 // Use this for initialization
 void Start () {

 }

 // Update is called once per frame
 void Update () {
     transform.Translate(0,0, speed * Time.deltaTime);
 }

 void OnTriggerEnter(Collider other){
     RaycastHit hit;
     PlayerCharacter player = other.GetComponent<PlayerCharacter>();
     ReactiveTarget target = other.GetComponent<ReactiveTarget>();
     WallBehavior wall = other.GetComponent<WallBehavior>();

     if(player != null){
         player.Hurt(damage);
     }
     if(target != null){
         target.ReactToHit();
     }
     if(wall != null){
         if(Physics.Raycast(transform.position, transform.forward, out hit)){
             wall.WallWasHit(hit);
         }
     }
     Destroy(this.gameObject);
 }
}

如您所见,我尝试过为每面墙添加一个 WallBehavior 脚本,如下所示:

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

 public class WallBehavior : MonoBehaviour {
 [SerializeField] private GameObject wallHit;
 private GameObject _wallHit;
 static int count = 0;

 // Use this for initialization
 void Start () {

 }

 // Update is called once per frame
 void Update () {

 }
 public void WallWasHit(RaycastHit hit){
             count++;
             Debug.Log("Wall was hit: " + count);

             _wallHit = Instantiate(wallHit) as GameObject;
             _wallHit.transform.position = hit.point;
 }
 }

但是 none 到目前为止,我尝试理解为什么这种情况很少发生的尝试是成功的,我希望有人能帮助我,因为我觉得在我继续阅读这本书之前可能对我的学习至关重要!提前致谢。如果需要更多信息,我很乐意提供。请参阅下图以更好地了解问题。

编辑: 如答案中所述,我相应地替换了火球脚本,但我不确定如何同样更改以下脚本。这个脚本在我的相机上,它在我的播放器对象上。在 Update 函数中,Fireball 被实例化并进行了移动,所以我猜这会导致问题?

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

public class RayShooter : MonoBehaviour {
private Camera _camera;

[SerializeField] private GameObject fireballPrefab;
private GameObject _fireball;


void Start () {
    _camera = GetComponent<Camera>();

    Cursor.lockState = CursorLockMode.Locked;
    Cursor.visible = false;
}

void OnGUI(){
    int size = 12;
    float posX = _camera.pixelWidth/2 - size/4;
    float posY = _camera.pixelHeight/2 - size/2;
    GUI.Label(new Rect(posX, posY, size, size), "X");
}

// Update is called once per frame
void Update () {
    if(Input.GetMouseButtonDown(0)){
        Vector3 point = new Vector3(_camera.pixelWidth/2, _camera.pixelHeight/2, 0);
        _fireball = Instantiate(fireballPrefab) as GameObject;
        _fireball.transform.position = transform.TransformPoint(Vector3.forward * 1.5f);
        _fireball.transform.rotation = transform.rotation;

        Ray ray2 = _camera.ScreenPointToRay(point);
        RaycastHit hit;
        if(Physics.Raycast(ray2, out hit)){
            GameObject hitObject = hit.transform.gameObject;
            ReactiveTarget target = hitObject.GetComponent<ReactiveTarget>();
            if(target !=null){
                target.ReactToHit();
            } 
        }
    }
}
}

这不是移动 Rigidbody 对象的方法,像这样移动它可能会导致很多问题,包括您问题中提到的问题。具有 Rigidbody 的对象应使用 Rigidbody 组件以及 Rigidbody.MovePositionRigidbody.AddForceRigidbody.velocity 等函数移动,而不是通过 transformtransform.Translate

此外,您应该将 Rigidbody 对象移动到 FixedUpdate 函数而不是 Update 函数中。 如果您要通过变换移动其他 Rigidbody 对象,那么您也必须修复它们。 下面的示例将 transform.Translate 替换为 Rigidbody.MovePosition Fireball 脚本:

public float speed = 10.0f;
public int damage = 1;
[SerializeField]
private GameObject wallHit;
private GameObject _wallHit;

public Rigidbody rb;

// Use this for initialization
void Start()
{
    rb = GetComponent<Rigidbody>();
}

// Update is called once per frame
void FixedUpdate()
{
    //Move to towards Z-axis
    Vector3 pos = new Vector3(0, 0, 1);
    pos = pos.normalized * speed * Time.deltaTime;

    rb.MovePosition(rb.transform.position + pos);
}

void OnTriggerEnter(Collider other)
{
    RaycastHit hit;
    PlayerCharacter player = other.GetComponent<PlayerCharacter>();
    ReactiveTarget target = other.GetComponent<ReactiveTarget>();
    WallBehavior wall = other.GetComponent<WallBehavior>();

    if (player != null)
    {
        player.Hurt(damage);
    }
    if (target != null)
    {
        target.ReactToHit();
    }
    if (wall != null)
    {
        if (Physics.Raycast(transform.position, transform.forward, out hit))
        {
            wall.WallWasHit(hit);
        }
    }
    Destroy(this.gameObject);
}

如果您仍然 运行 遇到问题,请改用 Rigidbody.velocity

void FixedUpdate()
{
    Vector3 pos = Vector3.zero;
    pos.z = speed * Time.deltaTime;

    rb.velocity = pos;
}

有时,根据物体的大小和它移动的速度,你可能需要设置它的刚体 Interpolate from NoneInterpolateCollision DetectionContinuous

不要忘记删除 Update 函数中的代码。


编辑:

我看了你的项目,发现了新问题:

1。您在 "Fireball" GameObject 上启用了 IsTrigger。请取消选中对撞机上的IsTrigger

2.你只是想射子弹。力应该只添加一次,而不是每隔 FixedUpdate。在 Start 函数而不是 FixedUpdate 函数中添加力。使用 Rigidbody.velocity 而不是 Rigidbody.MovePosition

删除 FixedUpdate 函数和其中的代码。

这应该是您的新开始功能:

void Start()
{
    rb = GetComponent<Rigidbody>();

    Vector3 pos = transform.forward * speed * Time.deltaTime;
    rb.velocity = pos;
}