【问题标题】:Unity OnTriggerEnter or OnCollisionEnter does not work even with DebugUnity OnTriggerEnter 或 OnCollisionEnter 即使使用 Debug 也不起作用
【发布时间】:2023-01-07 04:44:34
【问题描述】:

正如标题所提到的,我的统一项目的触发器不起作用。这很奇怪,因为它适用于另一个项目,但不适用于这个项目。我搜索了其他参考资料,它说有一个是刚体,打开了“isTrigger”等。我遵循了所有这些,但它仍然不起作用。请帮忙!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.MLAgents;
using Unity.MLAgents.Actuators;
using Unity.MLAgents.Sensors;

public class MoveAgent : Agent
{
    [SerializeField] 
    private Transform targetTransform;

    public override void OnEpisodeBegin(){
        transform.position = new Vector3(42.59956f, 18.9153f, 144.7489f);
    }

    public override void CollectObservations(VectorSensor sensor){
        sensor.AddObservation(transform.position);
        sensor.AddObservation(targetTransform.position);
    }

    public override void OnActionReceived(ActionBuffers actions){
        float moveX = actions.ContinuousActions[0];
        float moveZ = actions.ContinuousActions[1];

        float moveSpeed = 2f;

        transform.localPosition += new Vector3(moveX, 0, moveZ) * Time.deltaTime * moveSpeed;
    }
    public override void Heuristic(in ActionBuffers actionsOut){
        ActionSegment<float> continuousActions = actionsOut.ContinuousActions;
        continuousActions[0] = Input.GetAxisRaw("Horizontal");
        continuousActions[1] = Input.GetAxisRaw("Vertical");
    }

    private void OnCollisionEnter(Collision collision){
        Debug.Log("CollisionEnter");
    }

    private void OnTriggerEnter(Collider other){
        Debug.Log("Trigger!");
        if (other.TryGetComponent<Goal>(out Goal goal)){
            SetReward(+1f);
            EndEpisode();
        }

        if (other.TryGetComponent<Wall>(out Wall wall)){
            SetReward(-1f);
            EndEpisode();
        }
    }
}

【问题讨论】:

  • 我确定您知道碰撞矩阵。首先,您是通过传送而不是通过无济于事的物理学来移动位置。第二。如果你不符合碰撞矩阵的标准,它就不起作用。

标签: c# unity3d


【解决方案1】:

我同样被困在这个问题上。 如果每个人都需要创建一个环境来解决这个问题,你可以按照这个视频https://www.youtube.com/watch?v=zPFU30tbyKs&t=691s编码。

有关我的环境的详细信息:

py -3.7.9 -m venv venv
venvScriptsctivate
python -m pip install --upgrade pip
pip install torch==1.7.0 -f https://download.pytorch.org/whl/torch_stable.html
pip install mlagents==0.29.0
mlagents-learn --help

Unity -> 机器学习代理 -> v2.2.1-exp.1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多