unity3d 碰撞不适用于示例化预设

h7appiyu  于 2023-01-31  发布在  其他
关注(0)|答案(1)|浏览(143)

好的,我有一个碰撞脚本,它的工作原理是,如果我将一个游戏对象与另一个附加了脚本的对象碰撞,它会注册一个碰撞。
我使用的碰撞脚本:

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

然而,当我尝试将示例化的预制件与附带脚本的对象碰撞时,碰撞没有注册。
有人知道这是为什么吗?我想这是因为预制屋是一个“可移动的”,但我不能完全得到这个问题的底部。
附在预制件上的脚本:

public class Moveable : MonoBehaviour
{
    public float _speedMetersPerSecond = 50f;
    public float resistance = 10f;
    public float current = 0f;
    public List<Moveable> moveables = new List<Moveable>();
    private Vector3? _destination;
    private Vector3 _startPosition;
    private float _totalLerpDuration;
    private float _elapsedLerpDuration;
    private Action _onCompleteCallback;
    public GameObject Electron;
    public Transform Lightbulb;
    public SliderChange SliderScript;
    public VMT2Counter script2;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            // THE LIGHTBULB PART IS FOR THE SPAWN POINT SO DO NOT DELETE
            Moveable NextOnPath = Instantiate(Electron, Lightbulb.position, Quaternion.identity).GetComponent<Moveable>();
            moveables.Add(NextOnPath.GetComponent<Moveable>());
            MoverController.instance.targets.Add(NextOnPath.GetComponent<Moveable>());
        }

        if (_destination.HasValue == false)
            return;

        if (_elapsedLerpDuration >= _totalLerpDuration && _totalLerpDuration > 0)
            return;

        _elapsedLerpDuration += Time.deltaTime;
        float percent = (_elapsedLerpDuration / _totalLerpDuration);

        transform.position = Vector3.Lerp(_startPosition, _destination.Value, percent);

        if (_elapsedLerpDuration >= _totalLerpDuration)
            _onCompleteCallback?.Invoke();

        // resistance = SliderScript.slider.value;
        // current = script2.PotentialDifference / resistance;

    }

    public void MoveTo(Vector3 destination, Action onComplete = null)
    {
        var distanceToNextWaypoint = Vector3.Distance(transform.position, destination);
        _totalLerpDuration = distanceToNextWaypoint / _speedMetersPerSecond;

        _startPosition = transform.position;
        _destination = destination;
        _elapsedLerpDuration = 0f;
        _onCompleteCallback = onComplete;
    }

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.name == "Resistor")
        { 
        Debug.Log("lol");
        _speedMetersPerSecond = 25f;
        }
    }
}

另一个脚本与Moveable脚本一起使用,但未附加到预置件:

public class MoverController : MonoBehaviour
{
    public List<Moveable> targets;
    [SerializeField] private Moveable target;
    private List<Transform> _waypoints;
    private int _nextWaypointIndex;
    public static MoverController instance;

    void Awake()
    {
        if (instance == null)
        {
            instance = this;
        }
        else
        {
            Destroy(gameObject);
            return;
        }
        DontDestroyOnLoad(gameObject);
    }

    private void OnEnable()
    {
        MoveToNextWaypoint();
    }

    private void MoveToNextWaypoint()
    {
        for (int i = 0; i < targets.Count; i++)
        {
            if (targets[i] == null) continue;
            _waypoints = GetComponentsInChildren<Transform>().ToList();
            _waypoints.RemoveAt(0);
            var targetWaypointTransform = _waypoints[_nextWaypointIndex];
            targets[i].MoveTo(targetWaypointTransform.position, MoveToNextWaypoint);
            targets[i].transform.LookAt(_waypoints[_nextWaypointIndex].position);
            _nextWaypointIndex++;

            if (_nextWaypointIndex >= _waypoints.Count)
                _nextWaypointIndex = 0;
        }
    }
}

[![预制件图片][1]][1]
[1]:https://i.stack.imgur.com/uyuge.png*emphasized文本 *
显示情况的视频:https://clipchamp.com/watch/MXjnTKl2cqg

jvidinwx

jvidinwx1#

对撞机是一个触发器:
| 对撞机A|对撞机B|事件触发|
| - ------|- ------|- ------|
| 对撞机|对撞机|碰撞|
| 对撞机|触发器|触发器|
| 触发器|对撞机|触发器|
| 触发器|触发器|触发器|
若要解决此问题,请取消选中该框以使其正常碰撞,或者更改事件处理程序以检测触发器而不是碰撞器:

private void OnTriggerEnter(Collider other) {
    Debug.Log("collision registered");
}

更多https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html信息请参见此处:)
同样值得指出的是,如果你的两个对象都没有刚体,那么事件将不会触发。如果你需要它触发,但不想所有的刚体物理,你可以添加一个,但勾选isKinematic框。

相关问题