【问题标题】:Collision detection with character controller in Unity 3d在 Unity 3d 中使用角色控制器进行碰撞检测
【发布时间】:2020-08-25 06:06:22
【问题描述】:

我的播放器使用角色控制器移动。我在场景中放置了一个精灵,我希望当我的玩家与精灵碰撞以禁用精灵时,例如玩家抓住精灵(这是 Doom 的 64 电锯)。

精灵的碰撞当然适用于一切,但不适用于玩家。我怎样才能在它们之间进行适当的碰撞?

【问题讨论】:

标签: c# unity3d collision-detection


【解决方案1】:

你可以这样做: 1-将“Pickable”脚本附加到精灵。 2-将“播放器”脚本附加到角色控制器。

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

public class Pickable : MonoBehaviour
{
    public float radius = 1f;
    private void Start()
    {
        SphereCollider collider = gameObject.AddComponent<SphereCollider>();
        collider.center = Vector3.zero;
        collider.radius = radius;
        collider.isTrigger = true;
    }
}

这是另一个脚本。

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

public class Player : MonoBehaviour
{
    private void OnTriggerEnter(Collider other)
    {
        Pickable pickable = other.GetComponent<Pickable>();
        if(pickable != null)
        {
            Destroy(other.gameObject);
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-17
    相关资源
    最近更新 更多