在 Unity 3d 中使用角色控制器进行碰撞检测
Collision detection with character controller in Unity 3d
我的播放器使用角色控制器进行移动。我在场景中放置了一个精灵,我希望当我的玩家与精灵碰撞时禁用精灵,就像玩家抓住精灵(这是毁灭战士的 64 电锯)一样。
精灵的碰撞当然适用于一切,但不适用于玩家。我怎样才能使它们之间发生适当的碰撞?
你可以这样做:
1-将 "Pickable" 脚本附加到精灵。
2-将 "Player" 脚本附加到角色控制器。
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);
}
}
}
我的播放器使用角色控制器进行移动。我在场景中放置了一个精灵,我希望当我的玩家与精灵碰撞时禁用精灵,就像玩家抓住精灵(这是毁灭战士的 64 电锯)一样。
精灵的碰撞当然适用于一切,但不适用于玩家。我怎样才能使它们之间发生适当的碰撞?
你可以这样做: 1-将 "Pickable" 脚本附加到精灵。 2-将 "Player" 脚本附加到角色控制器。
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);
}
}
}